Files
CMS-Client/middlewares/AuthRoute.js
T
2023-10-21 03:06:58 -07:00

36 lines
770 B
JavaScript

"use client"
import { useEffect } from "react";
import { useRouter } from "next/router";
const AuthRoute = ({ children }) => {
const router = useRouter();
const token = req.cookies["cmc-token"]; // Access the token from cookies
useEffect(() => {
const isAuthenticated = token ? true : false;
if (router.pathname === "/auth/login" && isAuthenticated) {
router.push("/");
}
if (!authenticationPages.includes(router.pathname) && !isAuthenticated) {
router.push("/auth/login");
}
}, []);
return <>{children}</>;
};
export default AuthRoute;
const authenticationPages = [
// "/",
"/auth",
"/auth/login",
"/auth/sign-up",
"/auth/forgot-password",
"/auth/lock-screen",
"/auth/confirm-mail",
"/auth/logout",
];