-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmiddleware.ts
36 lines (29 loc) · 985 Bytes
/
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
import { NextRequest, NextResponse } from "next/server";
import { getToken } from "next-auth/jwt";
export { default } from "next-auth/middleware";
export async function middleware(req: NextRequest) {
const token = await getToken({ req, secret: process.env.NEXT_AUTH_SECRET! });
const currentUrl = req.nextUrl;
if (
!token &&
(currentUrl.pathname === "/home" ||
currentUrl.pathname === "/admin" ||
currentUrl.pathname.startsWith("/watch") ||
currentUrl.pathname.startsWith("/course"))
) {
return NextResponse.redirect(new URL("/", req.url));
}
if (
token &&
currentUrl.pathname.startsWith("/admin") &&
token.email !== process.env.ADMIN_EMAIL
) {
return NextResponse.redirect(new URL("/home", req.url));
}
if (token && currentUrl.pathname === "/") {
return NextResponse.redirect(new URL("/home", req.url));
}
}
export const config = {
matcher: ["/", "/home", "/admin", "/watch/:path*", "/course/:path*"],
};