Accessing credentials
An intentional decision with Amplify Auth was to avoid any public methods exposing credentials or manipulating them.
With Auth, you simply sign in and it handles everything else needed to keep the credentials up to date and vend them to the other categories.
However, if you need to access them in relation to working with an API outside Amplify or want access to AWS specific identifying information (e.g. IdentityId), you can access these implementation details by calling fetchAuthSession on the Cognito Auth Plugin. This will return a CognitoAuthSession
, which has additional attributes compared to AuthSession
, which is typically returned by fetchAuthSession. See the example below:
Future<void> fetchAuthSession() async { try { final result = await Amplify.Auth.fetchAuthSession(); safePrint('User is signed in: ${result.isSignedIn}'); } on AuthException catch (e) { safePrint('Error retrieving auth session: ${e.message}'); }}
Retrieving AWS credentials
Sometimes it can be helpful to retrieve the instance of the underlying plugin
which has more specific typing. In the case of Cognito, calling fetchAuthSession
on the Cognito plugin returns AWS-specific values such as the identity ID,
AWS credentials, and Cognito User Pool tokens.
Future<void> fetchCognitoAuthSession() async { try { final cognitoPlugin = Amplify.Auth.getPlugin(AmplifyAuthCognito.pluginKey); final result = await cognitoPlugin.fetchAuthSession(); final identityId = result.identityIdResult.value; safePrint("Current user's identity ID: $identityId"); } on AuthException catch (e) { safePrint('Error retrieving auth session: ${e.message}'); }}