feat: basic initial UI layout for mobile and desktop
This commit is contained in:
36
apps/web/src/context/AuthProvider.tsx
Normal file
36
apps/web/src/context/AuthProvider.tsx
Normal file
@@ -0,0 +1,36 @@
|
||||
import { createContext, useContext, useEffect, useState, type ReactNode } from 'react'
|
||||
import { authClient } from '../lib/auth-client'
|
||||
|
||||
type AuthSession = {
|
||||
user: (typeof authClient.$Infer.Session)['user']
|
||||
session: (typeof authClient.$Infer.Session)['session']
|
||||
}
|
||||
|
||||
type AuthContextValue = {
|
||||
user: AuthSession['user'] | null
|
||||
session: AuthSession['session'] | null
|
||||
isPending: boolean
|
||||
refresh: () => Promise<void>
|
||||
}
|
||||
|
||||
const AuthContext = createContext<AuthContextValue | null>(null)
|
||||
|
||||
export function AuthProvider({ children }: { children: ReactNode }) {
|
||||
const { data, isPending } = authClient.useSession()
|
||||
|
||||
const refresh = async () => {
|
||||
await authClient.getSession()
|
||||
}
|
||||
|
||||
return (
|
||||
<AuthContext.Provider value={{ user: data?.user ?? null, session: data?.session ?? null, isPending, refresh }}>
|
||||
{children}
|
||||
</AuthContext.Provider>
|
||||
)
|
||||
}
|
||||
|
||||
export function useAuth() {
|
||||
const ctx = useContext(AuthContext)
|
||||
if (!ctx) throw new Error('useAuth debe usarse dentro de <AuthProvider>')
|
||||
return ctx
|
||||
}
|
||||
Reference in New Issue
Block a user