added authentication

This commit was merged in pull request #7.
This commit is contained in:
2023-10-21 03:06:58 -07:00
parent ed2ebe41e6
commit c48c959749
7 changed files with 327 additions and 55 deletions
+23 -11
View File
@@ -1,24 +1,36 @@
"use client";
import { useEffect, useLayoutEffect } from "react";
"use client"
import { useEffect } from "react";
import { useRouter } from "next/router";
/**
* This function is used to protect routes in a web application.
* It checks if the user is authenticated and redirects them to the sign-in page if they are not.
*/
const AuthRoute = ({ children }) => {
const router = useRouter();
const token = req.cookies["cmc-token"]; // Access the token from cookies
useEffect(() => {
const isAuthenticated = false; // In a real application, this would be determined based on the user's authentication status.
if (!isAuthenticated) {
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",
];