Amazon web services AWS Cognito getCurrentUser()在身份验证后不刷新
我正在尝试在身份验证之后立即更新用户的属性。 Auth工作正常,我可以用它检索用户属性。 但问题是Amazon web services AWS Cognito getCurrentUser()在身份验证后不刷新,amazon-web-services,amazon-cognito,Amazon Web Services,Amazon Cognito,我正在尝试在身份验证之后立即更新用户的属性。 Auth工作正常,我可以用它检索用户属性。 但问题是var cognitoUser=getUserPool().getCurrentUser()返回null。如何检索当前用户,以便在不刷新浏览器的情况下更新属性 var cognitoUser = getUserPool().getCurrentUser(); if (cognitoUser != null) { cognitoUser.getSession(function(err, s
var cognitoUser=getUserPool().getCurrentUser()代码>返回null。如何检索当前用户,以便在不刷新浏览器的情况下更新属性
var cognitoUser = getUserPool().getCurrentUser();
if (cognitoUser != null) {
cognitoUser.getSession(function(err, session) {
if ( err ) {
console.log(err);
return;
}else if( session.isValid() ){
updateUserAttribute( cognitoUser, 'custom:attr', attr )
}
});
}else{
console.log('Cognito User is null');
return;
}
也许另一个问题是,如何在不刷新浏览器的情况下使用accessToken在当前用户上运行函数
var cognitoUser = getUserPool().getCurrentUser();
if (cognitoUser != null) {
cognitoUser.getSession(function(err, session) {
if ( err ) {
console.log(err);
return;
}else if( session.isValid() ){
updateUserAttribute( cognitoUser, 'custom:attr', attr )
}
});
}else{
console.log('Cognito User is null');
return;
}
如果用户池已正确初始化,则应返回最后一个经过身份验证的用户。认证成功后,我们将在客户端id上键入的用户保存到本地存储。无论何时调用getCurrentUser,它都将从本地存储中检索最后一个经过身份验证的特定用户。令牌在该用户和客户端id上键入。在成功身份验证后,令牌也会保存到本地存储。访问访问令牌应仅限于:
cognitoUser.getSignInUserSession().getAccessToken().getJwtToken())
您可以将令牌直接用于CognitoIdentityServiceProvider客户端中公开的操作。这是一个相当老的问题,因此您现在可能已经开始了,但我认为您需要使用userpoolId和clientID创建用户池。我认为,如果没有已知的和/或内存中的可用对象,就不能直接调用getUserPool()。例如:
function makeUserPool () {
var poolData = {
UserPoolId : "your user pool id here",
ClientId : "you client id here"
};
return new AWSCognito.CognitoIdentityServiceProvider.CognitoUserPool(poolData);
}
var userPool = makeUserPool();
var currAWSUser = userPool.getCurrentUser();
getUserPool().getCurrentUser()在本地存储上查找用户,如果返回null是因为本地存储没有设置用户
为了在本地存储上设置用户,我使用了制作作业的CognitoAuth实例
此解决方案使用Cognito托管的UI。
在Cognito UI返回的回调url上:
import {CognitoAuth} from 'amazon-cognito-auth-js';
var authData = {
UserPoolId: 'us-east-1_xxxxxx',
ClientId: 'xxxxxxx',
RedirectUriSignIn : 'https://examole.com/login',
RedirectUriSignOut : 'https://example.com/logout',
AppWebDomain : 'example.com',
TokenScopesArray: ['email']
};
var auth = new CognitoAuth(authData);
auth.userhandler = {
onSuccess: function(result) {
//you can do something here
},
onFailure: function(err) {
// do somethig if fail
}
};
//get the current URL with the Hash that contain Cognito Tokens tokens
var curUrl = window.location.href;
//This parse the hash and set the user on the local storage.
auth.parseCognitoWebResponse(curUrl);
然后,如果调用getUserPool().getCurrentUser(),则该值不会为空。哇,我当时正使劲把头撞在墙上,想弄清楚这个值。您的解决方案非常有效。谢谢你这么简单的解释!通过SAML进行身份验证时,您的答案不适用。用户池是通过控制台一次性创建的,而不是代码。