-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmiddleware.ts
More file actions
47 lines (39 loc) · 1.27 KB
/
middleware.ts
File metadata and controls
47 lines (39 loc) · 1.27 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
36
37
38
39
40
41
42
43
44
45
46
47
import { NextResponse } from 'next/server';
import type { NextRequest } from 'next/server';
import { getToken } from 'next-auth/jwt';
import { UserRole } from '@/features/shared/types/user';
import { getConfig } from '@/server/config';
export default async function middleware(req: NextRequest) {
const config = getConfig();
const useSecureCookies = config.enableSecureCookies;
const token = await getToken({ req, secureCookie: useSecureCookies });
if (!token) {
return NextResponse.redirect(new URL('/api/auth/signin', req.url));
}
if (token.role !== UserRole.Admin) {
const path = req.nextUrl.pathname;
if (path === '/analytics') {
return NextResponse.redirect(new URL('/404', req.url));
}
else if (path.startsWith('/settings')) {
// Check is user a Lead of *any* user group
if (!token.isUserGroupLead) {
return NextResponse.redirect(new URL('/404', req.url));
}
}
}
}
// https://nextjs.org/docs/pages/building-your-application/routing/middleware#matching-paths
// Commented out routes are protected via next-auth
export const config = {
matcher: [
'/analytics',
'/settings/:path*',
// /chat/:path*
// /legal
// /library/:path*
// /profile
// /prompt-generator
// /prompt-playground
],
};