Karthick Kumar
Karthick Kumar

Reputation: 1217

How to maintain session in React Native Apps?

Can anybody tell me how to maintain session for a user login. For example when the user sign-in to an application they have to be signed in unless the user logouts or uninstall the application.

Upvotes: 2

Views: 5999

Answers (2)

Purnima Naik
Purnima Naik

Reputation: 449

Use AsyncStorage.

Example:

For save:

AsyncStorage.multiSet([
    ["email", userInfo.email],
    ["password", userInfo.password]
])

For Delete:

let keys = ['email', 'password'];
AsyncStorage.multiRemove(keys, (err) => {
    console.log('Local storage user info removed!');
});

For Get:

AsyncStorage.multiGet(['email', 'password']).then((data) => {
    let email = data[0][1];
    let password = data[1][1];

    if (email !== null)
        //Your logic
});

Important - password should be encrypted

Upvotes: 4

abhimuralidharan
abhimuralidharan

Reputation: 5939

Normally,there will be a session duration maintained in the server.Like for example say 1 hour.So every time when the app launches,call the login api and create a session.When the user first login,save the email and password in NSUserDefaults and whenever the session expires,the next api call will return a session specific error code (say like for example:401 error),Then get the values from NSUserDefaults and login automatically.

Also clear the NSUserDefaults and all other user related values on logout.

Upvotes: 0

Related Questions