React Native - : How do I delete asyncstore when the application is closed.
Or What other method can I log in?
UseState in App.js is safe ?
AsyncStorage is an unencrypted, asynchronous, persistent, key-value storage system that is global to the app. It should be used instead of LocalStorage.
If you don't wan't to persist the user data, probably you don't even need to use the AsyncStorage.
Normally AsyncStorage is used to store the user token and other info from the login information to keep the user logged in (unless they logout manually). If you don't want this behavior of a persistent storage, you can keep the usertoken in a local state of the application. This way, once you close the application, the local state will reset and the token will also get deleted, when the user access your application, they will have to login every time to use the app.
If this is something that you are looking for, you can look for maybe the react-context apis or redux reducers to store the login info. If you want to go with a persistent storage option, you can opt for AsyncStorage.
You can save data to AsyncStorage with
AsyncStorage.setItem('#storage_Key', value)
And delete the data with
AsyncStorage.removeItem('#storage_Key', value)
You can read more about the AsyncStorage at https://react-native-async-storage.github.io/async-storage/docs/usage
Or you can choose react-native-keychain or react-native-sensitive-info which is more secure than using the AsyncStorage option.
Related
I am currently building an app using the following
Next.js
Express API
Redux-Toolkit & RTK Query
I have all of the authentication logic implemented, but have ran into an issue.
So, upon successful login, the express api returns 2 httponly cookies containing the access/refresh tokens.
I have an endpoint in my api to get the current user using the access token i.e /api/auth/me
This all works fine, but what I can't figure out is the best way to fetch the user on each page load and store them in the redux state.
Do I use RTK Query to hit the /api/auth/me endpoint and just call the query whenever I need it throughout the app?
Ideally I fetch and set the user in _app.tsx, but I cannot use redux dispatch since it's outside of the <Provider store={store}></Provider>
Also for example if I wanted to use the redux stored user in getServerSideProps I can't seem to do that either because it's not client side and doesn't have access to redux.
I just can't seem to find a good way to simply set the authenticated user and be able to use them globally throughout the app whether it's inside redux store or in something like getServerSideProps.
Any advice would be truly amazing, I am totally lost.
One way of persisting authentication state is storing the token in the local storage. And in the main App component you can check whether the token is present in the redux store. If it's not there you can set it manually in useEffect function.
As such:
useEffect(() => {
fetchAuthTokenFromLocalStorage().then((token) => {
dispatch(setAuthenticationResult(token));
});},[dispatch]);
I am making an expo application that has some user authentication features and I am trying to find a way to store the access token persistently. I have come across some library such as Asyncstorage and expo-secure-store. But I have some concerned about choosing between each of these two.
Asyncstorage --> I understand that it is an unencrypted, persistent data so I believe it means that if I want to store the token here securely, I should encrypt the token with some encryption method before store it into the storage.
expo-secure-store --> I take it that it stores that data in an encrypted way which make it more secure than bare Asyncstorage.
However, in my application, I also planned to use Redux for state management and I thinks that Asyncstorage might work with it. Based on this, should I use Asyncstorage to store my tokens or should I use both of them for different purposes or do you guys have any idea on how this will works out? Thanks!
I am trying to check the authentication status of a user when he opens my react-native application. I have used redux-persist to get my react store to persist on the device after my application has closed. My question is do I need to do this if I am going to use firebase authentication ? What are the 'rules' with regard to using firebase and redux-persist together ? How to make sure they work together properly ?
See the whole point of redux-persist is to store the redux state even when the app is closed via either AsyncStorage/sql-lite-storage.
I believe previously you used to store your JWT token and check if its valid and show the user the respective screens.
Now since FirebaseAuth doesnt have any such flow, you can omit the loginReducer in redux persist configuration.
Like this :
const persistConfig = {
key: 'root',
storage: storage,
blacklist: ['navigation'] // navigation will not be persisted
};
But there may be some other places you use the redux store , like suppose i use redux persist to store the trip data of user, so it doesnt fetches every time. So just check out whats your purpose.
Hope it helps. feel free for doubts.
I'm figuring out the data-flow, and writing action oriented code to deal with Redux.
This is the flow:
I reach SplashScreen and check if accessToken exists. If no, I send the user to LoginScreen. Else, I take him to the HomeScreen.
The flow is rather simple but I'm not able to wrap my head around how to store the token, expire the token or just check if user is logged in. So, technically, when a user logs in, the state(access token) should change. I'm not asking for code, just some pseudo-code and a little explanation will help!
If you want to user the redux store to save token or any user credentials you will need to ensure the state is saved on localStorage, and for that you can use a redux middleware like this or this, or you can use middle-ware of your own :
export default store => next => action => {
let result = next(action)
localStorage.state = JSON.stringify(store.getState())
return result
}
I don't know if you are using react router, If you are, take a look at this example: authenticator I think it's really the best way to do it..
In my app I used react-native-keychain. When a user logs in, I store the accessToken in the redux store. From there, any time I'm making a request, I just get the token from the store, ensure that it's valid, and make the request.
Can anyone explain how asyncStorage works ? Is it connected to database like SQLite or RocksDB ? Does asyncstorage write data to database directly ?
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's use full when you want to use some variable globally in App.
e.g like when you login first time in app the store the login id and password for next time it will take data from AsyncStorage and logged in successfully..
Refer this link