-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproxy.ts
More file actions
26 lines (21 loc) · 939 Bytes
/
proxy.ts
File metadata and controls
26 lines (21 loc) · 939 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 proxy(request: NextRequest) {
const sessionCookie = getSessionCookie(request);
// If user is on landing page and has a session, redirect to dashboard
if (request.nextUrl.pathname === "/" && sessionCookie) {
return NextResponse.redirect(new URL("/dashboard", request.url));
}
// If user doesn't have a session and tries to access protected routes, redirect to signup
if (
!sessionCookie &&
(request.nextUrl.pathname.startsWith("/dashboard") ||
request.nextUrl.pathname.startsWith("/chat"))
) {
return NextResponse.redirect(new URL("/signup", request.url));
}
return NextResponse.next();
}
export const config = {
matcher: ["/", "/dashboard", "/chat/:path*", "/dashboard/chat/:path*"], // Include root path and specify the routes the middleware applies to
};