我使用开发人员经过身份验证的身份通过客户端浏览器上的 Cognito 进行身份验证。当我的页面加载(或刷新)时,我希望我的应用程序能够记住身份,只要对象没有过期(我认为它会持续大约一个小时)。但是,我不知道如何从 Cognito 检索身份,而无需再次进行开发人员身份验证。
以下是代码在页面加载时执行的操作:
var cognitoCredentials
$(document).ready(function() {
"use strict";
cognitoParams = {
IdentityPoolId: 'us-east-1:xxxxxxx'
};
cognitoCredentials = new AWS.CognitoIdentityCredentials(cognitoParams);
AWS.config.credentials = cognitoCredentials;
});
并且通过开发者认证登录后:
cognitoCredentials.params.IdentityId = output.identityId;
cognitoCredentials.params.Logins = {
'cognito-identity.amazonaws.com': output.token
};
cognitoCredentials.expired = true;
如果我已经登录,然后刷新页面,然后尝试再次登录,我会收到一条错误消息,提示我正在尝试获取身份,而我已经拥有身份Error: Missing credentials in config(…) NotAuthorizedException: Missing credentials in config
"Access to Identity 'us-east-1:xxxxxxx' is forbidden."
但是,我不知道如何访问它。如何检索凭据,以便在刷新页面时可以检测到 Cognito 提供的先前身份?
至少节省accessKeyId, secretAccessKey, sessionToken
in sessionStorage
页面之间。您可以将它们加载到 AWS.config.credentials 中(当然是在加载 AWS SDK 之后)。这比等待 Cognito 响应要快得多。请记住,您必须使用提供商之一的令牌手动刷新它们,并且这只在临时令牌过期(大约 1 小时)之前有效。
var credKeys = [
'accessKeyId',
'secretAccessKey',
'sessionToken'
];
// After Cognito login
credKeys.forEach(function(key) {
sessionStorage.setItem(key, AWS.config.credentials[key]);
});
// After AWS SDK load
AWS.config.region = 'us-east-1'; // pick your region
credKeys.forEach(function(key) {
AWS.config.credentials[key] = sessionStorage.getItem(key);
});
// Now make your AWS calls to S3, DynamoDB, etc
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)