Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to persist Cognito User Session

I am using javascript sdk for AWS cognito and able to login with aws cognito and receiving tokens in response.

I can see that the user session is valid until I refresh the page. Please suggest how the user session can persist after refreshing the page.

Below is my code.

function getSession() {
let poolData = {
    UserPoolId: _config.cognito.userPoolId, // Your user pool id here
    ClientId: _config.cognito.clientId, // Your client id here
};

//alert(sessionStorage.getItem("SessionName"));


let userPool = new AmazonCognitoIdentity.CognitoUserPool(poolData);
cognitoUser = userPool.getCurrentUser();
cognitoUser.getSession(function (err, session) {
    if (err) {
        alert(err);
        return;
    }
    console.log('session validity: ' + session.isValid());
    //Set the profile info
    cognitoUser.getUserAttributes(function (err, result) {
        if (err) {
            console.log(err);
            return;
        }
        console.log("------>>" + result);
        //document.getElementById("email_value").innerHTML = result[2].getValue();  
    });

});

}

like image 271
Amit Pande Avatar asked Mar 09 '19 13:03

Amit Pande


People also ask

How long is a Cognito session valid?

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.

How do I refresh my Cognito access token?

Initiate new refresh tokens (API) Pass REFRESH_TOKEN_AUTH for the AuthFlow parameter. The authorization parameter, AuthParameters , is a key-value map where the key is "REFRESH_TOKEN" and the value is the actual refresh token. Amazon Cognito returns new ID and access tokens after your API request passes all challenges.

How long do Cognito access tokens last?

Access tokens can be configured to expire in as little as five minutes or as long as 24 hours. Refresh tokens can be configured to expire in as little as one hour or as long as ten years. These customizations enable Amazon Cognito customers to balance the security and usability of each application they develop.

What is a Cognito session?

When a user signs into your app, Amazon Cognito verifies the login information. If the login is successful, Amazon Cognito creates a session and returns an ID, access, and refresh token for the authenticated user.


1 Answers

good news - the SDK does this for you. Check out their code for the getsession method

You can see they store the tokens to local storage for you.

To view the tokens from Google Chrome, go to developer tools -> Application. You should see a 'Storage' section on the left hand side. Open Local Storage, the tokens are saved under the URL of the application.

You should not need to access these token directly, the SDK will fetch and save the tokens as required when you call different methods.

like image 99
F_SO_K Avatar answered Oct 18 '22 21:10

F_SO_K