generated from muhtadeetaron/nextjs-template
initial commit
This commit is contained in:
22
torpedo/middleware.ts
Normal file
22
torpedo/middleware.ts
Normal file
@ -0,0 +1,22 @@
|
||||
import { NextResponse } from 'next/server';
|
||||
import { getToken } from 'next-auth/jwt';
|
||||
import type { NextRequest } from 'next/server';
|
||||
|
||||
export async function middleware(req: NextRequest) {
|
||||
// Get the token from the request
|
||||
const token = await getToken({ req, secret: process.env.NEXTAUTH_SECRET });
|
||||
|
||||
const { pathname } = req.nextUrl;
|
||||
|
||||
// If the user is not authenticated and trying to access the dashboard, redirect to login
|
||||
if (!token && pathname.startsWith('/dashboard')) {
|
||||
return NextResponse.redirect(new URL('/auth/login', req.url));
|
||||
}
|
||||
|
||||
// Allow the request to proceed
|
||||
return NextResponse.next();
|
||||
}
|
||||
|
||||
export const config = {
|
||||
matcher: ['/dashboard/:path*'], // Protect all routes under /dashboard
|
||||
};
|
||||
Reference in New Issue
Block a user