AWS Cognito + 谷歌注册

问题描述 投票:0回答:2

我已经尝试了下面的代码,它工作正常。但是,我需要将这些注册详细信息存储在用户池中(此外,我还想添加一些自定义属性)。但我没有找到合适的方法来做到这一点。

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>

我想把它保存在这里。

javascript amazon-web-services aws-sdk amazon-cognito
2个回答
12
投票

根据您的代码片段,您正在使用 Cognito 联合身份(即身份池)并将您的 Google 令牌添加到登录映射中。这不会将 Google 用户添加到您的 Cognito 用户池,因为在联合身份中,Cognito 用户池只是另一个像 Google 一样的身份提供商 (IdP)。就像在用户池中注册新用户不会创建新的 Google 或 Facebook 帐户一样,添加 Google 令牌也不会创建新的用户池用户。简而言之,Cognito Userpool 与 IdentityPool 是分开的,IdentityPool 中的活动(例如在登录映射中添加 Google 令牌)不会影响它。

如果您想自动将 google 用户添加到您的用户池中,有一种方法可以实现。您需要直接将 Google 作为身份提供商添加到您的用户池中 & 使用 Cognito 的 内置(即托管)UI 进行登录。此后,所有 Google 登录都会自动在用户池中创建一个新用户。现在,只需将您的用户池添加到您的身份池中,即从您的身份池中删除 Google。在您的登录映射中,您将始终使用 Cognito 令牌。即使您使用 Google 登录(通过托管 UI),Google 令牌也会直接发送到用户池,并提供 Cognito 令牌。另外,请确保您在用户池中指定正确的属性映射


0
投票

这是使用 aws amplify 的最新版本(以上或@6.0.0)进行 google 注册的解决方案。

您需要安装库 amazon-cognito-identity-jsaws-sdk/client-cognito-identityaws-amplify

这里默认的Amplify配置使用userPoolId、userPoolClientId、identityPoolId以传统方式设置,如下所示。

Amplify.configure({
  Auth: {
    Cognito: {
      identityPoolId,
      userPoolId,
      userPoolClientId,
    },
  },
})

const currentCofig = Amplify.getConfig()

自定义凭证提供程序直接从 Cognito 联合身份获取 AWS 凭证,而不使用用户池联合。(不是 Cognito 托管的 UI)它适用于联合身份提供程序,例如 google 或 facebook。

这个 Custom Credential Provider 类有助于从 Cognito 获取凭证并与会话绑定。

cognitoTokenProvider 函数解码联合令牌并返回 cognito id 令牌和访问令牌。

  signInWithGoogle: async (googleResponse) => {
  
    const awsconfig = Amplify.getConfig() // default config

    const cognitoTokenProvider = {
      async getTokens ({ forceRefresh } = {}) {
        if (forceRefresh) {
          // try to obtain new tokens if possible
        }

        const accessTokenString = googleResponse.credential // google token
        const idTokenString = googleResponse.credential    // google token

        return {
          accessToken: decodeJWT(accessTokenString),
          idToken: decodeJWT(idTokenString),
        }
      },
    }

    Amplify.configure({
      Auth: {
        Cognito: awsconfig.Auth.Cognito
      },
    }, {
      Auth: {
        credentialsProvider: customCredentialsProvider, // for getting credential from cognito
        tokenProvider: cognitoTokenProvider // for getting token from cognito
      }
    }
    )

    customCredentialsProvider.loadFederatedLogin({ domain: 'accounts.google.com', token: googleResponse.credential })
    const fetchSessionResult = await fetchAuthSession()
  }

最后我们还可以获得带有凭据和令牌的会话。

© www.soinside.com 2019 - 2024. All rights reserved.