Stockage du jeton d'authentification dans le store local, permettant l'utilisation de hooks
This commit is contained in:
25
client/components/LoginProvider.tsx
Normal file
25
client/components/LoginProvider.tsx
Normal file
@ -0,0 +1,25 @@
|
||||
import { useAuth } from "@/hooks/useAuth"
|
||||
import { Href, useRouter } from "expo-router"
|
||||
import { useRouteInfo } from "expo-router/build/hooks"
|
||||
import { ReactNode, useEffect } from "react"
|
||||
|
||||
type Props = {
|
||||
loginRedirect: Href
|
||||
children: ReactNode
|
||||
}
|
||||
|
||||
export default function LoginProvider({ loginRedirect, children }: Props) {
|
||||
const router = useRouter()
|
||||
const route = useRouteInfo()
|
||||
|
||||
// Si on est pas connecté⋅e, on reste sur la fenêtre de connexion
|
||||
const auth = useAuth()
|
||||
useEffect(() => {
|
||||
if (!auth.loggedIn && route.pathname !== loginRedirect)
|
||||
router.navigate(loginRedirect)
|
||||
}, [auth, route, router])
|
||||
|
||||
return <>
|
||||
{children}
|
||||
</>
|
||||
}
|
Reference in New Issue
Block a user