Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AWS cognito: Auto login after registration confirmation

I am using the JavaScript SDK of AWS Cognito (http://docs.aws.amazon.com/cognito/latest/developerguide/using-amazon-cognito-user-identity-pools-javascript-examples.html).

When a new user completes registration confirmation, the documentation says the user is now ready to sign in. Is it possible to automatically sign in the user at this time?

For eg., after confirmation when I use the following I get null:

userPool.getCurrentUser(); 

If this is the intended behavior, are there any ways to sign in the user without explicitly asking the user again?

I know this is not a good idea, one thing I can think of is to save the user credentials in local storage and use them after confirmation to automatically sign in. Any other ideas better than this?

like image 283
vrtx54234 Avatar asked Aug 19 '17 17:08

vrtx54234


People also ask

How do I change my confirmation status in Cognito?

In order to change a Cognito user's status from FORCE_CHANGE_PASSWORD to CONFIRMED , we have to change their password. To change a Cognito user's password, use the admin-set-password command, setting the --permanent parameter. Copied!

How do I set up authentication in Cognito?

Go to AWS Cognito service and click “Manage Identity Pools”. 2. Enter “Identity pool name”, expand the “Authentication providers” section and select “Cognito” tab. This is where the Cognito authentication provider will be registered with the Identity pool.

How do I get a confirmation code for Cognito?

The Amazon Cognito service receives the sign-up request from the app. After verifying that the request contains all attributes required for sign-up, the service completes the sign-up process and sends a confirmation code to the user's phone (in an SMS message) or email. The code is valid for 24 hours.

How long does Cognito session last?

By default, Amazon Cognito refresh tokens expire 30 days after a user signs in to a user pool. When you create an app, you can set the app's refresh token expiration to any value between 60 minutes and 10 years.


1 Answers

Upon user signup, your backend will be receiving uses credentials, which you can use to generate the JWT token. Then you can add the JWT token in the same response, which can be use by the browser client to request authorized endpoints.

Example:

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

     });
 });
like image 104
Ashan Avatar answered Oct 06 '22 00:10

Ashan