added authentication
This commit was merged in pull request #7.
This commit is contained in:
+23
-11
@@ -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",
|
||||
];
|
||||
Reference in New Issue
Block a user