1- import { NextResponse } from "next/server " ;
1+ import type { CloudflareSessionResponse } from "better-auth-cloudflare " ;
22import type { NextRequest } from "next/server" ;
3- import { initAuth } from "@/auth" ; // Adjust if your auth init is elsewhere
3+ import { NextResponse } from "next/server" ;
44
55export async function middleware ( request : NextRequest ) {
66 const { pathname } = request . nextUrl ;
77
8- // Protect the /dashboard route
9- if ( pathname . startsWith ( "/dashboard" ) ) {
8+ // Routes that require authentication
9+ const protectedRoutes = [ "/dashboard" ] ;
10+ // Routes that should redirect to dashboard if already authenticated
11+ const authRoutes = [ "/" , "/sign-in" ] ;
12+
13+ const isProtectedRoute = protectedRoutes . some ( route => pathname . startsWith ( route ) ) ;
14+ const isAuthRoute = authRoutes . includes ( pathname ) ;
15+
16+ // Only check session for routes that need auth logic
17+ if ( isProtectedRoute || isAuthRoute ) {
1018 try {
11- const authInstance = await initAuth ( ) ;
12- const session = await authInstance . api . getSession ( { headers : request . headers } ) ;
19+ // Use the auth API route instead of importing better-auth directly
20+ // This avoids Edge Runtime dynamic code evaluation issues with @opennextjs /cloudflare
21+ const sessionResponse = await fetch ( new URL ( "/api/auth/get-session" , request . url ) , {
22+ method : "GET" ,
23+ headers : {
24+ cookie : request . headers . get ( "cookie" ) || "" ,
25+ } ,
26+ } ) ;
27+
28+ const isAuthenticated = sessionResponse . ok ;
29+ let sessionData : CloudflareSessionResponse | null = null ;
1330
14- if ( ! session ) {
15- // User is not authenticated, redirect to home page
31+ if ( isAuthenticated ) {
32+ try {
33+ sessionData = await sessionResponse . json ( ) ;
34+ // Double-check that we have a valid session
35+ if ( ! sessionData ?. session || ! sessionData . session . userId ) {
36+ sessionData = null ;
37+ }
38+ } catch {
39+ sessionData = null ;
40+ }
41+ }
42+
43+ // Handle protected routes - redirect to home if not authenticated
44+ if ( isProtectedRoute && ! sessionData ) {
1645 const url = request . nextUrl . clone ( ) ;
1746 url . pathname = "/" ;
1847 return NextResponse . redirect ( url ) ;
1948 }
49+
50+ // Handle auth routes - redirect to dashboard if already authenticated
51+ if ( isAuthRoute && sessionData ) {
52+ const url = request . nextUrl . clone ( ) ;
53+ url . pathname = "/dashboard" ;
54+ return NextResponse . redirect ( url ) ;
55+ }
56+
57+ // Optional: Log geolocation data for authenticated users
58+ if ( sessionData ) {
59+ console . log ( "Authenticated request from:" , {
60+ country : sessionData . session . country ,
61+ city : sessionData . session . city ,
62+ timezone : sessionData . session . timezone ,
63+ } ) ;
64+ }
2065 } catch ( error ) {
2166 console . error ( "Middleware error:" , error ) ;
22- // Optional: redirect to an error page or home on error
23- const url = request . nextUrl . clone ( ) ;
24- url . pathname = "/" ; // Or an error page like '/auth-error'
25- return NextResponse . redirect ( url ) ;
67+
68+ // On error, only redirect protected routes to avoid redirect loops
69+ if ( isProtectedRoute ) {
70+ const url = request . nextUrl . clone ( ) ;
71+ url . pathname = "/" ;
72+ return NextResponse . redirect ( url ) ;
73+ }
2674 }
2775 }
2876
@@ -32,5 +80,7 @@ export async function middleware(request: NextRequest) {
3280export const config = {
3381 matcher : [
3482 "/dashboard/:path*" , // Protects /dashboard and all its sub-routes
83+ "/" , // Home page - redirect to dashboard if authenticated
84+ "/sign-in" , // Sign-in page - redirect to dashboard if authenticated
3585 ] ,
3686} ;
0 commit comments