From d7a97b5d0859cb95591bef537209a2178c99e7e7 Mon Sep 17 00:00:00 2001 From: anuragnegi000 Date: Sun, 9 Jun 2024 09:45:12 +0530 Subject: [PATCH] fixes in login page --- src/pages/Login.jsx | 66 +++++++++++++-------------------------------- 1 file changed, 19 insertions(+), 47 deletions(-) diff --git a/src/pages/Login.jsx b/src/pages/Login.jsx index c6901e9..8fe21a4 100644 --- a/src/pages/Login.jsx +++ b/src/pages/Login.jsx @@ -26,52 +26,31 @@ function Login() { async function submitHandler(event) { event.preventDefault(); + setLoading(true); - if (formData.accountType === "User") { - //Loader will show till the api fetching is done as show as promise is resolved the loader will be not shown - setLoading(true); + const apiUrl = formData.accountType === "User" + ? "http://localhost:3000/api/v1/studentLogin" + : `${process.env.REACT_APP_BASE_URL}/canteenLogin`; - // const apiUrl = `${process.env.REACT_APP_BASE_URL}/studentLogin`; - const apiUrl = `${process.env.REACT_APP_BASE_URL}/studentLogin`; - - - // Assuming the response contains a token - const token = response.data.token; + try { + const response = await axios.post(apiUrl, formData); + const { token, cantId } = response.data; localStorage.setItem("token", token); - localStorage.setItem("canteenId", response.data.cantId); + localStorage.setItem("canteenId", cantId); + if (formData.accountType === "User") { toast.success("User logged in successfully!"); - navigate("/home"); - } catch (error) { - toast.error("Failed To Login. Please try again."); - console.error(error); - } finally { - setLoading(false); + } else { + toast.success("User Logged in "); + navigate(`/section/${cantId}`); } - } - - else{ - const apiUrl = `${process.env.REACT_APP_BASE_URL}/canteenLogin`; - setLoading(true); - - axios - .post(apiUrl, formData) - .then((response) => { - setLoading(false); - localStorage.setItem("canteenId", response.data.cantId); - localStorage.setItem("token", response.data.token); - toast.success("User Logged in "); - navigate( - `/section/${response.data.cantId}` - ); - }) - .catch((error) => { - //Loader will show till the api fetching is done as show as promise is resolved the loader will be not shown - setLoading(false); - toast.error("Failed to login"); - }); + } catch (error) { + toast.error("Failed To Login. Please try again."); + console.error(error); + } finally { + setLoading(false); } } @@ -83,14 +62,9 @@ function Login() {
- logo + logo

- Connecting You to Your College - Canteens + Connecting You to Your College Canteens

@@ -102,10 +76,8 @@ function Login() {
-

Hello Again!