-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathmiddleware.js
More file actions
228 lines (209 loc) · 7.15 KB
/
middleware.js
File metadata and controls
228 lines (209 loc) · 7.15 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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
import { NextResponse } from "next/server";
export const config = {
matcher: ["/((?!_next/static|_next/image|favicon.ico|grid.svg).*)"],
runtime: "nodejs",
};
export async function middleware(request) {
console.log(
`${request.method} ${request.nextUrl.pathname}${request.nextUrl.search}`
);
const publicPaths = [
"/_next",
"/favicon.ico",
"/api/plate-reads",
"/api/verify-session",
"/api/health-check",
"/api/verify-key",
"/api/verify-whitelist",
"/api/check-update",
"/api/test",
"/update",
"/180.png",
"/512.png",
"/192.png",
"/1024.png",
"/grid.svg",
"/manifest.webmanifest",
];
const url = new URL(request.url);
const queryApiKey = url.searchParams.get("api_key");
if (queryApiKey) {
try {
const response = await fetch(new URL("/api/verify-key", request.url), {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ apiKey: queryApiKey }),
signal: AbortSignal.timeout(5000),
});
const result = await response.json();
if (result.valid) {
const res = NextResponse.next();
res.headers.set("x-api-key", queryApiKey);
return res;
}
} catch (error) {
console.error("API key verification error:", error);
}
}
if (publicPaths.some((path) => request.nextUrl.pathname.startsWith(path))) {
if (request.nextUrl.pathname === "/api/plates") {
const authHeader = request.headers.get("Authorization");
if (!authHeader || !authHeader.startsWith("Bearer ")) {
return new Response("Unauthorized", { status: 401 });
}
const apiKey = authHeader.replace("Bearer ", "");
try {
const response = await fetch(new URL("/api/verify-key", request.url), {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ apiKey }),
signal: AbortSignal.timeout(5000),
});
if (!response.ok) {
return new Response("Invalid API Key", { status: 401 });
}
} catch (error) {
console.error("Auth verification error:", error);
return new Response("Internal Server Error", { status: 500 });
}
}
return NextResponse.next();
}
// --- REFINED SESSION COOKIE CHECK ---
const sessionCookie = request.cookies.get("session");
const sessionId = sessionCookie ? sessionCookie.value : null; // Explicitly get value or null
console.log(
`Middleware checking path: ${request.nextUrl.pathname}, Session ID from cookie: ${sessionId}`
);
// SPECIAL HANDLING FOR LOGIN PAGE
if (request.nextUrl.pathname === "/login") {
if (sessionId) {
// Check if sessionId exists
try {
const response = await fetch(
new URL("/api/verify-session", request.url),
{
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ sessionId }), // Pass sessionId directly
signal: AbortSignal.timeout(5000),
}
);
if (response.ok) {
const result = await response.json();
if (result.valid) {
console.log(
"Authenticated user accessing login, redirecting to home"
);
return NextResponse.redirect(new URL("/", request.url));
}
return NextResponse.next();
}
} catch (error) {
console.error("Session verification error on login page:", error);
const res = NextResponse.next();
res.cookies.delete("session"); // Clear potentially invalid session
return res;
}
}
return NextResponse.next(); // No valid session, allow access to login page
}
// For all other protected routes, check authentication
if (!sessionId) {
// Now this check should correctly reflect if a session ID was found
console.log(
"No session ID found in cookie. Checking IP whitelist or redirecting to login."
);
// Check IP whitelist (existing logic, kept as is)
try {
const isWhitelistedIpResponse = await fetch(
new URL("/api/verify-whitelist", request.url),
{
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({
ip: request.ip,
headers: Object.fromEntries(request.headers),
}),
signal: AbortSignal.timeout(5000),
}
);
if (isWhitelistedIpResponse.ok) {
const isWhitelistedIp = (await isWhitelistedIpResponse.json()).allowed;
if (isWhitelistedIp) {
console.log("IP whitelisted, allowing access.");
return NextResponse.next();
}
}
} catch (error) {
console.error("IP whitelist check error:", error);
}
console.log("No session or IP not whitelisted, redirecting to /login.");
return NextResponse.redirect(new URL("/login", request.url));
}
// Session verification for protected routes
try {
const response = await fetch(new URL("/api/verify-session", request.url), {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ sessionId }), // Pass sessionId directly
signal: AbortSignal.timeout(5000),
});
if (!response.ok) {
console.error(
`Session verification request failed: ${response.status} for path: ${request.nextUrl.pathname}`
);
if (response.status >= 400 && response.status < 500) {
console.log(
"Client error during session verification, redirecting to login and clearing cookie."
);
const res = NextResponse.redirect(new URL("/login", request.url));
res.cookies.delete("session");
return res;
} else {
console.log(
"Server error during session verification, allowing access to prevent random logouts."
);
return NextResponse.next();
}
}
const result = await response.json();
if (!result.valid) {
console.log(
"Invalid session for protected route, clearing cookie and redirecting to login."
);
const res = NextResponse.redirect(new URL("/login", request.url));
res.cookies.delete("session");
return res;
}
if (!request.nextUrl.pathname.startsWith("/api/")) {
try {
const updateResponse = await fetch(
new URL("/api/check-update", request.url),
{ signal: AbortSignal.timeout(5000) }
);
if (updateResponse.ok) {
const updateData = await updateResponse.json();
if (updateData.updateRequired) {
return NextResponse.redirect(new URL("/update", request.url));
}
}
} catch (error) {
console.error("Update check error:", error);
}
}
return NextResponse.next();
} catch (error) {
console.error("Session verification fetch error in middleware:", error);
if (error.name === "AbortError") {
console.log(
"Session verification timeout, allowing access to prevent logout."
);
} else {
console.log(
"Network error during session verification, allowing access."
);
}
return NextResponse.next();
}
}