使用 AWS Cognito 进行电子邮件和 Google 身份验证

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

我的应用程序当前使用 Cognito 用户池进行电子邮件和密码身份验证。它运作得很好。我现在想添加谷歌身份验证。

我已按照此处的文档添加 google 作为身份提供商http://docs.aws.amazon.com/cognito/latest/developerguide/cognito-user-pools-social.html

我已经使用 google 对我的用户进行了身份验证,并从 google 获取了一个身份验证令牌和一个 id 令牌。我不确定下一步该做什么。

我想我以某种方式将此令牌提供给cognito,而cognito为我提供了一个cognito id令牌,我可以使用它对我的应用程序进行身份验证。

amazon-web-services authentication google-authentication amazon-cognito
2个回答
3
投票

Cognito 不直接接受 Google 令牌。您将需要使用 auth sdk 与授权/令牌端点交互:
https://github.com/aws/amazon-cognito-auth-js/
https://github.com/aws/amazon-cognito-identity-js
您需要先使用 Google 登录。将在您的用户池中创建相应的用户,并且身份验证 SDK 会将该用户名和令牌保存在本地存储中(与此 SDK 检索用户名的位置相同)。通过使用此 SDK 中的用例 16,您可以检索该用户和包含令牌的会话。


0
投票

仅供参考,下面是 amazon-cognito-identity-js 和案例 16 代码片段的新链接,以供参考

https://github.com/aws-amplify/amplify-js/tree/master/packages/amazon-cognito-identity-js

// Use case 16. Retrieving the current user from local storage.

var poolData = {
    UserPoolId: '...', // Your user pool id here
    ClientId: '...', // Your client id here
};
var userPool = new AmazonCognitoIdentity.CognitoUserPool(poolData);
var cognitoUser = userPool.getCurrentUser();

if (cognitoUser != null) {
    cognitoUser.getSession(function(err, session) {
        if (err) {
            alert(err.message || JSON.stringify(err));
            return;https://stackoverflow.com/questions/46039134/email-and-google-authentication-using-aws-cognito#
        }
        console.log('session validity: ' + session.isValid());

        // NOTE: getSession must be called to authenticate user before calling getUserAttributes
        cognitoUser.getUserAttributes(function(err, attributes) {
            if (err) {
                // Handle error
            } else {
                // Do something with attributes
            }
        });

        AWS.config.credentials = new AWS.CognitoIdentityCredentials({
            IdentityPoolId: '...', // your identity pool id here
            Logins: {
                // Change the key below according to the specific region your user pool is in.
                'cognito-idp.<region>.amazonaws.com/<YOUR_USER_POOL_ID>': session
                    .getIdToken()
                    .getJwtToken(),
            },
        });

        // Instantiate aws sdk service objects now that the credentials have been updated.
        // example: var s3 = new AWS.S3();
    });
}

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