Files
otunes/apps/mobile/lib/auth-storage.ts
T
joshuafhigginsandClaude Opus 5.5 ffafbdc7ad feat(mobile): keep downloads going in the background
iOS songs download through a background URL session, and on iOS 26+ a
continued processing task keeps the app running with a Live Activity.
Android keeps the app alive with a dataSync foreground service while songs
are queued.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-20 10:25:11 -05:00

70 lines
1.9 KiB
TypeScript

import * as SecureStore from "expo-secure-store"
import type { TokenStorage } from "@convex-dev/auth/react"
const AUTH_STORAGE_KEYS = [
"__convexAuthOAuthVerifier",
"__convexAuthJWT",
"__convexAuthRefreshToken",
"__convexAuthServerStateFetchTime",
] as const
const secureStoreOptions: SecureStore.SecureStoreOptions = {
keychainAccessible: SecureStore.AFTER_FIRST_UNLOCK,
}
// The last value read or written per key, for when the keychain refuses a
// read (say, while the phone is locked and downloads continue).
const remembered = new Map<string, string>()
export async function secureGetItem(key: string): Promise<string | null> {
try {
const value = await SecureStore.getItemAsync(key, secureStoreOptions)
if (value === null) remembered.delete(key)
else remembered.set(key, value)
return value
} catch {
return remembered.get(key) ?? null
}
}
export async function secureSetItem(key: string, value: string): Promise<void> {
remembered.set(key, value)
await SecureStore.setItemAsync(key, value, secureStoreOptions)
}
export async function secureRemoveItem(key: string): Promise<void> {
remembered.delete(key)
try {
await SecureStore.deleteItemAsync(key, secureStoreOptions)
} catch {
// Item may already be gone.
}
}
export function authStorageKey(key: string, storageNamespace: string): string {
const escapedNamespace = storageNamespace.replace(/[^a-zA-Z0-9]/g, "")
return `${key}_${escapedNamespace}`
}
export async function clearStoredAuthTokens(
storageNamespace: string
): Promise<void> {
await Promise.all(
AUTH_STORAGE_KEYS.map((key) =>
secureRemoveItem(authStorageKey(key, storageNamespace))
)
)
}
export const secureStorage: TokenStorage = {
getItem(key) {
return secureGetItem(key)
},
setItem(key, value) {
return secureSetItem(key, value)
},
removeItem(key) {
return secureRemoveItem(key)
},
}