-
Notifications
You must be signed in to change notification settings - Fork 3
/
middleware.ts
57 lines (45 loc) · 1.78 KB
/
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
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import NextAuth from 'next-auth';
import authConfig from '@/auth.config';
import { NextRequest } from 'next/server';
import { Role } from '@prisma/client';
const { auth } = NextAuth(authConfig);
const RedirectResponse = (req: NextRequest, pathname: string) => {
const newUrl = req.nextUrl.clone();
newUrl.pathname = pathname;
return Response.redirect(newUrl);
};
export default auth((req) => {
/* Authentication & Authorization */
// If not signed in, send to login page
if (!req.auth || !req.auth.user)
return RedirectResponse(req, '/api/auth/signin');
const { pathname } = req.nextUrl;
const isAdmin = req.auth.user.role === Role.Admin;
// Redirect if accessing unauthorized pages
if (pathname.startsWith('/admin') && !isAdmin)
return RedirectResponse(req, '/hacker');
if (pathname.startsWith('/hacker') && isAdmin) {
return RedirectResponse(req, '/admin');
}
/* Redirects */
// If root, send to respective dashboard
if (pathname === '/')
return RedirectResponse(req, isAdmin ? '/admin' : '/hacker');
// Handle hacker routes
// if (pathname.startsWith('/hacker')) {
// return
// // // If not onboarded and not on onboarding page, send to onboarding
// // if (!onboarded && pathname !== '/hacker/onboarding') {
// // return RedirectResponse(req, '/hacker/onboarding');
// // }
// // // If onboarded and on onboarding page or not the dashboard page, send to hacker dashboard
// // if (onboarded && pathname === '/hacker/onboarding') {
// // return RedirectResponse(req, '/hacker');
// // }
// }
// If no comp, then send back to admin dashboard
if (pathname === '/admin/comp') return RedirectResponse(req, '/admin');
});
export const config = {
matcher: ['/((?!api|_next/static|_next/image|favicon.ico).*)'],
};