fix(mobile): prevent supabaseUrl crash on startup with fallback auth config
This commit is contained in:
parent
9b24bbc576
commit
a9c9a1ca6e
4 changed files with 35 additions and 17 deletions
|
|
@ -2,7 +2,7 @@
|
|||
import { createContext, useContext, useEffect, useState, useCallback } from 'react'
|
||||
import type { ReactNode } from 'react'
|
||||
import type { User, Session } from '@supabase/supabase-js'
|
||||
import { supabase } from './supabase'
|
||||
import { supabase, isSupabaseConfigured } from './supabase'
|
||||
|
||||
interface AuthContextValue {
|
||||
user: User | null
|
||||
|
|
@ -14,28 +14,41 @@ interface AuthContextValue {
|
|||
const AuthContext = createContext<AuthContextValue>({
|
||||
user: null,
|
||||
session: null,
|
||||
loading: true,
|
||||
loading: false,
|
||||
devBypass: () => {},
|
||||
})
|
||||
|
||||
export function AuthProvider({ children }: { children: ReactNode }): React.ReactElement {
|
||||
const [user, setUser] = useState<User | null>(null)
|
||||
const [session, setSession] = useState<Session | null>(null)
|
||||
const [loading, setLoading] = useState(true)
|
||||
const [loading, setLoading] = useState(false)
|
||||
|
||||
useEffect(() => {
|
||||
supabase.auth.getSession().then(({ data: { session: s } }) => {
|
||||
setSession(s)
|
||||
setUser(s?.user ?? null)
|
||||
if (!isSupabaseConfigured()) {
|
||||
setLoading(false)
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
const { data: { subscription } } = supabase.auth.onAuthStateChange((_event, s) => {
|
||||
setSession(s)
|
||||
setUser(s?.user ?? null)
|
||||
})
|
||||
try {
|
||||
supabase.auth.getSession()
|
||||
.then(({ data: { session: s } }) => {
|
||||
setSession(s)
|
||||
setUser(s?.user ?? null)
|
||||
setLoading(false)
|
||||
})
|
||||
.catch(() => {
|
||||
setLoading(false)
|
||||
})
|
||||
|
||||
return () => subscription.unsubscribe()
|
||||
const { data: { subscription } } = supabase.auth.onAuthStateChange((_event, s) => {
|
||||
setSession(s)
|
||||
setUser(s?.user ?? null)
|
||||
})
|
||||
|
||||
return () => subscription?.unsubscribe?.()
|
||||
} catch {
|
||||
setLoading(false)
|
||||
}
|
||||
}, [])
|
||||
|
||||
const devBypass = useCallback(() => {
|
||||
|
|
@ -53,3 +66,4 @@ export function AuthProvider({ children }: { children: ReactNode }): React.React
|
|||
export function useAuth(): AuthContextValue {
|
||||
return useContext(AuthContext)
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -3,13 +3,16 @@ import 'react-native-url-polyfill/auto'
|
|||
import AsyncStorage from '@react-native-async-storage/async-storage'
|
||||
import { createClient, type SupabaseClient } from '@supabase/supabase-js'
|
||||
|
||||
// TODO: move to env config or supabase-config.ts
|
||||
// Fallback dummy credentials to prevent createClient crash when unconfigured
|
||||
const FALLBACK_URL = 'https://auth.d3ro.chanpaca.net'
|
||||
const FALLBACK_ANON_KEY = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.dummy_anon_key'
|
||||
|
||||
const SUPABASE_URL = ''
|
||||
const SUPABASE_ANON_KEY = ''
|
||||
|
||||
export const supabase: SupabaseClient = createClient(
|
||||
SUPABASE_URL,
|
||||
SUPABASE_ANON_KEY,
|
||||
SUPABASE_URL || FALLBACK_URL,
|
||||
SUPABASE_ANON_KEY || FALLBACK_ANON_KEY,
|
||||
{
|
||||
auth: {
|
||||
storage: AsyncStorage,
|
||||
|
|
@ -23,3 +26,4 @@ export const supabase: SupabaseClient = createClient(
|
|||
export function isSupabaseConfigured(): boolean {
|
||||
return SUPABASE_URL.length > 0 && SUPABASE_ANON_KEY.length > 0
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue