2017-08-19 41 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); 
     }, 

    }); 
});