Skip to content

Instantly share code, notes, and snippets.

@jonathanpalma
Last active July 11, 2023 10:28
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save jonathanpalma/1a2953511a523d46d43c7551a55da39c to your computer and use it in GitHub Desktop.
Save jonathanpalma/1a2953511a523d46d43c7551a55da39c to your computer and use it in GitHub Desktop.
How to persis redux store using redux-persist and react-native-keychain

How to use it?

Please refer to the official redux-persist documentation and then when creating your persistConfig add the following lines:

const persistConfig = {
  // ...   
  storage: KeychainStorage,
  // ...
};
import AsyncStorage from '@react-native-community/async-storage';
import {
setGenericPassword,
getGenericPassword,
resetGenericPassword,
} from 'react-native-keychain';
const KeychainStorage = {
async getAllKeys(cb) {
try {
const keys = await AsyncStorage.getAllKeys();
if (cb) {
cb(undefined, keys);
}
return keys;
} catch (err) {
cb(err);
throw err;
}
},
async getItem(key, cb) {
try {
const sharedWebCredentials = await getGenericPassword({service: key});
// Check getGenericPassword documentation https://git.io/JffKK
if (typeof sharedWebCredentials === 'boolean') {
throw new Error('entry does not exist');
}
const {password} = sharedWebCredentials;
if (cb) {
cb(undefined, password);
}
return password;
} catch (err) {
if (cb) {
cb(err);
}
throw err;
}
},
async setItem(key, value, cb) {
try {
await Promise.all([
AsyncStorage.setItem(key, key),
setGenericPassword('user', value, {service: key}),
]);
await setGenericPassword('user', value, {service: key});
if (cb) {
cb(undefined);
}
} catch (err) {
if (cb) {
cb(err);
}
throw err;
}
},
async removeItem(key, cb) {
try {
await Promise.all([
AsyncStorage.removeItem(key),
resetGenericPassword({service: key}),
]);
if (cb) {
cb(undefined);
}
} catch (err) {
if (cb) {
cb(err);
}
throw err;
}
},
};
export default KeychainStorage;
import AsyncStorage from '@react-native-community/async-storage';
import {
setGenericPassword,
getGenericPassword,
resetGenericPassword,
} from 'react-native-keychain';
type Callback = (err?: Error, keys?: string | string[]) => void;
const KeychainStorage = {
async getAllKeys(cb: Callback) {
try {
const keys = await AsyncStorage.getAllKeys();
if (cb) {
cb(undefined, keys);
}
return keys;
} catch (err) {
cb(err);
throw err;
}
},
async getItem(key: string, cb: Callback) {
try {
const sharedWebCredentials = await getGenericPassword({service: key});
// Check getGenericPassword documentation https://git.io/JffKK
if (typeof sharedWebCredentials === 'boolean') {
throw new Error('entry does not exist');
}
const {password} = sharedWebCredentials;
if (cb) {
cb(undefined, password);
}
return password;
} catch (err) {
if (cb) {
cb(err);
}
throw err;
}
},
async setItem(key: string, value: string, cb: Callback) {
try {
await Promise.all([
AsyncStorage.setItem(key, key),
setGenericPassword('user', value, {service: key}),
]);
await setGenericPassword('user', value, {service: key});
if (cb) {
cb(undefined);
}
} catch (err) {
if (cb) {
cb(err);
}
throw err;
}
},
async removeItem(key: string, cb: Callback) {
try {
await Promise.all([
AsyncStorage.removeItem(key),
resetGenericPassword({service: key}),
]);
if (cb) {
cb(undefined);
}
} catch (err) {
if (cb) {
cb(err);
}
throw err;
}
},
};
export default KeychainStorage;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment