-
Notifications
You must be signed in to change notification settings - Fork 44
Expand file tree
/
Copy pathproxy.ts
More file actions
47 lines (39 loc) · 1.46 KB
/
proxy.ts
File metadata and controls
47 lines (39 loc) · 1.46 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
43
44
45
46
47
// For now, we're using the simpler approach:
// 1. Sign-up redirects to /onboarding (components/auth/sign-up.tsx)
// 2. Dashboard layout checks onboardingCompleted (app/dashboard/layout.tsx)
// This works well for our current use case and is easier to maintain.
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 auth pages
if (sessionCookie && ["/sign-in", "/sign-up"].includes(pathname)) {
return NextResponse.redirect(new URL("/dashboard", request.url));
}
// Quick redirect for unauthenticated users (cookie check only)
// Note: This only checks cookie existence, not validity
// Actual authentication verification happens in server components
if (!sessionCookie) {
// Protected routes that require authentication
const protectedPaths = ["/dashboard", "/admin", "/onboarding"];
const isProtectedPath = protectedPaths.some((path) =>
pathname.startsWith(path)
);
if (isProtectedPath) {
return NextResponse.redirect(new URL("/sign-in", request.url));
}
}
return NextResponse.next();
}
export const config = {
matcher: [
"/dashboard/:path*",
"/admin/:path*",
"/onboarding",
"/settings/:path*",
"/app-ideas/:path*",
"/sign-in",
"/sign-up",
],
};