Utilisation de mutations plutôt que d'appels fetch directs
This commit is contained in:
@ -3,6 +3,7 @@ import { useRouteInfo } from 'expo-router/build/hooks'
|
||||
import { ReactNode, useEffect } from 'react'
|
||||
import { useAuth, useAuthLogin } from '@/hooks/useAuth'
|
||||
import * as SecureStore from '@/utils/SecureStore'
|
||||
import { useLoginMutation } from '@/hooks/mutations/useLoginMutation'
|
||||
|
||||
type Props = {
|
||||
loginRedirect: Href
|
||||
@ -14,6 +15,15 @@ export default function LoginProvider({ loginRedirect, children }: Props) {
|
||||
const route = useRouteInfo()
|
||||
const auth = useAuth()
|
||||
const authLogin = useAuthLogin()
|
||||
const loginMutation = useLoginMutation({
|
||||
authLogin,
|
||||
onError: ({ response }) => {
|
||||
if (response)
|
||||
authLogin({ name: auth.name ?? "", password: null, token: null })
|
||||
else
|
||||
authLogin({ name: auth.name ?? "", token: null })
|
||||
}
|
||||
})
|
||||
|
||||
// Renouvellement auto du jeton d'authentification
|
||||
useEffect(() => {
|
||||
@ -31,23 +41,8 @@ export default function LoginProvider({ loginRedirect, children }: Props) {
|
||||
}
|
||||
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 })
|
||||
}
|
||||
if (password)
|
||||
loginMutation.mutate({ name, password })
|
||||
}, waitTime)
|
||||
return () => clearTimeout(timeout)
|
||||
}, [auth])
|
||||
|
Reference in New Issue
Block a user