AWS Cognito + google 注册
AWS Cognito + google signup
我已经尝试了下面的代码,它工作正常。但是我需要将这些注册详细信息存储在用户池中(另外我还想添加一些自定义属性)。但是我没有找到合适的方法来做到这一点。
function signinCallback(authResult) {
AWS.config.region = 'us-XXXXXXX-1';
// Add the Google access token to the Cognito credentials login map.
AWS.config.credentials = new AWS.CognitoIdentityCredentials({
IdentityPoolId: 'us-XXXX-1:XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
RoleArn: 'arn:aws:iam::XXXXXXXX:role/Cognito_XXXXXXXXXUnauth_Role',
Logins: {
'accounts.google.com': authResult['id_token']
}
});
// Obtain AWS credentials
AWS.config.credentials.get(function (err) {
alert(err);
if (err) {
console.log(err);
} else {
//client = new AWS.CognitoSyncManager();
console.log(AWS.config.credentials);
console.log("Cognito Identity Id: " + AWS.config.credentials.identityId);
}});
}
<span class="g-signin" data-callback="signinCallback" data-clientid="XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX-XXXXXXX.apps.googleusercontent.com"
data-cookiepolicy="single_host_origin" data-requestvisibleactions="http://schemas.google.com/AddActivity"
data-scope="https://www.googleapis.com/auth/plus.login">
</span>
我想把它保存在这里。
根据您的代码片段,您正在使用 Cognito 联合身份(即身份池)并将您的 Google 令牌添加到登录映射。这不会将 Google 用户添加到您的 Cognito 用户池,因为在联合身份中,Cognito 用户池只是另一个身份提供者 (IdP),如 Google。就像在您的用户池中注册新用户不会创建新的 Google 或 Facebook 帐户一样,类似地添加 Google 令牌不会创建新的用户池用户。简而言之,Cognito Userpool 与 IdentityPool 是分开的,IdentityPool 中的活动(例如在登录映射中添加 Google 令牌)不会影响它。
如果您想自动将 google 用户添加到您的用户池中,有一种方法可以做到。您需要在用户池中添加 Google as an Identity Provider to your Userpool directly & use the Cognito's built-in (i.e hosted) UI for login. After this, all Google logins will automatically, create a new user in Userpool. Now, just add your userpool to your Identity pool i.e remove Google from your Identity Pool. In your login map, you will always use a Cognito token. Even when you login using Google (via the hosted UI), the Google token is sent directly to userpool and it vends a Cognito token. Also, make sure you specify correct attribute mappings。
我已经尝试了下面的代码,它工作正常。但是我需要将这些注册详细信息存储在用户池中(另外我还想添加一些自定义属性)。但是我没有找到合适的方法来做到这一点。
function signinCallback(authResult) {
AWS.config.region = 'us-XXXXXXX-1';
// Add the Google access token to the Cognito credentials login map.
AWS.config.credentials = new AWS.CognitoIdentityCredentials({
IdentityPoolId: 'us-XXXX-1:XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
RoleArn: 'arn:aws:iam::XXXXXXXX:role/Cognito_XXXXXXXXXUnauth_Role',
Logins: {
'accounts.google.com': authResult['id_token']
}
});
// Obtain AWS credentials
AWS.config.credentials.get(function (err) {
alert(err);
if (err) {
console.log(err);
} else {
//client = new AWS.CognitoSyncManager();
console.log(AWS.config.credentials);
console.log("Cognito Identity Id: " + AWS.config.credentials.identityId);
}});
}
<span class="g-signin" data-callback="signinCallback" data-clientid="XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX-XXXXXXX.apps.googleusercontent.com"
data-cookiepolicy="single_host_origin" data-requestvisibleactions="http://schemas.google.com/AddActivity"
data-scope="https://www.googleapis.com/auth/plus.login">
</span>
我想把它保存在这里。
根据您的代码片段,您正在使用 Cognito 联合身份(即身份池)并将您的 Google 令牌添加到登录映射。这不会将 Google 用户添加到您的 Cognito 用户池,因为在联合身份中,Cognito 用户池只是另一个身份提供者 (IdP),如 Google。就像在您的用户池中注册新用户不会创建新的 Google 或 Facebook 帐户一样,类似地添加 Google 令牌不会创建新的用户池用户。简而言之,Cognito Userpool 与 IdentityPool 是分开的,IdentityPool 中的活动(例如在登录映射中添加 Google 令牌)不会影响它。
如果您想自动将 google 用户添加到您的用户池中,有一种方法可以做到。您需要在用户池中添加 Google as an Identity Provider to your Userpool directly & use the Cognito's built-in (i.e hosted) UI for login. After this, all Google logins will automatically, create a new user in Userpool. Now, just add your userpool to your Identity pool i.e remove Google from your Identity Pool. In your login map, you will always use a Cognito token. Even when you login using Google (via the hosted UI), the Google token is sent directly to userpool and it vends a Cognito token. Also, make sure you specify correct attribute mappings。