-
-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathmiddleware.ts
More file actions
53 lines (47 loc) · 1.83 KB
/
Copy pathmiddleware.ts
File metadata and controls
53 lines (47 loc) · 1.83 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
48
49
50
51
52
53
import { NextRequest, NextResponse } from "next/server";
import { getSessionCookie } from "better-auth/cookies";
export async function middleware(request: NextRequest) {
try {
const { pathname } = request.nextUrl;
// Always allow access to sign-up and sign-in pages - let them handle their own logic
if (["/sign-in", "/sign-up"].includes(pathname)) {
return NextResponse.next();
}
// Check for session cookie - Better Auth uses "better-auth.session_token" by default
let hasSession = false;
try {
// Try the Better Auth helper first
const sessionCookie = getSessionCookie(request);
hasSession = !!sessionCookie;
} catch (error) {
// Fallback: check for Better Auth cookie directly
const cookies = request.cookies;
// Check common Better Auth cookie names
hasSession = cookies.has("better-auth.session_token") ||
cookies.has("better-auth.session");
}
// For dashboard routes: redirect unauthenticated users to sign-up
if (pathname.startsWith("/dashboard")) {
if (!hasSession) {
const signUpUrl = new URL("/sign-up", request.url);
// Preserve the original path as a redirect parameter if needed
if (pathname !== "/dashboard") {
signUpUrl.searchParams.set("redirect", pathname);
}
return NextResponse.redirect(signUpUrl);
}
}
return NextResponse.next();
} catch (error) {
// If there's an error, allow access to auth pages but redirect dashboard to sign-up
const { pathname } = request.nextUrl;
if (pathname.startsWith("/dashboard")) {
return NextResponse.redirect(new URL("/sign-up", request.url));
}
// Allow other pages to continue
return NextResponse.next();
}
}
export const config = {
matcher: ["/dashboard/:path*", "/sign-in", "/sign-up"],
};