Skip to content

Instantly share code, notes, and snippets.

@ericlewis
Last active November 28, 2023 20:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ericlewis/4d04bd1e9d30644b5ba20eae33518172 to your computer and use it in GitHub Desktop.
Save ericlewis/4d04bd1e9d30644b5ba20eae33518172 to your computer and use it in GitHub Desktop.
import * as SecureStore from 'expo-secure-store';
import AsyncStorage from '@react-native-async-storage/async-storage';
class EphemeralSecureStore {
static prefix = "EphemeralSecureStore";
static getEphemeralKey(key) {
return `${this.prefix}_${key}`;
}
static async setItemAsync(key, value, options) {
await SecureStore.setItemAsync(key, value, options);
await AsyncStorage.setItem(this.getEphemeralKey(key), 'exists');
}
static async getItemAsync(key, options) {
const value = await SecureStore.getItemAsync(key, options);
const asyncStoreValue = await AsyncStorage.getItem(this.getEphemeralKey(key));
// If the key does not exist in AsyncStorage, clear it from SecureStore
if (!asyncStoreValue) {
await SecureStore.deleteItemAsync(key);
return null;
}
return value;
}
static async deleteItemAsync(key, options) {
await SecureStore.deleteItemAsync(key, options);
await AsyncStorage.removeItem(this.getEphemeralKey(key));
}
}
export default EphemeralSecureStore;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment