How do I remember and authorize a login token when using Amazon Web Services Cognito Userpool?

I created a website that uses AWS Cognito with Userpool functionality.

If I leave the page, the login will be forgotten, and expire in an hour.

I would like the login to be remembered when the user closes his browser and returns.

I would also like the auth token to be updated automatically, and not just give errors after one hour.

How can i do this?

+5
source share
1 answer

User pool icons are stored in local storage. And when you call getCurrentUser in the user pool object, you can get the last authenticated user object. After that, the getSession call must use the update token to receive new access tokens if they have expired, for example, in the example below. In the callback for getSession, you must have a valid session.

var poolData = { UserPoolId : '...', // Your user pool id here ClientId : '...' // Your client id here }; var userPool = new AWSCognito.CognitoIdentityServiceProvider.CognitoUserPool(poolData); var cognitoUser = userPool.getCurrentUser(); if (cognitoUser != null) { cognitoUser.getSession(function(err, session) { if (err) { alert(err); return; } console.log('session validity: ' + session.isValid()); //You should have a valid session here }); } 
+5
source

Source: https://habr.com/ru/post/1263733/


All Articles