Page updated Jan 16, 2024

Advanced workflows

Subscribing Events

You can take specific actions when users sign-in or sign-out by subscribing 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 have logged in with Auth.signIn you can not call federateToIdentityPool as Amplify will perform this federation automatically for you in the background. In general, you should only call Auth.federateToIdentityPool when using OAuth flows.

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

1func federateToIdentityPools() async throws {
2 guard let authCognitoPlugin = try Amplify.Auth.getPlugin(
3 for: "awsCognitoAuthPlugin") as? AWSCognitoAuthPlugin else {
4 fatalError("Unable to get the Auth plugin")
5 }
6 do {
7 let result = try await authCognitoPlugin.federateToIdentityPool(
8 withProviderToken: "YOUR_TOKEN", for: .facebook)
9 print("Federation successful with result: \(result)")
10 } catch {
11 print("Failed to federate to identity pools with error: \(error)")
12 }
13}

Note that when federated, API's 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 session using the Auth.fetchAuthSession API.

Token Refresh

NOTE: 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.

1func clearFederationToIdentityPools() async throws {
2 guard let authCognitoPlugin = try Amplify.Auth.getPlugin(
3 for: "awsCognitoAuthPlugin") as? AWSCognitoAuthPlugin else {
4 fatalError("Unable to get the Auth plugin")
5 }
6 do {
7 try await authCognitoPlugin.clearFederationToIdentityPool()
8 print("Federation cleared successfully")
9 } catch {
10 print("Clear federation failed with error: \(error)")
11 }
12}

clearFederationToIdentityPool will only clear the session from local cache, developer need 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.

1func federateToIdentityPoolsUsingCustomIdentityId() async throws {
2 guard let authCognitoPlugin = try Amplify.Auth.getPlugin(
3 for: "awsCognitoAuthPlugin") as? AWSCognitoAuthPlugin else {
4 fatalError("Unable to get the Auth plugin")
5 }
6 do {
7 let identityId = "YOUR_CUSTOM_IDENTITY_ID"
8 let result = try await authCognitoPlugin.federateToIdentityPool(
9 withProviderToken: "YOUR_TOKEN",
10 for: .facebook,
11 options: .init(developerProvidedIdentityID: identityId))
12 print("Federation successful with result: \(result)")
13 } catch {
14 print("Failed to federate to identity pools with error: \(error)")
15 }
16}