Amplify-js: How to persist the user session/ status?

Created on 9 Dec 2017  路  8Comments  路  Source: aws-amplify/amplify-js

It would be great if you could provide some informations how to persist the user session.

I am trying to avoid that users need to enter their credentials every time they start the app. After login, i store the userdata in redux and localstore. But how to validate the session and status?

question

Most helpful comment

Hello,

I'm having issues getting up and running with aws-amplify and I think this thread might be related to my issue.

I'm getting Local storage is missing an ID Token, Please authenticate whenever I try to sign in and get the user details or trying to reset the password. I'm working on a Vue app and It seems that the storage helper is either not saving the session key whilst doing the sign in or the key does not persist. Is there something I'm missing?

I've downloaded the vue sample and can reproduce the error using my aws-export config file

All 8 comments

@digitaledge if you use the Auth component the session is managed for you. It varies based on what your doing, but if you use the HOCs with React/React Native then you don't need to do anything outside of wrapping your App with the HOC. The credentials are cached / stored for you and there are methods on the Auth component to retrieve the session and current user, see here for details:
https://github.com/aws/aws-amplify/blob/master/media/authentication_guide.md

Let me know if this clears it up or if you have any other questions here.

Sounds great, thanks!
How long does it take until the session expires?

Even i wish there would be a better way to pass in custom components for the auth screens.
Perfect would be something like:

const myScreens = {SignIn, Signup, ConfirmSignUp, Greetings, AlwaysOn};
withAuth({...myScreens})(myApp) or as decorator @withAuth({...myScreens})

Using the HOC will persist the user to the HOC state which does not persist between sessions. I have tried digging around a little to figure out how to persist the user across multiple sessions and have come up short. I found that Auth.currentUser calls getCurrentUser on the class CognitoUserPool from amazon-cognito-identity-js https://github.com/aws/aws-amplify/blob/master/packages/aws-amplify-react-native/src/Auth/Auth.js#L326 . Digging a little further I noticed CognitoUserPool uses an in memory Storage solution for react native since there is no local storage available https://github.com/aws/amazon-cognito-identity-js/blob/master/src/CognitoUserPool.js#L61 https://github.com/aws/amazon-cognito-identity-js/blob/master/src/StorageHelper.js#L76 .

@mlabieniec

When i use the HOC like explained in the docs, the session is gone every time i restart the app.
I think it should somehow load the data from AsyncStorage, to avoid that users have to enter their login data every time.

Is this actually in scope of this library?

You can persist session similar to this approach .
Basically we store session in both memory and AsyncStorage, then when app start rehydrate session from async storage. There is some concern about which session should be stored. In my case I only persist session when user signIn. I already made a PR for it. Meanwhiles, you can clone this packages to local npm, enable storage configuration for your enhancement and try if this approach work for you.

Hello,

I'm having issues getting up and running with aws-amplify and I think this thread might be related to my issue.

I'm getting Local storage is missing an ID Token, Please authenticate whenever I try to sign in and get the user details or trying to reset the password. I'm working on a Vue app and It seems that the storage helper is either not saving the session key whilst doing the sign in or the key does not persist. Is there something I'm missing?

I've downloaded the vue sample and can reproduce the error using my aws-export config file

@mlabieniec the link you've shared is gone, is there any other link explaining how we can storage the current authenticated user session rather than call the API every time we need it?

Was this page helpful?
0 / 5 - 0 ratings