From 7705666c3bc09ba716c6df91eccb9272e92ae078 Mon Sep 17 00:00:00 2001 From: T'JAMPENS QUENTIN p2406187 Date: Sat, 15 Nov 2025 23:43:06 +0100 Subject: [PATCH] Fix redirect on login --- src/components/loginform/loginForm.jsx | 15 +++++---------- src/contexts/auth/authProvider.jsx | 4 +++- 2 files changed, 8 insertions(+), 11 deletions(-) diff --git a/src/components/loginform/loginForm.jsx b/src/components/loginform/loginForm.jsx index 753e525..edc6c55 100644 --- a/src/components/loginform/loginForm.jsx +++ b/src/components/loginform/loginForm.jsx @@ -1,10 +1,9 @@ -import { useState } from "react"; -import login from "../../utils/login.js"; -import getUser from "../../utils/getUser.js"; +import { useState, useContext } from "react"; import styles from "./loginForm.module.css"; import Button from "../ui/button/button.jsx"; import Input from "../ui/input/input.jsx"; import { useNavigate } from "react-router"; +import { AuthContext } from "../../contexts/auth/AuthContext.js"; function LoginForm({ className }) { @@ -12,16 +11,12 @@ function LoginForm({ className }) { const [password, setPassword] = useState(''); const [showPassword, setShowPassword] = useState(false); let navigate = useNavigate(); + const { login } = useContext(AuthContext); async function handleSubmit(e) { e.preventDefault(); - - const res = await login(email, password); - - if(res.status === 200) { - await getUser(); - navigate("/"); - } + await login(email, password); + navigate("/"); } const toggleShowPassword = () => { diff --git a/src/contexts/auth/authProvider.jsx b/src/contexts/auth/authProvider.jsx index a46d3c3..7151a96 100644 --- a/src/contexts/auth/authProvider.jsx +++ b/src/contexts/auth/authProvider.jsx @@ -17,9 +17,11 @@ export function AuthProvider({ children }) { }, []); const login = async (email, password) => { - await loginFunction(email, password); + const res = await loginFunction(email, password); + if (!res || res.status !== 200) throw new Error("Login error"); const u = await getUser(); setUser(u); + return res; }; const logout = async () => {