From d00b91daa335897bfe8fca3bea5ce44d119ce617 Mon Sep 17 00:00:00 2001 From: Ankur Halder Date: Sun, 10 Nov 2024 15:32:03 +0530 Subject: [PATCH] fix: Update authentication flow in Login component to use fetch API and handle user types --- pages/index.jsx | 81 +++++++++++++++++++++++++++++++------------------ 1 file changed, 52 insertions(+), 29 deletions(-) diff --git a/pages/index.jsx b/pages/index.jsx index c6c84058..c01c214f 100644 --- a/pages/index.jsx +++ b/pages/index.jsx @@ -20,37 +20,60 @@ function Login() { useEffect(() => { const platformName = localStorage.getItem("platformName"); if (session) { - devLog("Session detected:", session); - + console.log("session--->", session); setLoading(true); - authenticatePlatformUser(platformName, session) - .catch((error) => devLog("Error in platform auth:", error)) - .finally(() => setLoading(false)); - } - // eslint-disable-next-line react-hooks/exhaustive-deps - }, [session, showAlert]); - - const authenticatePlatformUser = async (platformName, sessionData) => { - const response = await apiRequest( - `/account/auth/platform?platform=${platformName}`, - "POST", - sessionData - ); - - devLog("Auth platform response:", response); - showAlert(response.message); - setLoading(false); - if (!response.status) { - setLoading(false); - return; + fetch( + `https://eduversa-api.onrender.com/account/auth/platform?platform=${platformName}`, + { + method: "PUT", + headers: { + "Content-Type": "application/json", + }, + body: JSON.stringify(session), + } + ) + .then((response) => response.json()) + .then(async (res) => { + console.log(res); + alert(res.message); + if (!res.status) { + setLoading(false); + return; + } + localStorage.removeItem("platformName"); + localStorage.setItem("authToken", res.authToken); + localStorage.setItem("email", res.data.email); + localStorage.setItem("userType", res.data.type); + localStorage.setItem("userid", res.data.user_id); + if (res.data.type === "applicant") { + localStorage.setItem( + "applicant_profile", + JSON.stringify(res.profileData) + ); + } + if (process.env.NODE_ENV === "development") { + console.log("AuthToken", localStorage.getItem("authToken")); + console.log("Email", localStorage.getItem("email")); + console.log("UserType", localStorage.getItem("userType")); + console.log("UserId", localStorage.getItem("userid")); + } + alert(res.message); + if (res.data.type === "applicant") { + await signOut({ callbackUrl: "/applicant" }); + } else if (res.data.type === "student") { + await signOut({ callbackUrl: "/student" }); + } else if (res.data.type === "faculty") { + await signOut({ callbackUrl: "/faculty" }); + localStorage.clear(); + } else if (res.data.type === "admin") { + await signOut({ callbackUrl: "/admin" }); + } else { + alert("Invalid User Type"); + } + }) + .catch((error) => console.log(error)); } - - localStorage.removeItem("platformName"); - setLoading(false); - storeUserData(response.data); - - await signOut({ callbackUrl: "/" }); - }; + }, [session]); const storeUserData = (data) => { localStorage.setItem("authToken", data.authToken);