From 7e3c2cf24aad8783d714e5ef6cec706e776ca12f Mon Sep 17 00:00:00 2001 From: Umar Adilov <99314948+adilovcode@users.noreply.github.com> Date: Sat, 3 May 2025 00:45:20 +0500 Subject: [PATCH] Added unauthorized redirect --- src/middleware.ts | 26 +++++++++++++++++ .../(dashboard)/corporate-dashboard/index.tsx | 24 +++++++++++++--- .../(dashboard)/customer-dashboard/index.tsx | 28 +++++++++++++++---- 3 files changed, 68 insertions(+), 10 deletions(-) create mode 100644 src/middleware.ts diff --git a/src/middleware.ts b/src/middleware.ts new file mode 100644 index 0000000..b92eb5f --- /dev/null +++ b/src/middleware.ts @@ -0,0 +1,26 @@ +import { NextRequest, NextResponse } from 'next/server'; + +export function middleware(req: NextRequest) { + const url = req.nextUrl.clone(); + const path = url.pathname; + + if ( + path.startsWith('/customer-dashboard') || + path.startsWith('/corporate-dashboard') + ) { + const token = req.cookies.get( + `${path.includes('customer') ? 'bonus' : 'corporate'}__token`, + ); + + if (!token) { + url.pathname = '/login'; + return NextResponse.redirect(url); + } + } + + return NextResponse.next(); +} + +export const config = { + matcher: ['/customer-dashboard/:path*', '/corporate-dashboard/:path*'], +}; diff --git a/src/pages-templates/(dashboard)/corporate-dashboard/index.tsx b/src/pages-templates/(dashboard)/corporate-dashboard/index.tsx index 2353347..062b643 100644 --- a/src/pages-templates/(dashboard)/corporate-dashboard/index.tsx +++ b/src/pages-templates/(dashboard)/corporate-dashboard/index.tsx @@ -1,5 +1,6 @@ 'use client'; +import { deleteCookie } from 'cookies-next'; import { subMonths } from 'date-fns'; import { Building2, LogOut, Wallet } from 'lucide-react'; import { useState } from 'react'; @@ -104,9 +105,16 @@ export function CorporateDashboard() {