2017-08-19 2 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:登録確認後の自動ログイン

、ドキュメントは、ユーザーが現在サインインする準備ができていると言います。それはこの時点で自動的にユーザーに署名することは可能ですか?例えばのために

、私は以下を使用するときに確認した後、私はnullを取得:。

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); 
     }, 

    }); 
}); 
関連する問題