Renouvellement automatique du jeton d'authentification
This commit is contained in:
parent
363dfa5c74
commit
b0c17db233
@ -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)
|
||||
|
@ -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<AuthPayload>) => {
|
||||
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
|
||||
|
Loading…
Reference in New Issue
Block a user