Reputation: 928
I want to store e.g. the password and the username of the user locally, that the user doesn't always need to log in again.
I just saw the tutorial How to store data in local storage with react native? but is there no way, to store a json with data in e.g. a normal folder. What are the benefits or are the benefits? How does it look like when compiling the app? Could there be an error? :)
Example:
user {
"name":"max",
"password":"123456"
}
(I know storing passwords in plain text isn't that smart, it is just an example :D)
Upvotes: 1
Views: 1100
Reputation: 809
I suggest you to use AsyncStorage. It can store json data as well as normal data too locally.
If you're working on to add "Remember login" the use this logic - as soon as the user logins in the app, store the data in AsyncStorage with AsyncStorage.setItem()
DemoLogin.js
login = (
username,
password,
) => {
axios
.post(apiEndPoint + "login", {
username,
password,
})
.then(async (response) => {
if (response.data) {
if (response.data.success) {
this.storeData(response.data);
this.props.navigation.replace("HomeScreen");
} else {
toast.show(response.data.message);
}
} else {
toast.show("There is some issue with API");
}
})
.catch((err) => {
toast.show("There is some issue with API");
});
};
storeData = async (userData) => {
try {
await AsyncStorage.setItem("userdata", JSON.stringify(userData));
} catch (error) {
console.log("error while storing data", error);
}
};
Upvotes: 2
Reputation: 146
You can with this react-native-fs package. To storage the username and password is not a good solution like you know. AsyncStorage is usefull to store the data of user for the context of the application. Store file is good if is an image or a specific file like PDF, etc..., where is a lot of data to store. AsyncStorage or the other secure storage has a limit on the size data you can save.
Upvotes: 0