-
Notifications
You must be signed in to change notification settings - Fork 41
Expand file tree
/
Copy pathmiddleware.ts
More file actions
104 lines (89 loc) · 2.82 KB
/
middleware.ts
File metadata and controls
104 lines (89 loc) · 2.82 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
import { NextRequest, NextResponse } from "next/server";
import { AUTH_COOKIE_NAME, verifySessionToken } from "@/lib/auth/session";
function isPublicPage(pathname: string): boolean {
return pathname === "/login";
}
function isPublicApi(req: NextRequest, pathname: string): boolean {
if (pathname === "/api/health") return true;
if (pathname === "/api/auth/login") return true;
if (pathname === "/api/auth/logout") return true;
if (pathname === "/api/auth/status") return true;
if (pathname === "/api/external/message") return true;
if (pathname === "/api/integrations/telegram" && req.method === "POST") {
return true;
}
return false;
}
function shouldBypass(pathname: string): boolean {
if (/\.[^/]+$/.test(pathname)) {
return true;
}
if (
pathname.startsWith("/_next/static") ||
pathname.startsWith("/_next/image") ||
pathname === "/favicon.ico" ||
pathname === "/robots.txt" ||
pathname === "/sitemap.xml"
) {
return true;
}
return false;
}
function buildLoginRedirect(req: NextRequest): NextResponse {
const loginUrl = new URL("/login", req.url);
const next = `${req.nextUrl.pathname}${req.nextUrl.search}`;
if (next && next !== "/") {
loginUrl.searchParams.set("next", next);
}
return NextResponse.redirect(loginUrl);
}
function buildCredentialsOnboardingRedirect(req: NextRequest): NextResponse {
const url = new URL("/dashboard/projects", req.url);
url.searchParams.set("onboarding", "1");
url.searchParams.set("credentials", "1");
return NextResponse.redirect(url);
}
export async function middleware(req: NextRequest) {
const { pathname } = req.nextUrl;
if (shouldBypass(pathname)) {
return NextResponse.next();
}
if (pathname.startsWith("/api/") && isPublicApi(req, pathname)) {
return NextResponse.next();
}
const token = req.cookies.get(AUTH_COOKIE_NAME)?.value || "";
const session = token ? await verifySessionToken(token) : null;
if (isPublicPage(pathname)) {
if (session) {
if (session.mustChangeCredentials) {
return buildCredentialsOnboardingRedirect(req);
}
return NextResponse.redirect(new URL("/dashboard", req.url));
}
return NextResponse.next();
}
if (!session) {
if (pathname.startsWith("/api/")) {
return NextResponse.json({ error: "Unauthorized" }, { status: 401 });
}
return buildLoginRedirect(req);
}
if (
session.mustChangeCredentials &&
pathname.startsWith("/dashboard") &&
pathname !== "/dashboard/projects"
) {
return buildCredentialsOnboardingRedirect(req);
}
if (
session.mustChangeCredentials &&
pathname === "/dashboard/projects" &&
req.nextUrl.searchParams.get("credentials") !== "1"
) {
return buildCredentialsOnboardingRedirect(req);
}
return NextResponse.next();
}
export const config = {
matcher: ["/:path*"],
};