Name:
interface
Value:
Extend your Amplify Gen 2 app with AWS Blocks — self-contained backend capabilities you compose into your existing backend.

Page updated Feb 21, 2024

NewDeploy web apps with Git or your AI coding agent using the latest AWS Amplify. Add managed databases and authentication as you grow. Explore the documentation.
Maintenance ModeYou are viewing Amplify Gen 1 documentation. Amplify Gen 1 has entered maintenance mode and will reach end of life on May 1, 2027. For existing Gen 1 projects, a migration guide and tooling are available to help you upgrade.

Accessing credentials

These docs cover Amplify Gen 1. For new web apps, we recommend the latest AWS Amplify, with a simplified CLI and native integrations for databases, storage, and AI models. Get started with the latest AWS Amplify.

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}');
}
}