-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmiddleware.ts
More file actions
26 lines (20 loc) · 849 Bytes
/
middleware.ts
File metadata and controls
26 lines (20 loc) · 849 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 { NextRequest, NextResponse } from "next/server";
import { getSessionCookie } from "better-auth/cookies";
export async function middleware(request: NextRequest) {
const sessionCookie = getSessionCookie(request);
const { pathname } = request.nextUrl;
const isAuthPage = pathname.startsWith("/auth") || pathname.startsWith("/login");
const isProtectedPage = !pathname.startsWith("/api") &&
!isAuthPage &&
pathname !== "/";
if (!sessionCookie && isProtectedPage) {
return NextResponse.redirect(new URL("/auth/signin", request.url));
}
if (sessionCookie && isAuthPage) {
return NextResponse.redirect(new URL("/dashboard", request.url));
}
return NextResponse.next();
}
export const config = {
matcher: ["/dashboard/:path*", "/auth/:path*", "/login/:path*"],
};