JBa*_*zuk 11 javascript amazon-web-services amazon-cognito
我使用开发人员身份验证身份在客户端浏览器上通过Cognito进行身份验证.当我的页面加载(或刷新)时,我希望我的应用程序记住身份,只要对象没有过期(我认为它持续大约一个小时).但是,我不知道如何从Cognito中检索身份,而无需再次通过开发人员身份验证.
以下是代码在页面加载时执行的操作:
var cognitoCredentials
$(document).ready(function() {
"use strict";
cognitoParams = {
IdentityPoolId: 'us-east-1:xxxxxxx'
};
cognitoCredentials = new AWS.CognitoIdentityCredentials(cognitoParams);
AWS.config.credentials = cognitoCredentials;
});
Run Code Online (Sandbox Code Playgroud)
通过开发人员身份验证登录后:
cognitoCredentials.params.IdentityId = output.identityId;
cognitoCredentials.params.Logins = {
'cognito-identity.amazonaws.com': output.token
};
cognitoCredentials.expired = true;
Run Code Online (Sandbox Code Playgroud)
如果我已经登录,然后刷新页面,并尝试再次登录,我得到一个错误,我正在尝试获取身份,当我已经有一个
Error: Missing credentials in config(…) NotAuthorizedException: Missing credentials in config
"Access to Identity 'us-east-1:xxxxxxx' is forbidden."
但是,我不知道如何访问它.如何检索凭据,以便在刷新页面时,我可以检测到Cognito提供的先前身份?
BKH*_*BKH 11
保存至少accessKeyId, secretAccessKey, sessionToken在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, etcRun Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4653 次 |
| 最近记录: |