forked from Achour/nextjs-better-auth
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproxy.ts
More file actions
25 lines (19 loc) · 812 Bytes
/
proxy.ts
File metadata and controls
25 lines (19 loc) · 812 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
import { NextRequest, NextResponse } from "next/server";
import { getSessionCookie } from "better-auth/cookies";
export async function proxy(request: NextRequest) {
const sessionCookie = getSessionCookie(request);
const { pathname } = request.nextUrl;
// Redirect authenticated users away from login/signup pages
if (sessionCookie && ["/login", "/signup"].includes(pathname)) {
return NextResponse.redirect(new URL("/dashboard", request.url));
}
// Redirect unauthenticated users trying to access protected routes
if (!sessionCookie && pathname.startsWith("/dashboard")) {
return NextResponse.redirect(new URL("/signup", request.url));
}
return NextResponse.next();
}
export const config = {
// Apply middleware to these routes
matcher: ["/dashboard", "/login", "/signup"]
};