Stockage du jeton d'authentification dans le store local, permettant l'utilisation de hooks

This commit is contained in:
2024-12-10 18:56:50 +01:00
parent 72862da3a6
commit 363dfa5c74
8 changed files with 108 additions and 31 deletions

View File

@ -1,3 +1,4 @@
import { useAuth, useAuthLogin, useAuthLogout } from "@/hooks/useAuth"
import * as SecureStore from "@/utils/SecureStore"
import { useRouter } from "expo-router"
import { useRef, useState } from "react"
@ -6,9 +7,14 @@ import { Appbar, Button, Dialog, Portal, Surface, Text, TextInput } from "react-
export default function Login() {
const router = useRouter()
const [isLoggedIn, setIsLoggedIn] = useState(SecureStore.getItem("apiToken") !== null)
const auth = useAuth()
const authLogin = useAuthLogin()
const authLogout = useAuthLogout()
const isLoggedIn = auth.loggedIn
const [loggingIn, setLoggingIn] = useState(false)
const [name, setName] = useState(SecureStore.getItem('apiName') ?? "")
const [name, setName] = useState(auth.name ?? "")
const [password, setPassword] = useState("")
const [errorDialogVisible, setErrorDialogVisible] = useState(false)
const [errorTitle, setErrorTitle] = useState("")
@ -45,6 +51,7 @@ export default function Login() {
return
}
setLoggingIn(false)
authLogin({ name: name, token: resp.accessToken })
SecureStore.setItem("apiName", name)
if (Platform.OS !== "web") {
// Le stockage navigateur n'est pas sûr, on évite de stocker un mot de passe à l'intérieur
@ -58,10 +65,10 @@ export default function Login() {
}
async function logout() {
authLogout()
await SecureStore.deleteItemAsync("apiName")
await SecureStore.deleteItemAsync("apiPassword")
await SecureStore.deleteItemAsync("apiToken")
setIsLoggedIn(false)
}
return (