Page updated Jan 16, 2024

Advanced workflows

Subscribing Events

You can take specific actions when users sign-in or sign-out by subscribing to authentication events in your app. Please see our Hub Module Developer Guide for more information.

Identity Pool Federation

Imagine that you are creating a mobile app that accesses AWS resources, such as a game that runs on a mobile device and stores player and score information using Amazon S3 and DynamoDB.

When you write such an app, you make requests to AWS services that must be signed with an AWS access key. However, we strongly recommend that you do not embed or distribute long-term AWS credentials with apps that a user downloads to a device, even in an encrypted store. Instead, build your app so that it requests temporary AWS security credentials dynamically when needed using web identity federation. The supplied temporary credentials map to an AWS role that has only the permissions needed to perform the tasks required by the mobile app.

With web identity federation, you don't need to create custom sign-in code or manage your own user identities. Instead, users of your app can sign in using a well-known external identity provider (IdP), such as Login with Amazon, Facebook, Google, or any other OpenID Connect (OIDC)-compatible IdP. They can receive an authentication token, and then exchange that token for temporary security credentials in AWS that map to an IAM role with permissions to use the resources in your AWS account. Using an IdP helps you keep your AWS account secure because you don't have to embed and distribute long-term security credentials with your application.

You can use federateToIdentityPool to get AWS credentials directly from Cognito Federated Identities and not use User Pool federation. If you logged in with Auth.signIn you cannot call federateToIdentityPool as Amplify will perform this federation automatically for you in the background. In general, you should only call Auth.federatedSignIn() when using OAuth flows.

You can use the escape hatch API federateToIdentityPool with a valid token from other social providers.

1if (Amplify.Auth.getPlugin("awsCognitoAuthPlugin") instanceof AWSCognitoAuthPlugin) {
2 AWSCognitoAuthPlugin plugin = (AWSCognitoAuthPlugin) Amplify.Auth.getPlugin("awsCognitoAuthPlugin");
3 plugin.federateToIdentityPool(
4 "YOUR_TOKEN",
5 AuthProvider.facebook(),
6 result -> {
7 Log.i("AuthQuickstart", "Successful federation to Identity Pool.");
8 // use result.getCredentials()
9 },
10 e -> {
11 Log.e("AuthQuickstart", "Failed to federate to Identity Pool.", e)
12 }
13 );
14}
1(Amplify.Auth.getPlugin("awsCognitoAuthPlugin") as? AWSCognitoAuthPlugin)?.let { plugin ->
2 plugin.federateToIdentityPool(
3 "YOUR_TOKEN",
4 AuthProvider.facebook(),
5 {
6 Log.i("AuthQuickstart", "Successful federation to Identity Pool.")
7 // use "it.credentials"
8 },
9 {
10 Log.e("AuthQuickstart", "Failed to federate to Identity Pool.", it)
11 }
12 )
13}

Note that when federated, APIs such as Auth.getCurrentUser will throw an error as the user is not authenticated with User Pools.

Retrieve Session

After federated login, you can retrieve the session using the Auth.fetchAuthSession API.

Token Refresh

Automatic authentication token refresh is NOT supported when federated.

By default, Amplify will NOT automatically refresh the tokens from the federated providers. You will need to handle the token refresh logic and provide the new token to the federateToIdentityPool API.

Clear Session

You can clear the federated session using the clearFederationToIdentityPool API.

1if (Amplify.Auth.getPlugin("awsCognitoAuthPlugin") instanceof AWSCognitoAuthPlugin) {
2 AWSCognitoAuthPlugin plugin = (AWSCognitoAuthPlugin) Amplify.Auth.getPlugin("awsCognitoAuthPlugin");
3 plugin.clearFederationToIdentityPool(
4 () -> Log.i("AuthQuickstart", "Federation cleared successfully."),
5 e -> Log.e("AuthQuickstart", "Failed to clear federation.", e)
6 );
7}
1(Amplify.Auth.getPlugin("awsCognitoAuthPlugin") as? AWSCognitoAuthPlugin)?.let { plugin ->
2 plugin.clearFederationToIdentityPool(
3 { Log.i("AuthQuickstart", "Federation cleared successfully.") },
4 { Log.e("AuthQuickstart", "Failed to clear federation.", it) }
5 )
6}

clearFederationToIdentityPool will only clear the session from the local cache, the developer needs to handle signing out from the federated provider.

Provide Custom Identity Id

You can provide a custom identity id to the federateToIdentityPool API. This is useful when you want to use the same identity id across multiple devices.

1FederateToIdentityPoolOptions options = FederateToIdentityPoolOptions.builder()
2 .developerProvidedIdentityId("YOUR_CUSTOM_IDENTITY_ID")
3 .build();
4
5if (Amplify.Auth.getPlugin("awsCognitoAuthPlugin") instanceof AWSCognitoAuthPlugin) {
6 AWSCognitoAuthPlugin plugin = (AWSCognitoAuthPlugin) Amplify.Auth.getPlugin("awsCognitoAuthPlugin");
7 plugin.federateToIdentityPool(
8 "YOUR_TOKEN",
9 AuthProvider.facebook(),
10 options,
11 result -> {
12 Log.i("AuthQuickstart", "Successful federation to Identity Pool.");
13 // use result.getCredentials()
14 },
15 e -> {
16 Log.e("AuthQuickstart", "Failed to federate to Identity Pool.", e)
17 }
18 );
19}
1val options = FederateToIdentityPoolOptions.builder()
2 .developerProvidedIdentityId("YOUR_CUSTOM_IDENTITY_ID")
3 .build()
4
5(Amplify.Auth.getPlugin("awsCognitoAuthPlugin") as? AWSCognitoAuthPlugin)?.let { plugin ->
6 plugin.federateToIdentityPool(
7 "YOUR_TOKEN",
8 AuthProvider.facebook(),
9 options,
10 {
11 Log.i("AuthQuickstart", "Successful federation to Identity Pool.")
12 // use "it.credentials"
13 },
14 {
15 Log.e("AuthQuickstart", "Failed to federate to Identity Pool.", it)
16 }
17 )
18}