-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmiddleware.ts
More file actions
42 lines (35 loc) · 1.03 KB
/
middleware.ts
File metadata and controls
42 lines (35 loc) · 1.03 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
import { NextRequest, NextResponse } from "next/server"
import { getSessionCookie } from "better-auth/cookies"
/**
* Middleware for route protection
* NOTE: This is an OPTIMISTIC check (cookie presence only)
* Full validation must be done on the server/page
*/
export function middleware(req: NextRequest) {
const { pathname } = req.nextUrl
/* ---------------- PUBLIC ROUTES ---------------- */
const publicRoutes = [
"/",
"/login",
"/signup",
]
if (
publicRoutes.includes(pathname) ||
pathname.startsWith("/api/auth") ||
pathname.startsWith("/_next") ||
pathname === "/favicon.ico"
) {
return NextResponse.next()
}
/* ---------------- AUTH CHECK ---------------- */
const sessionCookie = getSessionCookie(req)
if (!sessionCookie) {
const loginUrl = new URL("/login", req.url)
loginUrl.searchParams.set("callbackUrl", pathname)
return NextResponse.redirect(loginUrl)
}
return NextResponse.next()
}
export const config = {
matcher: ["/((?!api|_next|favicon.ico).*)"],
}