-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathproxy.ts
More file actions
35 lines (28 loc) · 1.03 KB
/
Copy pathproxy.ts
File metadata and controls
35 lines (28 loc) · 1.03 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
import { NextRequest, NextResponse } from "next/server";
import {
hasValidMentorBadgeSession,
MENTOR_BADGE_SESSION_COOKIE,
} from "@/lib/auth/mentor-badge-session";
export default async function middleware(request: NextRequest) {
const { pathname } = request.nextUrl;
const isMentorRoot = pathname === "/m" || pathname === "/m/";
const isProtectedMentorPath = pathname.startsWith("/m/") && !isMentorRoot;
if (!isProtectedMentorPath) {
return NextResponse.next();
}
const token = request.cookies.get(MENTOR_BADGE_SESSION_COOKIE)?.value;
const authenticated = await hasValidMentorBadgeSession(token);
if (authenticated) {
return NextResponse.next();
}
const redirectUrl = request.nextUrl.clone();
redirectUrl.pathname = "/m";
redirectUrl.searchParams.set("from", pathname);
return NextResponse.redirect(redirectUrl);
}
export const config = {
matcher: [
"/((?!_next|[^?]*\\.(?:html?|css|js(?!on)|jpe?g|webp|png|gif|svg|ttf|woff2?|ico|csv|docx?|xlsx?|zip|webmanifest)).*)",
"/(api|trpc)(.*)",
],
};