-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmiddleware.ts
More file actions
45 lines (38 loc) · 1.44 KB
/
middleware.ts
File metadata and controls
45 lines (38 loc) · 1.44 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
import { NextResponse } from "next/server";
import { auth } from "@/auth";
export default auth((req) => {
const { pathname } = req.nextUrl;
const session = req.auth;
const mustChangePwd = (
session?.user as { mustChangePassword?: boolean } | undefined
)?.mustChangePassword;
// ── Redirect old standalone routes to unified dashboard ───────────────────
if (pathname.startsWith("/operator") || pathname.startsWith("/admin")) {
return NextResponse.redirect(new URL("/dashboard", req.url));
}
if (
session &&
mustChangePwd === true &&
!pathname.startsWith("/change-password") &&
!pathname.startsWith("/api/auth") &&
!pathname.startsWith("/api/")
) {
return NextResponse.redirect(new URL("/change-password", req.url));
}
// ── Dashboard — must be authenticated ────────────────────────────────────
if (pathname.startsWith("/dashboard")) {
if (!session) {
return NextResponse.redirect(
new URL("/auth?callbackUrl=/dashboard", req.url),
);
}
}
// ── Auth page — redirect away if already signed in ────────────────────────
if (pathname === "/auth" && session) {
return NextResponse.redirect(new URL("/dashboard", req.url));
}
return NextResponse.next();
});
export const config = {
matcher: ["/((?!_next/static|_next/image|favicon.ico|.*\\.png$).*)"],
};