pinqy520 / mobx-persist

persist mobx stores
MIT License
560 stars 62 forks source link

Can mobx-persist be used with react-native-keychain or a similar storage container? #86

Open knakamura13 opened 4 years ago

knakamura13 commented 4 years ago

According to the docs, mobx-persist can only store data in AsyncStorage, localStorage, and LocalForge.

However, is there any way to make mobx-persist work with a different storage container, such as react-native-keychain, to allow for encrypted storage?

I'm looking for a secure method for persisting passwords, tokens, cookies, etc. on iOS and Android, while still using Mobx.

DjamshidDjurayev commented 4 years ago

Yes. You can create new class secureStorage, implement functions (getItem, setItem, removeItem) and pass it instead of async storage

I'm using react-native-sensitive-info, you can use whatever you want

import SInfo from 'react-native-sensitive-info';

export const setItem = (key: string, value: string) => {
  return new Promise(async (resolve, reject) => {
    try {
      await SInfo.setItem(key, value, options);
      resolve(null);
    } catch (e) {
      reject(e);
    }
  });
};

export const getItem = (key: string) => {
  return new Promise(async (resolve, reject) => {
    try {
      const result = await SInfo.getItem(key, options);
      resolve(result);
    } catch (e) {
      reject(e);
    }
  });
};

export function removeItem(key: string) {
  return new Promise(async (resolve, reject) => {
    try {
      await SInfo.deleteItem(key, options);
      resolve(null);
    } catch (err) {
      reject(err);
    }
  });
}

import * as SecureStorage from 'your-secure-storage';

const hydrate = create({
  storage: SecureStorage,
  jsonify: true,
});