-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproxy.js
More file actions
35 lines (29 loc) · 1.07 KB
/
Copy pathproxy.js
File metadata and controls
35 lines (29 loc) · 1.07 KB
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
import { withAuth } from 'next-auth/middleware';
import { NextResponse } from 'next/server';
export default withAuth(
function middleware(req) {
const { pathname } = req.nextUrl;
const role = req.nextauth?.token?.role;
if (pathname === '/login' || pathname === '/register') return NextResponse.next();
if (pathname.startsWith('/users') && role !== 'ADMIN') {
return NextResponse.redirect(new URL('/', req.url));
}
if (role === 'TECHNICIAN' && (pathname.startsWith('/customers') || pathname.startsWith('/vehicles') || pathname.startsWith('/invoices'))) {
return NextResponse.redirect(new URL('/jobs', req.url));
}
return NextResponse.next();
},
{
callbacks: {
authorized: ({ token, req }) => {
const { pathname } = req.nextUrl;
if (pathname === '/login' || pathname === '/register' || pathname.startsWith('/api/register')) return true;
return !!token;
},
},
pages: { signIn: '/login' },
}
);
export const config = {
matcher: ['/((?!api/auth|_next/static|_next/image|favicon\\.ico).*)'],
};