From b0c17db233f989dd76865bb002f115a1bd3e0112 Mon Sep 17 00:00:00 2001 From: Emmy D'Anello Date: Tue, 10 Dec 2024 19:26:10 +0100 Subject: [PATCH] Renouvellement automatique du jeton d'authentification --- client/components/LoginProvider.tsx | 49 ++++++++++++++++++--- client/utils/features/location/authSlice.ts | 5 ++- 2 files changed, 47 insertions(+), 7 deletions(-) diff --git a/client/components/LoginProvider.tsx b/client/components/LoginProvider.tsx index 40fb3f9..6d23c65 100644 --- a/client/components/LoginProvider.tsx +++ b/client/components/LoginProvider.tsx @@ -1,7 +1,8 @@ -import { useAuth } from "@/hooks/useAuth" -import { Href, useRouter } from "expo-router" -import { useRouteInfo } from "expo-router/build/hooks" -import { ReactNode, useEffect } from "react" +import { Href, useRouter } from 'expo-router' +import { useRouteInfo } from 'expo-router/build/hooks' +import { ReactNode, useEffect } from 'react' +import { useAuth, useAuthLogin } from '@/hooks/useAuth' +import * as SecureStore from '@/utils/SecureStore' type Props = { loginRedirect: Href @@ -11,9 +12,47 @@ type Props = { export default function LoginProvider({ loginRedirect, children }: Props) { const router = useRouter() const route = useRouteInfo() + const auth = useAuth() + const authLogin = useAuthLogin() + + // Renouvellement auto du jeton d'authentification + useEffect(() => { + const { name, token } = auth + const password = SecureStore.getItem('apiPassword') + if (name === null || (password === null && token === null)) + return + let waitTime = 0 + if (token !== null && token !== undefined) { + const arrayToken = token.split('.') + const tokenPayload = JSON.parse(atob(arrayToken[1])) + const expTime: number = tokenPayload.exp * 1000 + const now: number = Math.floor(new Date().getTime()) + waitTime = expTime - now + } + const timeout = setTimeout(async () => { + const password = SecureStore.getItem('apiPassword') + if (password) { + await fetch(`${process.env.EXPO_PUBLIC_TRAINTRAPE_MOI_SERVER}/auth/login/`, { + method: "POST", + headers: { "Content-Type": "application/json" }, + body: JSON.stringify({ name: name, password: password }) + }) + .then(resp => resp.json()) + .then(resp => { + if (!resp.error) + authLogin({ name: name, token: resp.accessToken }) + else + authLogin({ name: name, token: null }) + }) + } + else { + authLogin({ name: name, token: null }) + } + }, waitTime) + return () => clearTimeout(timeout) + }, [auth]) // 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) diff --git a/client/utils/features/location/authSlice.ts b/client/utils/features/location/authSlice.ts index 9365c35..8f0028f 100644 --- a/client/utils/features/location/authSlice.ts +++ b/client/utils/features/location/authSlice.ts @@ -8,7 +8,7 @@ interface AuthState { } export interface AuthPayload { - name: string | null, + name: string, token: string | null, } @@ -23,9 +23,10 @@ export const authSlice = createSlice({ initialState: initialState, reducers: { login: (state, action: PayloadAction) => { - state.loggedIn = true + state.loggedIn = action.payload.token !== null state.name = action.payload.name state.token = action.payload.token + console.log(state) }, logout: (state) => { state.loggedIn = false