【发布时间】:2017-06-19 10:54:30
【问题描述】:
使用他们github page 上的示例构建一个简单的应用程序。我可以使用 Cognito 登录我的应用程序。我不能做的是注销,因为无论我尝试什么,我都无法获得用户对象。我对其他各种调用无济于事(在他们的 API 页面上找到here)。我发现的唯一other post on SO 不适用,因为我没有使用联合身份。我使用的代码几乎是 github 页面上的逐字记录,但为方便起见,将在此处发布:
登录代码:
var userName = $('#user_name_login').val();
var userPassword = $('#user_password_login').val();
var userData = {Username: userName, Pool : userPool};
var cognitoUser = new AWSCognito.CognitoIdentityServiceProvider.CognitoUser(userData);
var authenticationData = {Username : userName, Password : userPassword};
var authenticationDetails = new AWSCognito.CognitoIdentityServiceProvider.AuthenticationDetails(authenticationData);
cognitoUser.authenticateUser(authenticationDetails, {
onSuccess: function (result) {
// now that we've gotten our identity credentials, we're going to check in with the federation so we can
// avail ourselves of other amazon services
//
// critical that you do this in this manner -- see https://github.com/aws/amazon-cognito-identity-js/issues/162
// for details
var loginProvider = {};
loginProvider[cognitoCredentialKey] = result.getIdToken().getJwtToken();
AWS.config.credentials = new AWS.CognitoIdentityCredentials({
IdentityPoolId: identityPoolId,
Logins: loginProvider,
});
// //AWS.config.credentials = AWSCognito.config.credentials;
// AWSCognito.config.credentials = AWS.config.credentials;
// //call refresh method in order to authenticate user and get new temp credentials
// AWS.config.credentials.refresh((error) => {
// if (error) {
// alert(error);
// } else {
// console.log('Successfully logged in!');
// }
// });
// this is the landing page once a user completes the authentication process. we're getting a
// temporary URL that is associated with the credentials we've created so we can access the
// restricted area of the s3 bucket (where the website is, bruah).
var s3 = new AWS.S3();
var params = {Bucket: '********.com', Key: 'restricted/pages/user_logged_in_test.html'};
s3.getSignedUrl('getObject', params, function (err, url) {
if (err) {
alert(err);
console.log(err);
}
else {
console.log("The URL is", url);
window.location = url;
}
});
},
mfaRequired: function(session){
new MFAConfirmation(cognitoUser, 'login');
},
onFailure: function(err) {
alert("err: " + err);
},
});
我正在尝试通过执行注销:
userPool.getCurrentUser().signOut();
请注意,userPool 等是在另一个文件中定义的,因此被初始化:
var poolData = {
UserPoolId : '*****',
ClientId : '*****'
};
var userPool = new AWSCognito.CognitoIdentityServiceProvider.CognitoUserPool(poolData);
那么我如何让我的用户退出应用程序?
【问题讨论】:
-
就在您尝试退出之前,如果您在浏览器的控制台中输入
localStorage.getItem('CognitoIdentityServiceProvider.<CLIENT_ID>.LastAuthUser'),它会显示当前用户名吗? -
不,它显示为空。我已验证 CLIENT_ID 正确
标签: amazon-web-services aws-cognito