Last active
November 28, 2023 20:25
-
-
Save ericlewis/4d04bd1e9d30644b5ba20eae33518172 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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