javascript - 如何使用 Amazon Cognito 托管的 Web UI 获取用户参数

标签 javascript amazon-web-services amazon-cognito

最近我使用了类似于 this 的注册和登录模板,由 Vladimir Budilov 开发。

但现在,我一直在修改我的应用程序以使用 Amazon 开发的托管 UI。因此,我的应用程序重定向到托管 UI,所有身份验证都在那里进行,他们向我发送身份验证 token ,更多操作系统更少,如 this 教程中所述。

总结一下,我调用托管 UI 并登录: https://my_domain/login?response_type=token&client_id=my_client_id&redirect_uri=https://www.example.com

我被重定向到: https://www.example.com/#id_token=123456789tokens123456789&expires_in=3600&token_type=Bearer

所以,我现在有了 token_id,但我无法从中获取当前用户或用户参数。有人可以帮助我提供信息或一些指示吗?

我已经尝试过 Amazon developer guide 中的方法。

当我使用 Vladimir Budilov 的模板但尝试使用 token_id 时,它运行良好,但没有成功。预先感谢您的时间和帮助。

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

if (cognitoUser != null) {
    cognitoUser.getSession(function(err, session) {
        if (err) {
           alert(err);
            return;
        }
        console.log('session validity: ' + session.isValid());

        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();

    });
}

最佳答案

您配置为添加为声明的属性已在具有 Base64 编码的 id_token 中可用(因为它是 JWT token )。

您可以使用 Javascript 在客户端和服务器上解码 token 并访问这些属性。

有关更多信息,请参阅 StackOverflow 问题 How to decode JWT tokens in JavaScript .

注意:如果您需要信任这些属性以进行后端操作,请确保在信任这些属性之前验证 JWT 签名。

关于javascript - 如何使用 Amazon Cognito 托管的 Web UI 获取用户参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50499372/

相关文章:

javascript - VueJS : unable to update a component property from inside a watcher

amazon-web-services - 如何在 AWS 账户与外部服务或应用程序(不在 AWS 上)之间建立连接

amazon-cognito - 将 Github 身份提供商添加到 AWS Cognito

javascript - 如何使用排序键中的任何值查询 DynamoDB?

amazon-web-services - AWS ELB : Failed to load resource: the server responded with a status of 503 (Service Unavailable: Back-end server is at capacity)

node.js - Cognito 身份验证后自定义响应

amazon-web-services - Aws Cognito,删除用户名/密码登录并仅使用社交网站登录

javascript - jquery 在填充选择框后找不到选定的节点

javascript - JS如何让列表项滑动

javascript - Facebook Feed,在墙上张贴两张照片。 JavaScript SDK