-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmiddleware.ts
24 lines (20 loc) · 881 Bytes
/
middleware.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
import { NextResponse } from 'next/server'
import type { NextRequest } from 'next/server'
import { getToken } from 'next-auth/jwt' // Use this to get the token from cookies
export async function middleware(request: NextRequest) {
const { pathname } = request.nextUrl
const token = await getToken({ req: request }) // Get the token
// Allow authenticated users to access IssuePage and showIssuePage
if (pathname.startsWith('/IssuePage') || pathname.startsWith('/showIssuePage')) {
if (!token) {
// If the user is not authenticated, redirect to the login page
return NextResponse.redirect(new URL('/LoginFormPage', request.url))
}
}
// Allow the request to continue if conditions are met
return NextResponse.next()
}
// Apply this middleware to specific paths
export const config = {
matcher: ['/IssuePage/:path*', '/showIssuePage/:path*'],
}