本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。
将未经身份验证的用户切换为经过身份验证的用户
Amazon Cognito 身份池同时支持经过身份验证的用户和未经身份验证的用户。未经身份验证的用户即使未使用您的任何身份提供商登录,也可以访问您的 Amazon 资源(IdPs)。这种访问权限对于在用户登录之前向其显示内容非常有用。每个未经身份验证的用户在身份池中都有一个唯一的身份,即使他们尚未单独登录和进行身份验证。
本节介绍您的用户选择从使用未经身份验证的身份登录切换到使用经过身份验证的身份登录的情况。
Android
用户可以以未经身份验证的访客身份登录您的应用程序。最终,他们可能会决定使用其中一个支持的登录 IdPs。Amazon Cognito 将确保旧身份保留与新身份相同的唯一标识符,并确保配置文件数据自动合并。
应用程序会通过 IdentityChangedListener 界面收到配置文件合并的消息。在界面中实施 identityChanged 方法以接收这些消息:
@override public void identityChanged(String oldIdentityId, String newIdentityId) { // handle the change }
iOS – objective-C
用户可以以未经身份验证的访客身份登录您的应用程序。最终,他们可能会决定使用其中一个支持的登录 IdPs。Amazon Cognito 将确保旧身份保留与新身份相同的唯一标识符,并确保配置文件数据自动合并。
NSNotificationCenter 通知应用程序配置文件合并的消息:
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(identityIdDidChange:) name:AWSCognitoIdentityIdChangedNotification object:nil]; -(void)identityDidChange:(NSNotification*)notification { NSDictionary *userInfo = notification.userInfo; NSLog(@"identity changed from %@ to %@", [userInfo objectForKey:AWSCognitoNotificationPreviousId], [userInfo objectForKey:AWSCognitoNotificationNewId]); }
iOS – swift
用户可以以未经身份验证的访客身份登录您的应用程序。最终,他们可能会决定使用其中一个支持的登录 IdPs。Amazon Cognito 将确保旧身份保留与新身份相同的唯一标识符,并确保配置文件数据自动合并。
NSNotificationCenter 通知应用程序配置文件合并的消息:
[NSNotificationCenter.defaultCenter().addObserver(observer: self selector:"identityDidChange" name:AWSCognitoIdentityIdChangedNotification object:nil) func identityDidChange(notification: NSNotification!) { if let userInfo = notification.userInfo as? [String: AnyObject] { print("identity changed from: \(userInfo[AWSCognitoNotificationPreviousId]) to: \(userInfo[AWSCognitoNotificationNewId])") } }
JavaScript
最初未经身份验证的用户
用户最初通常具有未经身份验证的角色。对于此角色,您可以创建一个不带logins属性的fromCognitoIdentityPool凭证提供程序并将其附加到您的服务客户端。在这种情况下,您的默认配置可能如下所示:
import { fromCognitoIdentityPool } from "@aws-sdk/credential-providers"; // Create a credentials provider without a logins map for the unauthenticated role. const region = "us-east-1"; const credentials = fromCognitoIdentityPool({ identityPoolId: "us-east-1:1699ebc0-7900-4099-b910-2df94f52a030", clientConfig: { region } }); // Attach the provider to each service client that needs credentials. const client = new SomeServiceClient({ region, credentials });
切换为经过身份验证的用户
当未经身份验证的用户登录 IdP 并且您拥有令牌时,您可以调用一个自定义函数,使用包含logins该令牌的地图创建fromCognitoIdentityPool新的提供者,从而将该用户从未经身份验证切换为经过身份验证:
// Called when an identity provider has a token for a logged in user function userLoggedIn(providerName, token) { // Re-create the provider with a logins map to switch to the authenticated role. const credentials = fromCognitoIdentityPool({ identityPoolId: "us-east-1:1699ebc0-7900-4099-b910-2df94f52a030", logins: { [providerName]: token }, clientConfig: { region } }); // Re-create any service clients so they pick up the new credentials. // SomeServiceClient is a placeholder: import the client for the service you're calling // (for example, S3Client from @aws-sdk/client-s3). const client = new SomeServiceClient({ region, credentials }); }
由于fromCognitoIdentityPool提供者直接连接到每个服务客户端,因此从未经身份验证的角色切换到经过身份验证的角色意味着使用logins地图创建一个新的提供者,然后将其传递给随后创建的任何服务客户端。与 Amazon 适用于 JavaScript v2 的 SDK 不同,v3 SDK 不使用全局配置对象,因此无需重置共享凭据。
有关fromCognitoIdentityPool提供商的更多信息,请参阅Amazon 适用于 JavaScript v3 凭证提供商的 SDK。