最近我使用了类似于 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/