Added Logout Option and prefetch for the dashboard

This commit is contained in:
2023-10-23 00:45:13 -07:00
parent c3dea8ae95
commit de57daf98a
6 changed files with 95 additions and 85 deletions
+17 -4
View File
@@ -1,6 +1,6 @@
import React, { useState } from "react"; import React, { useEffect, useState } from "react";
import { useRouter } from "next/router"; import { useRouter } from "next/router";
import { setCookie } from 'cookies-next'; import { setCookie } from "cookies-next";
import Link from "next/link"; import Link from "next/link";
import Grid from "@mui/material/Grid"; import Grid from "@mui/material/Grid";
import LoadingButton from "@mui/lab/LoadingButton"; import LoadingButton from "@mui/lab/LoadingButton";
@@ -79,9 +79,17 @@ const SignInForm = () => {
} }
// Store the token in cookies // Store the token in cookies
await setCookie("cmc-token", res.token); // Calculate the expiration time in 24 hours
router.push("/"); 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); console.log(res);
} catch (error) { } catch (error) {
@@ -100,6 +108,11 @@ const SignInForm = () => {
setShowPassword(!showPassword); setShowPassword(!showPassword);
}; };
useEffect(() => {
// Prefetch the dashboard
router.prefetch("/");
});
return ( return (
<> <>
<div className={styles.authenticationBox}> <div className={styles.authenticationBox}>
+5 -6
View File
@@ -20,14 +20,11 @@ const Layout = ({ children }) => {
useEffect(() => { useEffect(() => {
const authenticationPages = [ const authenticationPages = [
// "/",
"/auth",
"/auth/login", "/auth/login",
"/auth/sign-up", "/auth/sign-up",
"/auth/forgot-password", "/auth/forgot-password",
"/auth/lock-screen", "/auth/lock-screen",
"/auth/confirm-mail", "/auth/confirm-mail",
"/auth/logout",
]; ];
setIsAuthenticationPage(authenticationPages.includes(router.pathname)); setIsAuthenticationPage(authenticationPages.includes(router.pathname));
@@ -38,8 +35,8 @@ const Layout = ({ children }) => {
const title = isAuthenticationPage ? "CMC - auth" : "CMC - dashboard"; const title = isAuthenticationPage ? "CMC - auth" : "CMC - dashboard";
const mainWrapper = { const mainWrapper = {
paddingLeft: typeof window !== "undefined" && isAuthenticationPage && "0" paddingLeft: typeof window !== "undefined" && isAuthenticationPage && "0",
} };
return ( return (
<> <>
@@ -65,7 +62,9 @@ const Layout = ({ children }) => {
{children} {children}
</div> </div>
{!isAuthenticationPage && <Footer />} {!isAuthenticationPage && router.pathname !== "/auth/logout" && (
<Footer />
)}
</div> </div>
<ScrollToTop /> <ScrollToTop />
@@ -6,6 +6,7 @@ import LayersIcon from "@mui/icons-material/Layers";
import CheckBoxOutlineBlankIcon from "@mui/icons-material/CheckBoxOutlineBlank"; import CheckBoxOutlineBlankIcon from "@mui/icons-material/CheckBoxOutlineBlank";
import ContentCopyIcon from "@mui/icons-material/ContentCopy"; import ContentCopyIcon from "@mui/icons-material/ContentCopy";
import LockIcon from "@mui/icons-material/Lock"; import LockIcon from "@mui/icons-material/Lock";
import LogoutIcon from "@mui/icons-material/Logout";
import SettingsIcon from "@mui/icons-material/Settings"; import SettingsIcon from "@mui/icons-material/Settings";
import PostAddIcon from "@mui/icons-material/PostAdd"; import PostAddIcon from "@mui/icons-material/PostAdd";
import MailOutlineIcon from "@mui/icons-material/MailOutline"; import MailOutlineIcon from "@mui/icons-material/MailOutline";
@@ -513,4 +514,9 @@ export const SidebarData = [
}, },
], ],
}, },
{
title: "Logout",
path: "/auth/logout",
icon: <LogoutIcon />,
},
]; ];
+34 -34
View File
@@ -1,59 +1,59 @@
import React from 'react'; import React from "react";
import { import { Box } from "@mui/material";
Box
} from "@mui/material";
import { styled } from "@mui/material/styles"; import { styled } from "@mui/material/styles";
import { SidebarData } from './SidebarData'; import { SidebarData } from "./SidebarData";
import SubMenu from './SubMenu'; import SubMenu from "./SubMenu";
import Link from 'next/link'; import Link from "next/link";
import ClearIcon from '@mui/icons-material/Clear'; import ClearIcon from "@mui/icons-material/Clear";
import IconButton from '@mui/material/IconButton'; import IconButton from "@mui/material/IconButton";
const SidebarNav = styled("nav")(({ theme }) => ({ const SidebarNav = styled("nav")(({ theme }) => ({
background: '#fff', background: "#fff",
boxShadow: "0px 4px 20px rgba(47, 143, 232, 0.07)", boxShadow: "0px 4px 20px rgba(47, 143, 232, 0.07)",
width: '300px', width: "300px",
padding: '30px 10px', padding: "30px 10px",
height: '100vh', height: "100vh",
display: 'flex', display: "flex",
justifyContent: 'center', justifyContent: "center",
position: 'fixed', position: "fixed",
top: 0, top: 0,
left: 0, left: 0,
transition: '350ms', transition: "350ms",
zIndex: '10', zIndex: "10",
overflowY: 'auto' overflowY: "auto",
})); }));
const SidebarWrap = styled("div")(({ theme }) => ({ const SidebarWrap = styled("div")(({ theme }) => ({
width: '100%' width: "100%",
})); }));
const Sidebar = ({ toggleActive }) => { const Sidebar = ({ toggleActive }) => {
return ( return (
<> <>
<div className='leftSidebarDark'> <div className="leftSidebarDark">
<SidebarNav className="LeftSidebarNav"> <SidebarNav className="LeftSidebarNav">
<SidebarWrap> <SidebarWrap>
<Box <Box
sx={{ sx={{
mb: '20px', mb: "20px",
px: '20px', px: "20px",
display: 'flex', display: "flex",
alignItems: 'center', alignItems: "center",
justifyContent: 'space-between' justifyContent: "space-between",
}} }}
> >
<Link href='/'> <Link href="/">
<img <img
src="/images/logos/wrenchboard-logo.png" alt="Logo" src="/images/logos/wrenchboard-logo.png"
className='black-logo' alt="Logo"
className="black-logo"
/> />
{/* For Dark Variation */} {/* For Dark Variation */}
<img <img
src="/images/logos/wrenchboard-logo.png" alt="Logo" src="/images/logos/wrenchboard-logo.png"
className='white-logo' alt="Logo"
className="white-logo"
/> />
</Link> </Link>
@@ -61,9 +61,9 @@ const Sidebar = ({ toggleActive }) => {
onClick={toggleActive} onClick={toggleActive}
size="small" size="small"
sx={{ sx={{
background: 'rgb(253, 237, 237)', background: "rgb(253, 237, 237)",
display: { lg: 'none' }, display: { lg: "none" },
marginLeft: "1rem" marginLeft: "1rem",
}} }}
> >
<ClearIcon /> <ClearIcon />
-16
View File
@@ -32,17 +32,6 @@ export async function middleware(req, next) {
}); });
} }
// Add authentication logic here (verify the token, etc.)
// const isAuthenticated = verifyToken(token);
// const isAuthenticated = hasCookie("cmc-token", { req });
// if (!isAuthenticated) {
// // Handle unauthenticated users
// return NextResponse.error(new Error("Authentication failed"), {
// status: 401,
// });
// }
} catch (error) { } catch (error) {
console.error("Error during authentication check:", error); console.error("Error during authentication check:", error);
return NextResponse.error(); return NextResponse.error();
@@ -54,12 +43,7 @@ export const config = {
}; };
const authenticationPages = [ const authenticationPages = [
// "/",
"/auth",
"/auth/login", "/auth/login",
"/auth/sign-up", "/auth/sign-up",
"/auth/forgot-password",
"/auth/lock-screen",
"/auth/confirm-mail",
"/auth/logout", "/auth/logout",
]; ];
+22 -14
View File
@@ -1,8 +1,16 @@
import { useRouter } from 'next/router'
import { Typography } from "@mui/material"; import { Typography } from "@mui/material";
import { Box } from "@mui/system"; import { Box } from "@mui/system";
import Button from "@mui/material/Button"; import Button from "@mui/material/Button";
import LogoutIcon from "@mui/icons-material/Logout";
import { deleteCookie } from "cookies-next";
export default function Logout() { export default function Logout() {
const router = useRouter()
const handleLogout = () => {
deleteCookie("cmc-token");
router.push("/auth/login")
};
return ( return (
<> <>
<div className="authenticationBox"> <div className="authenticationBox">
@@ -20,51 +28,51 @@ export default function Logout() {
maxWidth: "510px", maxWidth: "510px",
ml: "auto", ml: "auto",
mr: "auto", mr: "auto",
textAlign: "center" textAlign: "center",
}} }}
className="bg-black" className="bg-black"
> >
<Box> <Box>
<img <img
src="/images/logo.png" src="/images/logos/wrenchboard-logo.png"
alt="Black logo" alt="Logo"
className="black-logo" className="black-logo"
/> />
<img <img
src="/images/logo-white.png" src="/images/logos/wrenchboard-logo.png"
alt="White logo" alt="Logo"
className="white-logo" className="white-logo"
/> />
</Box> </Box>
<Box mt={4} mb={4}> <Box mt={4} mb={4}>
<img src="/images/coffee.png" alt="Coffee" /> <LogoutIcon />
</Box> </Box>
<Typography as="h1" fontSize="20px" fontWeight="500" mb={1}> <Typography as="h1" fontSize="20px" fontWeight="500" mb={1}>
You are Logged Out Would you like to sign out?
</Typography>
<Typography>
Thank you for using Admash admin template
</Typography> </Typography>
<Button <Button
href="/auth/sign-in/" onClick={handleLogout}
fullWidth fullWidth
variant="contained" variant="contained"
sx={{ sx={{
mt: 3, mt: 3,
backgroundColor: "#4687BA",
textTransform: "capitalize", textTransform: "capitalize",
borderRadius: "8px", borderRadius: "8px",
fontWeight: "500", fontWeight: "500",
fontSize: "16px", fontSize: "16px",
padding: "12px 10px", padding: "12px 10px",
color: "#fff !important" color: "#fff !important",
"&:hover": {
backgroundColor: "rgba(70, 135, 186, 0.8)",
},
}} }}
> >
Sign In Sign Out
</Button> </Button>
</Box> </Box>
</Box> </Box>