mirror of
https://github.com/StanGirard/quivr.git
synced 2024-12-11 11:43:54 +03:00
327074c5d4
* feat(auth): backend authentification verification * feat(auth): added to all endpoints * feat(auth): added to all endpoints * feat(auth): redirect if not connected * chore(print): removed * feat(login): redirect * feat(icon): added * chore(yarn): removed lock * chore(gitignore): removed
57 lines
1.2 KiB
TypeScript
57 lines
1.2 KiB
TypeScript
'use client'
|
|
|
|
import { createContext, useContext, useEffect, useState } from 'react'
|
|
import { Session, createBrowserSupabaseClient } from '@supabase/auth-helpers-nextjs'
|
|
import { useRouter } from 'next/navigation'
|
|
|
|
import type { SupabaseClient } from '@supabase/auth-helpers-nextjs'
|
|
|
|
|
|
type MaybeSession = Session | null
|
|
|
|
type SupabaseContext = {
|
|
supabase: SupabaseClient
|
|
session: MaybeSession
|
|
}
|
|
|
|
const Context = createContext<SupabaseContext | undefined>(undefined)
|
|
|
|
export default function SupabaseProvider({
|
|
children,
|
|
session,
|
|
}: {
|
|
children: React.ReactNode
|
|
session: MaybeSession
|
|
}) {
|
|
const [supabase] = useState(() => createBrowserSupabaseClient())
|
|
const router = useRouter()
|
|
|
|
useEffect(() => {
|
|
const {
|
|
data: { subscription },
|
|
} = supabase.auth.onAuthStateChange(() => {
|
|
router.refresh()
|
|
})
|
|
|
|
return () => {
|
|
subscription.unsubscribe()
|
|
}
|
|
}, [router, supabase])
|
|
|
|
return (
|
|
<Context.Provider value={{ supabase, session }}>
|
|
<>{children}</>
|
|
</Context.Provider>
|
|
)
|
|
}
|
|
|
|
export const useSupabase = () => {
|
|
const context = useContext(Context)
|
|
|
|
if (context === undefined) {
|
|
throw new Error('useSupabase must be used inside SupabaseProvider')
|
|
}
|
|
|
|
return context
|
|
}
|