Skip to content

Commit

Permalink
[#137] Check auth token by fetch /me before verification (#138)
Browse files Browse the repository at this point in the history
  • Loading branch information
wayangalihpratama authored Nov 21, 2024
1 parent 1eea276 commit 19c4621
Showing 1 changed file with 26 additions and 5 deletions.
31 changes: 26 additions & 5 deletions frontend/src/app/(auth)/verify/[loginId]/page.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 }) => {
Expand Down Expand Up @@ -42,7 +42,7 @@ const VerifyLogin = ({ params }) => {
});
setTimeout(() => {
route.replace("/chats");
}, 1000);
}, 500);
} else {
setError(true);
}
Expand All @@ -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("/#");
authDispatch({ type: "DELETE" });
userDispatch({ type: "DELETE" });
deleteCookie("AUTH_TOKEN");
setTimeout(() => {
route.replace("/#");
}, 100);
};

return (
Expand Down

0 comments on commit 19c4621

Please # to comment.