diff --git a/app/account/login/code/page.tsx b/app/account/login/code/page.tsx index 05f075e..140126e 100644 --- a/app/account/login/code/page.tsx +++ b/app/account/login/code/page.tsx @@ -16,7 +16,7 @@ export default function Login() { const router = useRouter() useEffect(() => { - const checkStatus = async () => { + const validateStageTwo = async () => { setErrorMessage("") setIsLoading(true) @@ -42,7 +42,7 @@ export default function Login() { } } - checkStatus() + validateStageTwo() }, [magicCode, router]) const handleSubmit = async (e: React.FormEvent) => { @@ -63,7 +63,6 @@ export default function Login() { if (response.ok && data.success) { Cookies.set("key", data.key) - Cookies.remove("email") Cookies.remove("stageTwoKey") router.push("/account/dashboard") } else { diff --git a/app/account/logout/page.tsx b/app/account/logout/page.tsx index af4976a..a9e5baa 100644 --- a/app/account/logout/page.tsx +++ b/app/account/logout/page.tsx @@ -3,14 +3,66 @@ import { useEffect } from "react" import { Flex, Text, Spinner } from "@radix-ui/themes" import { useRouter } from "next/navigation" +import Cookies from "js-cookie" export default function Logout() { const router = useRouter() useEffect(() => { + const verifyKey = async () => { + try { + const response = await fetch('http://localhost:3001/auth/validateKey', { + method: "POST", + headers: { + "Content-Type": "application/json", + }, + body: JSON.stringify({ email: Cookies.get("email"), key: Cookies.get("key") }), + }) + + const data = await response.json() + + if (data.error) { + router.push('/account/login') + return false; + } + } catch (error) { + console.error("There was a problem with checking the existing key:", error); + return false; + } + } + + const logoutUser = async () => { + const keycheck = await verifyKey() + if (keycheck !== false) { + try { + const response = await fetch('http://localhost:3001/auth/logout', { + method: "POST", + headers: { + "Content-Type": "application/json", + }, + body: JSON.stringify({ email: Cookies.get("email"), key: Cookies.get("key") }), + }) + + const data = await response.json() + + if (data.success) { + Cookies.remove("key") + Cookies.remove("email") + router.push("/account/login") + } else if (data.error) { + console.error("There was a problem with processing the logout on the backend:", data.error); + } else { + console.error("There was a problem with processing the logout on the backend:"); + } + } catch (error) { + console.error("There was a problem with processing the logout on the backend:", error); + } + } + } + const timer = setTimeout(() => { - router.push("/account/login") - }, 5000) + logoutUser() + }, 1250) return () => clearTimeout(timer) }, [router])