2017-08-19 60 views
1

我使用AWS Cognito的JavaScript的SDK(http://docs.aws.amazon.com/cognito/latest/developerguide/using-amazon-cognito-user-identity-pools-javascript-examples.html)后自动登录。AWS cognito:报名确认

当新用户完成报名确认,该文件说,用户现在可以登录。是否有可能在此时的用户自动登录?

对于如确认后,当我使用下面我得到空。

userPool.getCurrentUser(); 

如果这是预期的行为,还有在用户登录无需再次明确要求用户的任何方式?

我知道这是不是一个好主意,有一件事我能想到的是保存用户凭据在本地存储和使用它们确认后自动登录。任何其他的想法比这更好的?

回答

0

一旦用户注册,你的后台将收到的用途凭证,你可以用它来生成JWT令牌。然后,您可以在同一个响应中添加JWT令牌,浏览器客户端可以使用该令牌来请求授权的终端。

实施例:

AWSCognito.config.region = 'us-east-1'; //This is required to derive the endpoint 

var poolData = { 
    UserPoolId: 'us-east-1_TcoKGbf7n', 
    ClientId: '4pe2usejqcdmhi0a25jp4b5sh3' 
}; 
var userPool = new AWSCognito.CognitoIdentityServiceProvider.CognitoUserPool(poolData); 
var attributeList = []; 
var dataEmail = { 
    Name: 'email', 
    Value: '[email protected]' 
}; 
var authenticationData = { 
    Username: 'username', 
    Password: 'password', 
}; 
var attributeEmail = new AWSCognito.CognitoIdentityServiceProvider.CognitoUserAttribute(dataEmail); 
attributeList.push(attributeEmail); 

userPool.signUp(authenticationData.Username, authenticationData.Password, attributeList, null, function (err, result) { 
    if (err) { 
     alert(err); 
     return; 
    } 
    var authenticationDetails = new AWSCognito.CognitoIdentityServiceProvider.AuthenticationDetails(authenticationData); 
    var userData = { 
     Username: authenticationData.Username, 
     Pool: userPool 
    }; 
    var cognitoUser = new AWSCognito.CognitoIdentityServiceProvider.CognitoUser(userData); 
    cognitoUser.authenticateUser(authenticationDetails, { 
     onSuccess: function (result) { 
      console.log('access token + ' + result.getAccessToken().getJwtToken()); 
      /*Use the idToken for Logins Map when Federating User Pools with Cognito Identity or when passing through an Authorization Header to an API Gateway Authorizer*/ 
      console.log('idToken + ' + result.idToken.jwtToken); 
      /*Return the result.idToken.jwtToken with the response*/ 
     }, 
     onFailure: function (err) { 
      alert(err); 
     }, 

    }); 
});