Updated local auth state storage

This commit is contained in:
Christopher Sanden
2026-03-11 17:09:34 +01:00
parent 05a9b4f023
commit 86432d9281

View File

@@ -1,53 +1,26 @@
import '@react-native-async-storage/async-storage'
import { createClient } from '@supabase/supabase-js' import { createClient } from '@supabase/supabase-js'
import AsyncStorage from '@react-native-async-storage/async-storage' import Constants from "expo-constants"
import { deleteItemAsync, getItemAsync, setItemAsync } from 'expo-secure-store' import { deleteItemAsync, getItemAsync, setItemAsync } from 'expo-secure-store'
import 'react-native-url-polyfill/auto'
import Constants from 'expo-constants'
import { Platform } from 'react-native' import { Platform } from 'react-native'
import 'react-native-url-polyfill/auto'
const REFRESH_TOKEN_STORAGE_KEY = 'supabase.auth.refresh-token' const ExpoSecureStoreAdapter = {
getItem: (key: string) => {
type PersistedSession = { return getItemAsync(key)
refresh_token?: string
}
const NativeSplitStorageAdapter = {
getItem: async (key: string) => {
return AsyncStorage.getItem(key)
}, },
setItem: (key: string, value: string) => {
setItem: async (key: string, value: string) => { if (value.length > 2048) {
await AsyncStorage.setItem(key, value) console.warn('Value being stored in SecureStore is larger than 2048 bytes and it may not be stored successfully. In a future SDK version, this call may throw an error.')
try {
const session = JSON.parse(value) as PersistedSession
if (session.refresh_token) {
await setItemAsync(REFRESH_TOKEN_STORAGE_KEY, session.refresh_token)
} else {
await deleteItemAsync(REFRESH_TOKEN_STORAGE_KEY)
}
} catch (error) {
console.warn('Stored Supabase session, but could not parse refresh token for SecureStore backup.', error)
} }
return setItemAsync(key, value)
}, },
removeItem: (key: string) => {
removeItem: async (key: string) => { return deleteItemAsync(key)
await AsyncStorage.removeItem(key)
await deleteItemAsync(REFRESH_TOKEN_STORAGE_KEY)
}, },
} }
export const hasSecureRefreshToken = async () => { const extra = (Constants.expoConfig?.extra ?? Constants.expoConfig?.extra) as {
if (Platform.OS === 'web') {
return false
}
const refreshToken = await getItemAsync(REFRESH_TOKEN_STORAGE_KEY)
return Boolean(refreshToken)
}
const extra = (Constants.expoConfig?.extra ?? Constants.manifest?.extra) as {
supabaseUrl?: string supabaseUrl?: string
supabaseKey?: string supabaseKey?: string
} }
@@ -55,17 +28,22 @@ const extra = (Constants.expoConfig?.extra ?? Constants.manifest?.extra) as {
const supabaseUrl = extra?.supabaseUrl const supabaseUrl = extra?.supabaseUrl
const supabaseAnonKey = extra?.supabaseKey const supabaseAnonKey = extra?.supabaseKey
if (!supabaseUrl || !supabaseAnonKey) { if(!supabaseUrl || !supabaseAnonKey){
throw new Error('Cannot read env variables') throw new Error("Cannot read env variables")
} }
const storage = Platform.OS === 'web' ? window.localStorage : NativeSplitStorageAdapter const storage = (
Platform.OS === "web"
? window.localStorage
: ExpoSecureStoreAdapter
)
export const supabase = createClient(supabaseUrl, supabaseAnonKey, { export const supabase = createClient(supabaseUrl, supabaseAnonKey, {
auth: { auth: {
storage: storage as any, storage: storage as any,
autoRefreshToken: true, autoRefreshToken: true,
persistSession: true, persistSession: true,
detectSessionInUrl: false, detectSessionInUrl: false,
}, },
}) }
)