diff --git a/components/Authentication/ForgotPasswordForm.js b/components/Authentication/ForgotPasswordForm.js index 96ea043..4f64d1c 100644 --- a/components/Authentication/ForgotPasswordForm.js +++ b/components/Authentication/ForgotPasswordForm.js @@ -104,7 +104,7 @@ const ForgotPasswordForm = () => { Back to Sign in diff --git a/components/Authentication/SignUpForm.js b/components/Authentication/SignUpForm.js index 64da2af..cebe119 100644 --- a/components/Authentication/SignUpForm.js +++ b/components/Authentication/SignUpForm.js @@ -45,7 +45,7 @@ const SignUpForm = () => { Already have an account?{" "} Sign in diff --git a/components/_App/Layout.js b/components/_App/Layout.js index d77c904..979e0df 100644 --- a/components/_App/Layout.js +++ b/components/_App/Layout.js @@ -20,8 +20,9 @@ const Layout = ({ children }) => { useEffect(() => { const authenticationPages = [ + "/", "/auth", - "/auth/sign-in", + "/auth/login", "/auth/sign-up", "/auth/forgot-password", "/auth/lock-screen", @@ -36,6 +37,9 @@ const Layout = ({ children }) => { console.log("isAuthenticationPage:", isAuthenticationPage, router.pathname); const title = isAuthenticationPage ? "CMC - auth" : "CMC - dashboard"; + const mainWrapper = { + paddingLeft: typeof window !== "undefined" && isAuthenticationPage && "0" + } return ( <> @@ -46,9 +50,7 @@ const Layout = ({ children }) => {
{!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..a373b1c --- /dev/null +++ b/middleware.js @@ -0,0 +1,48 @@ +import { NextResponse } from "next/server"; + +const checkAuthentication = async () => { + // Replace this logic with your actual authentication check. + const isAuthenticated = false; // Check if the user is authenticated. + return isAuthenticated; +}; + +export async function middleware(req) { + 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 { + const authenticated = await checkAuthentication(); + + if (pathname === "/auth/login" && authenticated) { + return NextResponse.redirect(new URL("/ecommerce")); + } + + if (authenticationPages.includes(pathname) && !authenticated) { + return NextResponse.redirect(new URL("/auth/", origin)); + } + + 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..ba915d9 100644 --- a/middlewares/AuthRoute.js +++ b/middlewares/AuthRoute.js @@ -1,5 +1,5 @@ "use client"; -import { useEffect } from "react"; +import { useEffect, useLayoutEffect } from "react"; import { useRouter } from "next/router"; /** @@ -10,13 +10,12 @@ import { useRouter } from "next/router"; const AuthRoute = ({ children }) => { const router = useRouter(); - useEffect(() => { - const isAuthenticated = false; // In a real application, this would be determined based on the user's authentication status. + 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]); + if (!isAuthenticated) { + router.push("/"); + return null; + } return <>{children}; }; 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 8bdeb0d..20ae2c0 100644 --- a/package.json +++ b/package.json @@ -40,7 +40,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/_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..a3c2273 100644 --- a/pages/index.js +++ b/pages/index.js @@ -1,87 +1,6 @@ -import React from "react"; -import Grid from "@mui/material/Grid"; -import Link from "next/link"; -import styles from "@/styles/PageTitle.module.css"; -import Features from "@/components/Dashboard/eCommerce/Features"; -import Ratings from "@/components/Dashboard/eCommerce/Ratings"; -import AudienceOverview from "@/components/Dashboard/eCommerce/AudienceOverview"; -import VisitsByDay from "@/components/Dashboard/eCommerce/VisitsByDay"; -import Impressions from "@/components/Dashboard/eCommerce/Impressions"; -import ActivityTimeline from "@/components/Dashboard/eCommerce/ActivityTimeline"; -import RevenuStatus from "@/components/Dashboard/eCommerce/RevenuStatus"; -import SalesByCountries from "@/components/Dashboard/eCommerce/SalesByCountries"; -import NewCustomers from "@/components/Dashboard/eCommerce/NewCustomers"; -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 SignInForm from "@/components/Authentication/SignInForm"; +import axios from "axios" -function MainPage() { - return ( - <> - {/* Page title */} -
-

eCommerce

-
    -
  • - Dashboard -
  • -
  • eCommerce
  • -
-
- - - {/* Features */} - - {/* AudienceOverview */} - - - - {/* VisitsByDay */} - - - - {/* Impressions */} - - - {/* ActivityTimeline */} - - - - {/* RevenuStatus */} - - - - - - {/* Ratings */} - - {/* LiveVisitsOnOurSite */} - - {/* SalesByLocations */} - - {/* NewCustomers */} - - - - {/* Recent Orders */} - - - - {/* TeamMembersList */} - - - - {/* BestSellingProducts */} - - - - - ); -} - -export default MainPage \ No newline at end of file +export default function SignIn() { + return ; +} \ No newline at end of file