34 lines
856 B
TypeScript
34 lines
856 B
TypeScript
import { createContext, useContext, useMemo, type ReactNode } from "react"
|
|
|
|
type ConvexAuthSessionContextValue = {
|
|
invalidateSession: () => Promise<void>
|
|
}
|
|
|
|
const ConvexAuthSessionContext =
|
|
createContext<ConvexAuthSessionContextValue | null>(null)
|
|
|
|
export function ConvexAuthSessionProvider({
|
|
children,
|
|
invalidateSession,
|
|
}: {
|
|
children: ReactNode
|
|
invalidateSession: () => Promise<void>
|
|
}) {
|
|
const value = useMemo(() => ({ invalidateSession }), [invalidateSession])
|
|
return (
|
|
<ConvexAuthSessionContext.Provider value={value}>
|
|
{children}
|
|
</ConvexAuthSessionContext.Provider>
|
|
)
|
|
}
|
|
|
|
export function useInvalidateAuthSession() {
|
|
const context = useContext(ConvexAuthSessionContext)
|
|
if (!context) {
|
|
throw new Error(
|
|
"useInvalidateAuthSession must be used within ConvexAuthSessionProvider"
|
|
)
|
|
}
|
|
return context
|
|
}
|