Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to store tokens in react native?

Tags:

react-native

I used to develop in android previously and i used to used SharePreference for storing user tokens. Is there anything such available in react native for both ios and android?

like image 737
PuskarShestha Avatar asked May 18 '18 05:05

PuskarShestha


People also ask

Where do you store tokens in React Native?

Keychain Services allows you to securely store small chunks of sensitive info for the user. This is an ideal place to store certificates, tokens, passwords, and any other sensitive information that doesn't belong in Async Storage.

How do I send a token in React Native?

const options = { url: URLs. addLeads, method: "POST", params: { token } }; This will add a token query parameter with a correctly URL-encoded value. You don't need to change the headers; the Axios defaults already have you covered.


1 Answers

If you're using create-react-native-app or exp, you can use Expo.SecureStore to store your token.

import {SecureStore} from 'expo';  await SecureStore.setItemAsync('secure_token','sahdkfjaskdflas$%^&'); const token = await SecureStore.getItemAsync('secure_token'); console.log(token); // output: sahdkfjaskdflas$%^& 

Details: https://docs.expo.io/versions/latest/sdk/securestore

Update in Dec 2020:

The SecureStore module is now become expo-secure-store

import * as SecureStore from 'expo-secure-store';  await SecureStore.setItemAsync('secure_token','sahdkfjaskdflas$%^&'); const token = await SecureStore.getItemAsync('secure_token'); console.log(token); // output: sahdkfjaskdflas$%^& 
like image 193
Calvin W Avatar answered Oct 14 '22 12:10

Calvin W