Skip to content

Commit

Permalink
Merge pull request #138 from eduversa/ankur
Browse files Browse the repository at this point in the history
fix: Update authentication flow in Login component to use fetch API a…
  • Loading branch information
ankurhalder authored Nov 10, 2024
2 parents 391e627 + d00b91d commit 2182587
Showing 1 changed file with 52 additions and 29 deletions.
81 changes: 52 additions & 29 deletions pages/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 2182587

Please sign in to comment.