-
Notifications
You must be signed in to change notification settings - Fork 0
/
middleware.ts
32 lines (26 loc) · 977 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
import { NextResponse } from "next/server";
import type { NextRequest } from "next/server";
import { getToken } from "next-auth/jwt";
const protectedRoutes = ["/TherapistDashboard", "/TherapistApply"];
export async function middleware(request: NextRequest) {
const token = await getToken({
req: request,
secret: process.env.NEXTAUTH_SECRET,
});
console.log("Middleware executed for path:", request.nextUrl.pathname);
console.log("Token:", token);
if (
!token &&
protectedRoutes.some((route) => request.nextUrl.pathname.startsWith(route))
) {
console.log("Redirecting to signin page");
const signInUrl = new URL("/api/auth/signin", request.url);
signInUrl.searchParams.set("callbackUrl", request.url);
return NextResponse.redirect(signInUrl);
}
console.log("Proceeding to next middleware/page");
return NextResponse.next();
}
export const config = {
matcher: ["/TherapistDashboard/:path*", "/TherapistApply/:path*"],
};