|
|
@ -1,21 +1,22 @@
|
|
|
|
import { FormEvent, useRef, useState } from "react"
|
|
|
|
import { FormEvent, startTransition, useState } from "react"
|
|
|
|
import { BASE } from "../Constants.ts"
|
|
|
|
import { BASE } from "../Constants.ts"
|
|
|
|
import { fetchAPI, redirect } from "../Fetcher.ts"
|
|
|
|
import { fetchAPI } from "../Fetcher.ts"
|
|
|
|
import { Failure } from "../api/failure.ts"
|
|
|
|
import { Failure } from "../api/failure.ts"
|
|
|
|
import { getSession, saveSession } from "../api/session.ts"
|
|
|
|
import { getSession, saveSession } from "../api/session.ts"
|
|
|
|
import "../style/form.css"
|
|
|
|
import "../style/form.css"
|
|
|
|
|
|
|
|
import { useNavigate } from "react-router-dom"
|
|
|
|
|
|
|
|
|
|
|
|
export default function LoginApp() {
|
|
|
|
export default function LoginApp() {
|
|
|
|
const [errors, setErrors] = useState<Failure[]>([])
|
|
|
|
const [errors, setErrors] = useState<Failure[]>([])
|
|
|
|
|
|
|
|
|
|
|
|
const emailRef = useRef<HTMLInputElement>(null)
|
|
|
|
const navigate = useNavigate()
|
|
|
|
const passwordRef = useRef<HTMLInputElement>(null)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async function handleSubmit(e: FormEvent) {
|
|
|
|
async function handleSubmit(e: FormEvent) {
|
|
|
|
e.preventDefault()
|
|
|
|
e.preventDefault()
|
|
|
|
|
|
|
|
|
|
|
|
const email = emailRef.current!.value
|
|
|
|
const { email, password } = Object.fromEntries(
|
|
|
|
const password = passwordRef.current!.value
|
|
|
|
new FormData(e.target as HTMLFormElement),
|
|
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
const response = await fetchAPI(
|
|
|
|
const response = await fetchAPI(
|
|
|
|
"auth/token",
|
|
|
|
"auth/token",
|
|
|
@ -28,7 +29,9 @@ export default function LoginApp() {
|
|
|
|
const session = getSession()
|
|
|
|
const session = getSession()
|
|
|
|
const { token, expirationDate } = await response.json()
|
|
|
|
const { token, expirationDate } = await response.json()
|
|
|
|
saveSession({ ...session, auth: { token, expirationDate }, urlTarget: undefined })
|
|
|
|
saveSession({ ...session, auth: { token, expirationDate }, urlTarget: undefined })
|
|
|
|
redirect(session.urlTarget ?? "/")
|
|
|
|
startTransition(() => {
|
|
|
|
|
|
|
|
navigate(session.urlTarget ?? "/")
|
|
|
|
|
|
|
|
})
|
|
|
|
return
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
@ -38,7 +41,7 @@ export default function LoginApp() {
|
|
|
|
{
|
|
|
|
{
|
|
|
|
type: "Non autorisé",
|
|
|
|
type: "Non autorisé",
|
|
|
|
messages: [
|
|
|
|
messages: [
|
|
|
|
"L'adresse email ou le mot de passe sont invalide.",
|
|
|
|
"L'adresse email ou le mot de passe sont invalides.",
|
|
|
|
],
|
|
|
|
],
|
|
|
|
},
|
|
|
|
},
|
|
|
|
])
|
|
|
|
])
|
|
|
@ -81,7 +84,6 @@ export default function LoginApp() {
|
|
|
|
<div className="form-group">
|
|
|
|
<div className="form-group">
|
|
|
|
<label htmlFor="email">Email :</label>
|
|
|
|
<label htmlFor="email">Email :</label>
|
|
|
|
<input
|
|
|
|
<input
|
|
|
|
ref={emailRef}
|
|
|
|
|
|
|
|
type="text"
|
|
|
|
type="text"
|
|
|
|
id="email"
|
|
|
|
id="email"
|
|
|
|
name="email"
|
|
|
|
name="email"
|
|
|
@ -90,7 +92,6 @@ export default function LoginApp() {
|
|
|
|
|
|
|
|
|
|
|
|
<label htmlFor="password">Mot de passe :</label>
|
|
|
|
<label htmlFor="password">Mot de passe :</label>
|
|
|
|
<input
|
|
|
|
<input
|
|
|
|
ref={passwordRef}
|
|
|
|
|
|
|
|
type="password"
|
|
|
|
type="password"
|
|
|
|
id="password"
|
|
|
|
id="password"
|
|
|
|
name="password"
|
|
|
|
name="password"
|
|
|
|