Skip to content

Instantly share code, notes, and snippets.

@patrickkempff
Created June 18, 2018 16:32
Show Gist options
  • Save patrickkempff/86f44f017568a973f5c041b205e4365c to your computer and use it in GitHub Desktop.
Save patrickkempff/86f44f017568a973f5c041b205e4365c to your computer and use it in GitHub Desktop.
redux-persist react-native-keychain
import { AsyncStorage } from 'react-native'
import { setGenericPassword, getGenericPassword, resetGenericPassword } from 'react-native-keychain'
export default {
async getAllKeys (callback) {
try {
const keys = await AsyncStorage.getAllKeys()
if (callback) {
callback(null, keys)
}
return keys
} catch (error) {
callback(error)
throw error
}
},
async getItem (key, callback) {
try {
const { password } = await getGenericPassword(key) || null
if (callback) {
callback(null, password)
}
return password
} catch (error) {
if (callback) {
callback(error)
}
throw error
}
},
async setItem (key, value, callback) {
try {
await Promise.all([
AsyncStorage.setItem(key, key),
setGenericPassword('user', value, key)
])
await setGenericPassword('user', value, key)
if (callback) {
callback(null)
}
} catch (error) {
if (callback) {
callback(error)
}
throw error
}
},
async removeItem (key, callback) {
try {
await Promise.all([
AsyncStorage.removeItem(key),
resetGenericPassword(key)
])
if (callback) {
callback(null)
}
} catch (error) {
if (callback) {
callback(error)
}
throw error
}
}
}
usage:
storeConfig: {
// ...
storage: KeychainStorage,
// ...
}
@telmen
Copy link

telmen commented Apr 13, 2020

Does it affect the read speed?

@jonathanpalma
Copy link

Thank you @patrickkempff, this is great, my only concern is that this gist might be outdated by this time due to some changes on the lib's api. For those looking for an updated version of this implementation, you can have a look at this

cc: @abbasmoosavi @telmen

@deyanskiq
Copy link

Has anybody tested it on Android? On iOS is working fine.. on Android, when cleaning the app and open it again it can't rehydrate correctly. @jonathanpalma

@jonathanpalma
Copy link

@deyanskiq is there any error logs that you can provide?

@deyanskiq
Copy link

@jonathanpalma There are no errors in the logs. The bug is on Android 9 or older versions when trying to rehydrate data with size ~200kB

I've checked that other people are having this issue as well:
oblador/react-native-keychain#184
oblador/react-native-keychain#208

Have you hit this so far?

@jonathanpalma
Copy link

@deyanskiq got it! No, sorry I have never had this issue before. However, I would suggest you use redux-persist whitelist/blacklist or nested persist in order to persist only the sensitive information inside the Keychain (just in case you haven't tried this already). I hope I could help. Please let me know if you find a solution

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment