-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmiddleware.ts
More file actions
28 lines (21 loc) · 1.01 KB
/
Copy pathmiddleware.ts
File metadata and controls
28 lines (21 loc) · 1.01 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
import { NextResponse } from "next/server"
import type { NextRequest } from "next/server"
const PROTECTED = ["/dashboard", "/settings", "/rewards"]
const AUTH_PAGES = ["/login", "/register"]
const SESSION_COOKIE = "better-auth.session_token"
const SECURE_SESSION_COOKIE = "__Secure-better-auth.session_token"
export function middleware(request: NextRequest) {
const { pathname } = request.nextUrl
const isAuth =
!!request.cookies.get(SESSION_COOKIE)?.value ||
!!request.cookies.get(SECURE_SESSION_COOKIE)?.value
if (PROTECTED.some((p) => pathname.startsWith(p)) && !isAuth)
return NextResponse.redirect(new URL("/login", request.url))
if (AUTH_PAGES.some((p) => pathname.startsWith(p)) && isAuth)
return NextResponse.redirect(new URL("/dashboard", request.url))
if (pathname === "/" && isAuth) return NextResponse.redirect(new URL("/dashboard", request.url))
return NextResponse.next()
}
export const config = {
matcher: ["/((?!api|_next/static|_next/image|favicon.ico|.*\\.(?:png|svg|ico)).*)"],
}