import { AsyncStorage } from 'react-native' import { getGenericPassword, resetGenericPassword, setGenericPassword } from 'react-native-keychain' const noop = () => null export default ({ service = 'safestorage' }) => { return { prependService (key) { return `${service}:${key}` }, async getItem (key, callback = noop) { try { const serviceKey = this.prependService(key) const entry = await getGenericPassword(serviceKey) if (entry) { const { password } = entry callback(null, password) return password } return null } catch (e) { callback(e) throw e } }, async setItem (key, value, callback = noop) { try { const serviceKey = this.prependService(key) await [ AsyncStorage.setItem(serviceKey, '1'), setGenericPassword(key, value, serviceKey) ] callback(null) } catch (e) { callback(e) throw e } }, async removeItem (key, callback = noop) { try { const serviceKey = this.prependService(key) await [ AsyncStorage.removeItem(serviceKey), resetGenericPassword(serviceKey) ] callback(null) } catch (e) { callback(e) throw e } }, async getAllKeys (callback = noop) { try { const keys = await AsyncStorage.getAllKeys() callback(null, keys) return keys } catch (e) { callback(e) throw e } } } }
Leave a Reply