-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathmiddleware.ts
More file actions
68 lines (59 loc) · 1.91 KB
/
middleware.ts
File metadata and controls
68 lines (59 loc) · 1.91 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
import { auth } from "@/lib/auth";
import { NextResponse } from "next/server";
export default auth((req) => {
const { nextUrl } = req;
const isLoggedIn = !!req.auth;
// Public routes (no login required)
const publicRoutes = [
"/login",
"/register",
"/api/auth",
"/api/templates",
"/templates",
];
// Internal service-to-service APIs (verified via INTERNAL_API_SECRET; bypass NextAuth)
// These APIs are called by ai-server and do not require user login, but have their own secret verification.
if (nextUrl.pathname.startsWith("/api/internal")) {
return NextResponse.next();
}
// Check whether this is a public route
const isPublicRoute = publicRoutes.some(route =>
nextUrl.pathname.startsWith(route)
) || nextUrl.pathname === "/";
// Static assets and most API routes are not protected
if (
nextUrl.pathname.startsWith("/_next") ||
nextUrl.pathname.startsWith("/favicon") ||
nextUrl.pathname.includes(".")
) {
return NextResponse.next();
}
// If public route, allow
if (isPublicRoute) {
// If a logged-in user visits login/register, redirect to dashboard
if (isLoggedIn && (nextUrl.pathname === "/login" || nextUrl.pathname === "/register")) {
return NextResponse.redirect(new URL("/dashboard", nextUrl));
}
return NextResponse.next();
}
// If not logged in and visiting a protected page, redirect to login
if (!isLoggedIn) {
const callbackUrl = encodeURIComponent(nextUrl.pathname + nextUrl.search);
return NextResponse.redirect(
new URL(`/login?callbackUrl=${callbackUrl}`, nextUrl)
);
}
return NextResponse.next();
});
export const config = {
matcher: [
/*
* Match all paths except:
* - api/auth (NextAuth API routes)
* - _next/static (static files)
* - _next/image (image optimization)
* - favicon.ico
*/
"/((?!api/auth|_next/static|_next/image|favicon.ico).*)",
],
};