-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathproxy.ts
More file actions
67 lines (59 loc) · 2.16 KB
/
Copy pathproxy.ts
File metadata and controls
67 lines (59 loc) · 2.16 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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import { NextResponse } from "next/server";
import type { NextRequest } from "next/server";
export async function proxy(request: NextRequest) {
const { pathname } = request.nextUrl;
// Protect the /dashboard route
if (pathname.startsWith("/dashboard")) {
try {
// Validate session by calling the Better Auth get-session endpoint
const baseUrl = request.nextUrl.origin;
const sessionResponse = await fetch(`${baseUrl}/api/auth/get-session`, {
method: "GET",
headers: {
// Forward the cookie header to maintain session context
cookie: request.headers.get("cookie") || "",
},
});
// Check if the session validation succeeded
if (!sessionResponse.ok) {
// Invalid or expired session, redirect to login
const url = request.nextUrl.clone();
url.pathname = "/login";
url.searchParams.set("redirect", pathname);
return NextResponse.redirect(url);
}
// Parse the response to validate the session data
const sessionData = await sessionResponse.json();
// Check if session data is valid and contains a user
if (!sessionData || !sessionData.user) {
// No valid user session, redirect to login
const url = request.nextUrl.clone();
url.pathname = "/login";
url.searchParams.set("redirect", pathname);
return NextResponse.redirect(url);
}
// Valid session, allow the request to proceed
return NextResponse.next();
} catch (error) {
console.error("Middleware session validation error:", error);
// On error (network, parsing, etc.), redirect to login
const url = request.nextUrl.clone();
url.pathname = "/login";
url.searchParams.set("redirect", pathname);
return NextResponse.redirect(url);
}
}
return NextResponse.next();
}
export const config = {
matcher: [
/*
* Match all request paths except for the ones starting with:
* - api (API routes)
* - _next/static (static files)
* - _next/image (image optimization files)
* - favicon.ico (favicon file)
*/
"/((?!api|_next/static|_next/image|favicon.ico).*)",
],
};