{!isAuthenticationPage && (
<>
diff --git a/components/_App/LeftSidebar/SidebarData.js b/components/_App/LeftSidebar/SidebarData.js
index d9f7457..b6157bb 100644
--- a/components/_App/LeftSidebar/SidebarData.js
+++ b/components/_App/LeftSidebar/SidebarData.js
@@ -450,7 +450,7 @@ export const SidebarData = [
},
{
title: "Authentication",
- path: "/auth/sign-in/",
+ path: "/auth/login/",
icon:
,
iconClosed:
,
iconOpened:
,
diff --git a/middleware.js b/middleware.js
new file mode 100644
index 0000000..15cb37d
--- /dev/null
+++ b/middleware.js
@@ -0,0 +1,83 @@
+import { NextResponse } from "next/server";
+import { cookies } from "next/headers";
+
+const checkAuthentication = async () => {
+ const token = req.cookies["cmc-token"]; // Access the token from cookies
+ console.log("checking token", token);
+ const isAuthenticated = token ? true : false; // Check if the user is authenticated.
+ return isAuthenticated;
+};
+
+const isTokenValid = () => {
+ const cookies = document.cookie.split("; "); // Get all cookies and split them into an array
+
+ for (const cookie of cookies) {
+ const [name, value] = cookie.split("="); // Split the cookie into its name and value
+
+ if (name === "cmc-token" && value) {
+ return true; // The cmc-token cookie exists
+ }
+ }
+
+ return false; // The cmc-token cookie does not exist
+};
+
+export async function middleware(req) {
+ const token = isTokenValid()
+ // req.cookies["cmc-token"]; // Access the token from cookies
+ const cookieList = cookies();
+
+ const headers = new Headers(req.headers);
+ headers.set("X-XSS-Protection", "1; mode=block");
+ headers.set("X-Frame-Options", "SAMEORIGIN");
+ headers.set("Content-Security-Policy", "frame-ancestors 'same';");
+
+ const { origin, pathname } = req.nextUrl;
+
+ try {
+ if (pathname === "/auth/login" && token) {
+ // Redirect to the home page if already authenticated
+ return NextResponse.redirect(new URL("/"), { status: 307 });
+ }
+
+ if (!authenticationPages.includes(pathname) && !token) {
+ // Redirect to the login page if not authenticated
+ return NextResponse.redirect(new URL("/auth/login", origin), {
+ status: 307,
+ });
+ }
+
+ // Add authentication logic here (verify the token, etc.)
+ // const isAuthenticated = verifyToken(token);
+ const isAuthenticated = cookieList.has("cmc-token");
+ console.log(token)
+
+ if (!isAuthenticated) {
+ // Handle unauthenticated users
+ return NextResponse.error(new Error("Authentication failed"), {
+ status: 401,
+ });
+ }
+
+ // Continue with the request if authenticated
+ return NextResponse.next();
+ } catch (error) {
+ console.error("Error during authentication check:", error);
+ return NextResponse.error();
+ }
+}
+
+export const config = {
+ matcher: "/",
+};
+
+const authenticationPages = [
+ // "/",
+ "/auth",
+ "/auth/login",
+ "/auth/sign-up",
+ "/auth/forgot-password",
+ "/auth/lock-screen",
+ "/auth/confirm-mail",
+ "/auth/logout",
+];
diff --git a/middlewares/AuthRoute.js b/middlewares/AuthRoute.js
index b1553a4..cfc8a64 100644
--- a/middlewares/AuthRoute.js
+++ b/middlewares/AuthRoute.js
@@ -1,24 +1,36 @@
-"use client";
+"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) {
- router.push("/auth/sign-in");
- }
- }, [router]);
+ 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",
+];
\ No newline at end of file
diff --git a/next.config.js b/next.config.js
index c538366..16157a9 100644
--- a/next.config.js
+++ b/next.config.js
@@ -14,7 +14,7 @@ const nextConfig = {
i18n: {
locales: ['en', 'ar'],
defaultLocale: 'en',
- }
+ },
}
module.exports = nextConfig
diff --git a/package.json b/package.json
index dd40d8b..50d49c5 100644
--- a/package.json
+++ b/package.json
@@ -41,7 +41,8 @@
"react-simple-maps": "^3.0.0",
"react-tabs": "^6.0.0",
"recharts": "^2.2.0",
- "swiper": "^8.4.5"
+ "swiper": "^8.4.5",
+ "axios": "^0.24.0"
},
"devDependencies": {
"sass": "^1.57.1"
diff --git a/pages/_app.js b/pages/_app.js
index feaebbe..d21f59a 100644
--- a/pages/_app.js
+++ b/pages/_app.js
@@ -23,11 +23,11 @@ function MyApp({ Component, pageProps }) {
<>
-
+ {/* */}
-
+ {/* */}
>
);
diff --git a/pages/_document.js b/pages/_document.js
index 5163bb8..32de724 100644
--- a/pages/_document.js
+++ b/pages/_document.js
@@ -1,3 +1,4 @@
+import AuthRoute from "middlewares/AuthRoute";
import Document, { Html, Head, Main, NextScript } from "next/document";
class MyDocument extends Document {
diff --git a/pages/auth/sign-in.js b/pages/auth/login.js
similarity index 72%
rename from pages/auth/sign-in.js
rename to pages/auth/login.js
index 00c63b8..f2d9e82 100644
--- a/pages/auth/sign-in.js
+++ b/pages/auth/login.js
@@ -1,5 +1,5 @@
import SignInForm from "@/components/Authentication/SignInForm";
-export default function SignIn() {
+export default function Login() {
return
;
}
diff --git a/pages/index.js b/pages/index.js
index 9c2b14d..2883faf 100644
--- a/pages/index.js
+++ b/pages/index.js
@@ -15,6 +15,7 @@ import RecentOrders from "@/components/Dashboard/eCommerce/RecentOrders";
import TeamMembersList from "@/components/Dashboard/eCommerce/TeamMembersList";
import BestSellingProducts from "@/components/Dashboard/eCommerce/BestSellingProducts";
import LiveVisitsOnOurSite from "@/components/Dashboard/eCommerce/LiveVisitsOnOurSite";
+import AuthRoute from "middlewares/AuthRoute";
function MainPage() {
return (
diff --git a/services/Fetcher.js b/services/Fetcher.js
new file mode 100644
index 0000000..6e667cc
--- /dev/null
+++ b/services/Fetcher.js
@@ -0,0 +1,61 @@
+import Axios from "axios";
+
+class Fetcher {
+ constructor(url) {
+ // this.url = url;
+ console.log("first request!!!");
+ }
+
+ // Endpoints Here
+ // GET /api/
+
+ // POST /api/
+ login(values) {
+ return this.postAuxEnd("/auth/login", values);
+ }
+
+ //---------------------------------------- -----
+ // Unified call below
+ //---------------------------------------- -----
+
+ async getAuxEnd(uri, reqData) {
+ const endPoint =
+ (process.env.AUX_ENDPOINT || "http://localhost:50016/api") + uri;
+ console.log("Checking endpoint get request", endPoint);
+ try {
+ const response = await Axios.get(endPoint);
+ console.log(response.data); // Log the response data if needed.
+ return response.data;
+ } catch (error) {
+ this.handleAxiosError(error);
+ }
+ }
+
+ async postAuxEnd(uri, reqData) {
+ const endPoint =
+ (process.env.AUX_ENDPOINT || "http://localhost:50016/api") + uri;
+ console.log("Checking endpoint post request", endPoint);
+ try {
+ const response = await Axios.post(endPoint, reqData);
+ console.log(response.data); // Log the response data if needed.
+ return response.data;
+ } catch (error) {
+ this.handleAxiosError(error);
+ }
+ }
+
+ handleAxiosError(error) {
+ if (error.response) {
+ // Response status is an error code.
+ console.log(error.response.status);
+ } else if (error.request) {
+ // Response not received though the request was sent.
+ console.log(error.request);
+ } else {
+ // An error occurred when setting up the request.
+ console.log(error.message);
+ }
+ }
+}
+
+export default Fetcher;