-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmiddleware.ts
More file actions
31 lines (26 loc) · 1007 Bytes
/
middleware.ts
File metadata and controls
31 lines (26 loc) · 1007 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
import { getToken } from "next-auth/jwt";
import { NextResponse } from "next/server";
import type { NextRequest } from "next/server";
export async function middleware(req: NextRequest) {
const { pathname } = req.nextUrl;
// Debug: Log every middleware invocation
console.log(`[middleware] Invoked for pathname: ${pathname}`);
// Only protect this page
if (pathname === "/ask-a-question") {
try {
const token = await getToken({ req, secret: process.env.NEXTAUTH_SECRET });
console.log(`[middleware] Token for /ask-a-question:`, token);
if (!token) {
console.log(`[middleware] No token found, redirecting to /auth/signin`);
return NextResponse.redirect(new URL("/auth/signin", req.url));
}
} catch (err) {
console.error("[middleware] Token error in middleware:", err);
return NextResponse.redirect(new URL("/auth/signin", req.url));
}
}
return NextResponse.next();
}
export const config = {
matcher: ["/ask-a-question"],
};