0

I am trying to store the logged in user as a session in react native. Here is the code where I check for the login credential:

login = () => {
    // code to retrieve from firebase
    query.once( 'value', data => {
        data.forEach(userSnapshot => {
          let userKey = userSnapshot.key;
          var accountData = userSnapshot.val();
          var password = accountData.password;

          if(passwordInput != password){
            console.log('invalid credential.');
          }else{
            console.log('successful logged in.');
            // store the userKey as session to be used in other pages
          }
        });
    });
  }

I managed to grab the unique push ID for user if successfully logged in. Any idea on how to store it as a session?

Thanks in advance

1 Answer 1

1

You can use AsyncStorage to store your data. Here is some example

Persisting data

try {
  await AsyncStorage.setItem('Key', 'I like to save it.');
} catch (error) {
  // Error saving data
}

Fetching data:

try {
  const value = await AsyncStorage.getItem('Key');
  if (value !== null){
    // We have data!!
    console.log(value);
  }
} catch (error) {
  // Error retrieving data
}

Remove Data

try {
  AsyncStorage.removeItem('Key');
} catch (error) {
  // Error retrieving data
}

Clear All Data

try {
 AsyncStorage.clear();
} catch (error) {
 // Error retrieving data
}
Sign up to request clarification or add additional context in comments.

5 Comments

@JeffereyRajan But the error message told me that await is a reserved word.
if you want to use await functionality then you have to use async before starting the function example: async function fetchJSONAsync(url) { let response = await fetch(url); let body = await response.json(); return body; }
So in my case, login = async() => {} ? if I don't put the await, the code can be compiled perfectly, just that when I tried to getItem(), it returns me [object Object]
yes like this. const login = asyn () => { try { await AsyncStorage.setItem('Key', 'I like to save it.'); } catch (error) { // Error saving data } }
I see but how do I retrieve it? Because the getItem[] is currently returning me [object Object] rather than its value

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.