react-nativeautologin

Does react native has auto signin concept?


I am newbie to react native. I have created login, home and logout screens. If i close my app without logging out and open my app again then it should redirect to home screen from login without asking the user to enter the login credentials.

Can i store a global variable with login details and a flag and delete them when user clicks on logout? or is there any other way to do it?


Solution

  • You can use AsyncStorage. AsyncStorage is a simple, unencrypted, asynchronous, persistent, key-value storage system that is global to the app. It should be used instead of LocalStorage.

    It is recommended that you use an abstraction on top of AsyncStorage instead of AsyncStorage directly for anything more than light usage since it operates globally.

    To store data:

    await AsyncStorage.setItem('@MySuperStore:key', 'I like to save it.');
    

    To Fetch data:

    const value = await AsyncStorage.getItem('@MySuperStore:key');
      if (value !== null){
        // We have data!!
        console.log(value);
      }
    

    To remove data:

    AsyncStorage.clear();
    

    After getting response from login API, store userid in AsyncStorage and on logout click you can clear AsyncStorage.