-
Notifications
You must be signed in to change notification settings - Fork 0
/
middleware.ts
56 lines (33 loc) · 1.39 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
import { NextFetchEvent, NextRequest, NextResponse } from "next/server";
import { getToken } from "next-auth/jwt";
export async function middleware(req: NextRequest, env: NextFetchEvent) {
if (req.nextUrl.pathname.startsWith("/checkout")) {
const session = await getToken({req, secret: process.env.NEXTAUTH_SECRET})
if(!session) {
return NextResponse.redirect(new URL(`/auth/login?p=${req.nextUrl.pathname}`, req.url));
}
return NextResponse.next();
}
if (req.nextUrl.pathname.startsWith("/admin/user")) {
const session: any = await getToken({req, secret: process.env.NEXTAUTH_SECRET});
if(!session) {
return NextResponse.redirect(new URL(`/auth/login?p=${req.nextUrl.pathname}`, req.url));
}
const validRoles = ['admin', 'super-user', 'SEO', 'client'];
if(!validRoles.includes(session.user.role)) {
return NextResponse.redirect(new URL('/', req.url));
}
return NextResponse.next();
}
if (req.nextUrl.pathname.startsWith("/api/admin/user")) {
const session: any = await getToken({req, secret: process.env.NEXTAUTH_SECRET});
if(!session) {
return NextResponse.redirect(new URL(`/auth/login?p=${req.nextUrl.pathname}`, req.url));
}
const validRoles = ['admin', 'super-user', 'SEO', 'client'];
if(!validRoles.includes(session.user.role)) {
return NextResponse.redirect(new URL('/', req.url));
}
return NextResponse.next();
}
}