I am trying to set up a session in my react native
I am new to mobile app development. I don’t know whether this is the correct approach to set a session
I am coming from react js background so I tried this approach but in the application tab in react native debugger I dont find the session token set can someone explain and help me whether this is a correct approach
axios({
method: 'POST',
url: 'http://127.0.0.1:8000/api/register',
data: Data56,
});
.then(function (response) {
storeData(response.data.token);
alert('sucess');
.catch(error => {
alert(JSON.stringify(error.response, 'catch'));
});
}
5
Answers
first install
Then
Then Call your session anywhere as like this
You can use @react-native-async-storage/async-storage library for storing your AUTH_TOKEN. Here is the code with explanation.
When user login your api would return a token that you can store the in the AysncStorage.
and with this function you can get your token.
Here you can use it like this.
After you set your token , on the start of the application get the token and store it the redux-store. of the token is null redirect user to login else redirect to Home Screen .
on API call use can use that token like this.
Here is the code where you can store a token and get that token back.
While handling tokens it’s better to use
react-native-keychain
. It is more secure than async storage and better for storing sensitive data.install and import keychain :
In your case try this approach :
If your tokens are not strings and you need to store an Object, just stringify them using
and retrieve the token anywhere using :
You can also reset a session easily using :
Here is simple way using the Async Storage it’s similar window.localStorage.
Install React Native Async Storage
Now make a api helper name with api.js
Now store the token into the Async Storage
After the storing a token in async storage next api will call with auth token