fix(mobile): prevent supabaseUrl crash on startup with fallback auth config

This commit is contained in:
Yun Chan 2026-08-20 23:29:10 +09:00
parent 9b24bbc576
commit a9c9a1ca6e
4 changed files with 35 additions and 17 deletions

View file

@ -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)
}