-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmiddleware.ts
More file actions
41 lines (28 loc) · 797 Bytes
/
middleware.ts
File metadata and controls
41 lines (28 loc) · 797 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import { getToken } from "next-auth/jwt"
import { NextRequest, NextResponse } from "next/server"
// Lets Protect Users & Admins Routes
export async function middleware (req:NextRequest){
const token = await getToken({req})
if(!token){
return NextResponse.redirect(new URL('/', req.url))
}
// Role Based Auth
const {pathname} = req.nextUrl;
// Admin Routes
const adminRoutes = [
'/Admin'
]
// Check if User is Trying To Access The Admin Routes
if(adminRoutes.some((route) => pathname.startsWith(route))){
if(!token.isAdmin){
return NextResponse.redirect(new URL('/Home', req.url))
}
}
return NextResponse.next()
}
export const config = {
matcher: [
'/Admin',
'/Home'
]
}