-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmiddleware.js
More file actions
38 lines (29 loc) · 1007 Bytes
/
middleware.js
File metadata and controls
38 lines (29 loc) · 1007 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
import { NextResponse } from 'next/server';
import { getToken } from 'next-auth/jwt';
export async function middleware(req) {
const token = await getToken({
req,
secret: process.env.NEXTAUTH_SECRET,
});
const isAdmin = token?.is_admin;
const { pathname } = req.nextUrl;
if (pathname.startsWith('/restaurant/')) {
return NextResponse.next();
}
if (isAdmin && pathname === '/') {
return NextResponse.redirect(new URL('/admin-dashboard', req.url));
}
if (!isAdmin && pathname === '/admin-dashboard') {
return NextResponse.redirect(new URL('/dashboard', req.url));
}
if (token && pathname === '/') {
return NextResponse.redirect(new URL('/dashboard', req.url));
}
if (!token && pathname !== '/') {
return NextResponse.redirect(new URL('/', req.url));
}
return NextResponse.next();
}
export const config = {
matcher: ['/', '/dashboard', '/admin-dashboard', '/restaurant/:path*'],
};