Merge branch 'adding-login' of Controls/CMS-Client into master

This commit is contained in:
2023-10-22 16:50:47 +00:00
committed by Gogs
7 changed files with 38 additions and 59 deletions
+7 -7
View File
@@ -1,6 +1,6 @@
import React, { useState } from "react";
import { useRouter } from "next/router";
// import { cookies } from "next/headers";
import { setCookie } from 'cookies-next';
import Link from "next/link";
import Grid from "@mui/material/Grid";
import LoadingButton from "@mui/lab/LoadingButton";
@@ -79,18 +79,18 @@ const SignInForm = () => {
}
// Store the token in cookies
// const cookieStore = cookies();
// cookieStore.set("cmc-profile", { ...res.profile }, { secure: true });
// cookieStore.set("cmc-token", res.token, { secure: true });
document.cookie = "cmc-token=" + res.token;
await setCookie("cmc-token", res.token);
router.push("/");
// setLoading(false);
router.push("/");
console.log(res);
} catch (error) {
setLoading(false);
console.log(error);
} finally {
setTimeout(() => {
setLoading(false);
}, 5000);
}
};
+1 -1
View File
@@ -34,7 +34,7 @@ const Layout = ({ children }) => {
}, [router.pathname]);
// Debugging: Log the value of isAuthenticationPage
console.log("isAuthenticationPage:", isAuthenticationPage, router.pathname);
// console.log("isAuthenticationPage:", isAuthenticationPage, router.pathname);
const title = isAuthenticationPage ? "CMC - auth" : "CMC - dashboard";
const mainWrapper = {
+19 -39
View File
@@ -1,35 +1,10 @@
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;
};
import { getCookie, hasCookie } from "cookies-next";
const isTokenValid = () => {
if (typeof window === "undefined") {
return false; // Don't execute this code on the server-side
}
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.trim() === "cmc-token" && value) {
return true; // The cmc-token cookie exists
}
}
return false;
};
export async function middleware(req) {
const token = isTokenValid();
// req.cookies["cmc-token"]; // Access the token from cookies
const cookieList = cookies();
export async function middleware(req, next) {
const token = getCookie("cmc-token", { req }); // Access the token from cookies
const headers = new Headers(req.headers);
headers.set("X-XSS-Protection", "1; mode=block");
@@ -38,13 +13,19 @@ export async function middleware(req) {
const { origin, pathname } = req.nextUrl;
try {
if (pathname === "/auth/login" && token) {
console.log("Test path", pathname, origin);
if (token) {
// Redirect to the home page if already authenticated
return NextResponse.redirect(new URL("/"), { status: 307 });
NextResponse.redirect(new URL("/"), { status: 201 });
next();
}
if (!authenticationPages.includes(pathname) && !token) {
if (
!authenticationPages.includes(pathname) ||
(authenticationPages.includes(pathname) && !token)
) {
// Redirect to the login page if not authenticated
return NextResponse.redirect(new URL("/auth/login", origin), {
status: 307,
@@ -53,15 +34,14 @@ export async function middleware(req) {
// Add authentication logic here (verify the token, etc.)
// const isAuthenticated = verifyToken(token);
const isAuthenticated = cookieList.has("cmc-token");
console.log(token);
// const isAuthenticated = hasCookie("cmc-token", { req });
if (!isAuthenticated) {
// Handle unauthenticated users
return NextResponse.error(new Error("Authentication failed"), {
status: 401,
});
}
// if (!isAuthenticated) {
// // Handle unauthenticated users
// return NextResponse.error(new Error("Authentication failed"), {
// status: 401,
// });
// }
// Continue with the request if authenticated
return NextResponse.next();
+6 -6
View File
@@ -11,13 +11,13 @@ const AuthRoute = ({ children }) => {
const isAuthenticated = token ? true : false;
if (router.pathname === "/auth/login" && isAuthenticated) {
router.push("/");
}
// if (router.pathname === "/auth/login" && isAuthenticated) {
// router.push("/");
// }
if (!authenticationPages.includes(router.pathname) && !isAuthenticated) {
router.push("/auth/login");
}
// if (!authenticationPages.includes(router.pathname) && !isAuthenticated) {
// router.push("/auth/login");
// }
}, []);
return <>{children}</>;
+3 -1
View File
@@ -42,7 +42,9 @@
"react-tabs": "^6.0.0",
"recharts": "^2.2.0",
"swiper": "^8.4.5",
"axios": "^0.24.0"
"axios": "^0.24.0",
"cookies-next": "4.0.0"
},
"devDependencies": {
"sass": "^1.57.1"
-1
View File
@@ -1,4 +1,3 @@
import AuthRoute from "middlewares/AuthRoute";
import Document, { Html, Head, Main, NextScript } from "next/document";
class MyDocument extends Document {
+2 -4
View File
@@ -17,7 +17,7 @@ import BestSellingProducts from "@/components/Dashboard/eCommerce/BestSellingPro
import LiveVisitsOnOurSite from "@/components/Dashboard/eCommerce/LiveVisitsOnOurSite";
import AuthRoute from "middlewares/AuthRoute";
function MainPage() {
export default function MainPage() {
return (
<>
{/* Page title */}
@@ -83,6 +83,4 @@ function MainPage() {
</Grid>
</>
);
}
export default MainPage
}