-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmiddleware.ts
More file actions
35 lines (27 loc) · 943 Bytes
/
middleware.ts
File metadata and controls
35 lines (27 loc) · 943 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
33
34
35
import { NextRequest, NextResponse } from "next/server";
export async function middleware(req: NextRequest) {
const token = req.cookies.get("firebaseToken");
const currentPath = req.nextUrl.pathname;
const signinUrl = new URL(`/signin?redirect=${encodeURIComponent(currentPath)}`, req.url);
if (!token) {
return NextResponse.redirect(signinUrl);
}
try {
const apiUrl = new URL("/api/auth/verify-token", req.url);
const res = await fetch(apiUrl.toString(), {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ token: token.value }),
});
if (!res.ok) {
return NextResponse.redirect(signinUrl);
}
return NextResponse.next();
} catch (err) {
console.error("Middleware auth error:", err);
return NextResponse.redirect(signinUrl);
}
}
export const config = {
matcher: ["/create-auction", "/profile", "/(auctions/(?!$).*)"],
};