This repository was archived by the owner on Oct 15, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmiddleware.ts
More file actions
46 lines (38 loc) · 1.38 KB
/
middleware.ts
File metadata and controls
46 lines (38 loc) · 1.38 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
import type { NextRequest } from "next/server";
import { NextResponse } from "next/server";
// Paths that should not be processed by the middleware
const PUBLIC_FILE = /\.(.*)$/;
export function middleware(req: NextRequest) {
const { nextUrl, cookies } = req;
const { pathname, searchParams } = nextUrl;
// Skip static files and Next internals
if (PUBLIC_FILE.test(pathname) || pathname.startsWith("/_next")) {
return NextResponse.next();
}
// Detect simple auth state via client-managed cookie
const isAuthed = cookies.get("diuacm_auth")?.value === "1";
// If visiting /login while authenticated, redirect to the desired destination
if (pathname === "/login" && isAuthed) {
const redirect = searchParams.get("redirect") || "/";
const url = req.nextUrl.clone();
url.pathname = redirect;
url.search = "";
return NextResponse.redirect(url);
}
// Guard certain authenticated-only pages: redirect to /login with ?redirect=...
const protectedPaths = ["/profile/edit"];
if (protectedPaths.includes(pathname) && !isAuthed) {
const url = req.nextUrl.clone();
url.pathname = "/login";
url.searchParams.set("redirect", pathname + (nextUrl.search || ""));
return NextResponse.redirect(url);
}
return NextResponse.next();
}
export const config = {
matcher: [
"/login",
"/profile/:path*",
// add more app routes if needed
],
};