From 19c4621f89a6a41dddc2d5dc041c04c8817a294d Mon Sep 17 00:00:00 2001 From: Wayan Galih Pratama <49269977+wayangalihpratama@users.noreply.github.com> Date: Thu, 21 Nov 2024 18:57:22 +0800 Subject: [PATCH] [#137] Check auth token by fetch /me before verification (#138) --- .../src/app/(auth)/verify/[loginId]/page.js | 31 ++++++++++++++++--- 1 file changed, 26 insertions(+), 5 deletions(-) diff --git a/frontend/src/app/(auth)/verify/[loginId]/page.js b/frontend/src/app/(auth)/verify/[loginId]/page.js index 28d0d52c..01167e8e 100644 --- a/frontend/src/app/(auth)/verify/[loginId]/page.js +++ b/frontend/src/app/(auth)/verify/[loginId]/page.js @@ -6,7 +6,7 @@ import { useAuthContext, useAuthDispatch } from "@/context/AuthContextProvider"; import { useUserContext, useUserDispatch } from "@/context/UserContextProvider"; import { useRouter } from "next/navigation"; import { api } from "@/lib"; -import { setCookie } from "@/lib/cookies"; +import { setCookie, deleteCookie } from "@/lib/cookies"; import { BackIcon } from "@/utils/icons"; const VerifyLogin = ({ params }) => { @@ -42,7 +42,7 @@ const VerifyLogin = ({ params }) => { }); setTimeout(() => { route.replace("/chats"); - }, 1000); + }, 500); } else { setError(true); } @@ -56,12 +56,33 @@ const VerifyLogin = ({ params }) => { userDispatch, ]); + const fetchUserMe = useCallback(async () => { + const res = await api.get("me"); + if (res.status === 200) { + const resData = await res.json(); + userDispatch({ type: "UPDATE", payload: { ...resData } }); + setTimeout(() => { + route.replace("/chats"); + }, 500); + } else { + verifyUser(); + } + }, [verifyUser, route, userDispatch]); + useEffect(() => { - verifyUser(); - }, [verifyUser]); + // check if there's auth token + // if yes redirect to chats page + // else verify the user + fetchUserMe(); + }, [fetchUserMe]); const handleBack = () => { - route.replace("/login"); + authDispatch({ type: "DELETE" }); + userDispatch({ type: "DELETE" }); + deleteCookie("AUTH_TOKEN"); + setTimeout(() => { + route.replace("/login"); + }, 100); }; return (