-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmiddleware.ts
More file actions
37 lines (31 loc) · 799 Bytes
/
middleware.ts
File metadata and controls
37 lines (31 loc) · 799 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
import { withAuth } from 'next-auth/middleware';
import { NextResponse } from 'next/server';
import { canAccessPath, isPublicPath, isUserRole } from '@/lib/appAccess';
export default withAuth(
function middleware(req) {
return NextResponse.next();
},
{
callbacks: {
authorized: ({ token, req }) => {
const pathname = req.nextUrl.pathname;
if (isPublicPath(pathname)) {
return true;
}
if (!token) {
return false;
}
if (!isUserRole(token.role)) {
return false;
}
return canAccessPath(pathname, token.role);
},
},
pages: {
signIn: '/auth/signin',
},
},
);
export const config = {
matcher: ['/((?!api|_next/static|_next/image|favicon.ico|images).*)'],
};