-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmiddleware.ts
More file actions
27 lines (19 loc) · 776 Bytes
/
middleware.ts
File metadata and controls
27 lines (19 loc) · 776 Bytes
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
import { NextResponse, type NextRequest } from "next/server"
export function middleware(req: NextRequest) {
const pathname = req.nextUrl.pathname
if (pathname.startsWith("/api")) return NextResponse.next()
const sessionToken = req.cookies.get("ai_terminal_session")?.value
const authed = Boolean(sessionToken)
const isLogin = pathname === "/login"
const isProtected = pathname === "/" || pathname.startsWith("/settings") || pathname.startsWith("/admin")
if (authed && isLogin) {
return NextResponse.redirect(new URL("/", req.url))
}
if (!authed && isProtected) {
return NextResponse.redirect(new URL("/login", req.url))
}
return NextResponse.next()
}
export const config = {
matcher: ["/((?!_next/static|_next/image|favicon.ico).*)"]
}