From de57daf98ae147f7006516d59465b8dd03d3ae55 Mon Sep 17 00:00:00 2001 From: Chief Bube Date: Mon, 23 Oct 2023 00:45:13 -0700 Subject: [PATCH] Added Logout Option and prefetch for the dashboard --- components/Authentication/SignInForm.js | 21 ++++-- components/_App/Layout.js | 11 ++- components/_App/LeftSidebar/SidebarData.js | 6 ++ components/_App/LeftSidebar/index.js | 82 +++++++++++----------- middleware.js | 16 ----- pages/auth/logout.js | 44 +++++++----- 6 files changed, 95 insertions(+), 85 deletions(-) diff --git a/components/Authentication/SignInForm.js b/components/Authentication/SignInForm.js index 29c3668..33f7c58 100644 --- a/components/Authentication/SignInForm.js +++ b/components/Authentication/SignInForm.js @@ -1,6 +1,6 @@ -import React, { useState } from "react"; +import React, { useEffect, useState } from "react"; import { useRouter } from "next/router"; -import { setCookie } from 'cookies-next'; +import { setCookie } from "cookies-next"; import Link from "next/link"; import Grid from "@mui/material/Grid"; import LoadingButton from "@mui/lab/LoadingButton"; @@ -79,9 +79,17 @@ const SignInForm = () => { } // Store the token in cookies - await setCookie("cmc-token", res.token); - router.push("/"); + // Calculate the expiration time in 24 hours + const expirationDate = new Date(); + expirationDate.setTime(expirationDate.getTime() + 24 * 60 * 60 * 1000); // 24 hours in milliseconds + const cookieOptions = { + expires: expirationDate, + httpOnly: true, // Make the cookie accessible only via HTTP (recommended for security) + }; + + await setCookie("cmc-token", res.token); + router.push("/"); console.log(res); } catch (error) { @@ -100,6 +108,11 @@ const SignInForm = () => { setShowPassword(!showPassword); }; + useEffect(() => { + // Prefetch the dashboard + router.prefetch("/"); + }); + return ( <>
diff --git a/components/_App/Layout.js b/components/_App/Layout.js index edca465..1736b2a 100644 --- a/components/_App/Layout.js +++ b/components/_App/Layout.js @@ -20,14 +20,11 @@ const Layout = ({ children }) => { useEffect(() => { const authenticationPages = [ - // "/", - "/auth", "/auth/login", "/auth/sign-up", "/auth/forgot-password", "/auth/lock-screen", "/auth/confirm-mail", - "/auth/logout", ]; setIsAuthenticationPage(authenticationPages.includes(router.pathname)); @@ -38,8 +35,8 @@ const Layout = ({ children }) => { const title = isAuthenticationPage ? "CMC - auth" : "CMC - dashboard"; const mainWrapper = { - paddingLeft: typeof window !== "undefined" && isAuthenticationPage && "0" - } + paddingLeft: typeof window !== "undefined" && isAuthenticationPage && "0", + }; return ( <> @@ -65,7 +62,9 @@ const Layout = ({ children }) => { {children}
- {!isAuthenticationPage &&