-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmiddleware.ts
More file actions
32 lines (27 loc) · 820 Bytes
/
middleware.ts
File metadata and controls
32 lines (27 loc) · 820 Bytes
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
import { NextResponse, NextRequest } from "next/server"
export const config = {
matcher: [
"/",
"/login",
"/courses/:path*",
"/calender",
"/profile",
"/tools/:path*",
],
}
export function middleware(req: NextRequest) {
const isPublicPath = req.nextUrl.pathname === "/login"
const token = req.cookies.get("user-token")?.value
if (isPublicPath && token) {
return NextResponse.redirect(new URL("/", req.nextUrl))
}
// redirect to login page if token was not found
// handle csrf token in the client side
if (!isPublicPath && !token) {
const destination = req.nextUrl.href.split(
`${process.env.NEXT_PUBLIC_URL}/`
)[1]
const url = destination ? `/login?callback=${destination}` : "/login"
return NextResponse.redirect(new URL(url, req.nextUrl))
}
}