@@ -4,6 +4,43 @@ import { isEnvEnabled, isDebugFlag } from "./app/_utils/env-utils";
44
55const debugProxy = isDebugFlag ( "proxy" ) ;
66
7+ const SESSION_CACHE_TTL = 10_000 ;
8+ const sessionCache = new Map < string , { valid : boolean ; ts : number } > ( ) ;
9+
10+ const _checkSession = async ( sessionId : string , internalApiUrl : string , cookie : string ) => {
11+ const cached = sessionCache . get ( sessionId ) ;
12+ if ( cached && Date . now ( ) - cached . ts < SESSION_CACHE_TTL ) return cached . valid ;
13+
14+ const sessionCheckUrl = new URL ( `${ internalApiUrl } /api/auth/check-session` ) ;
15+
16+ if ( debugProxy ) {
17+ console . log ( "MIDDLEWARE - Session Check URL:" , sessionCheckUrl . href ) ;
18+ }
19+
20+ const res = await fetch ( sessionCheckUrl , {
21+ headers : { Cookie : cookie } ,
22+ cache : "no-store" ,
23+ } ) ;
24+
25+ if ( debugProxy ) {
26+ console . log ( "MIDDLEWARE - Session Check Response:" ) ;
27+ console . log ( " status:" , res . status ) ;
28+ console . log ( " statusText:" , res . statusText ) ;
29+ console . log ( " ok:" , res . ok ) ;
30+ }
31+
32+ sessionCache . set ( sessionId , { valid : res . ok , ts : Date . now ( ) } ) ;
33+
34+ if ( sessionCache . size > 1000 ) {
35+ const now = Date . now ( ) ;
36+ sessionCache . forEach ( ( val , key ) => {
37+ if ( now - val . ts > SESSION_CACHE_TTL ) sessionCache . delete ( key ) ;
38+ } ) ;
39+ }
40+
41+ return res . ok ;
42+ } ;
43+
744export const proxy = async ( request : NextRequest ) => {
845 const { pathname } = request . nextUrl ;
946
@@ -61,27 +98,13 @@ export const proxy = async (request: NextRequest) => {
6198 console . log ( " → Using:" , internalApiUrl ) ;
6299 }
63100
64- const sessionCheckUrl = new URL ( `${ internalApiUrl } /api/auth/check-session` ) ;
65-
66- if ( debugProxy ) {
67- console . log ( "MIDDLEWARE - Session Check URL:" , sessionCheckUrl . href ) ;
68- }
69-
70- const sessionCheck = await fetch ( sessionCheckUrl , {
71- headers : {
72- Cookie : request . headers . get ( "Cookie" ) || "" ,
73- } ,
74- cache : "no-store" ,
75- } ) ;
76-
77- if ( debugProxy ) {
78- console . log ( "MIDDLEWARE - Session Check Response:" ) ;
79- console . log ( " status:" , sessionCheck . status ) ;
80- console . log ( " statusText:" , sessionCheck . statusText ) ;
81- console . log ( " ok:" , sessionCheck . ok ) ;
82- }
101+ const valid = await _checkSession (
102+ sessionId ,
103+ internalApiUrl ,
104+ request . headers . get ( "Cookie" ) || "" ,
105+ ) ;
83106
84- if ( ! sessionCheck . ok ) {
107+ if ( ! valid ) {
85108 const redirectResponse = NextResponse . redirect ( loginUrl ) ;
86109 redirectResponse . cookies . delete ( cookieName ) ;
87110
@@ -102,6 +125,6 @@ export const proxy = async (request: NextRequest) => {
102125
103126export const config = {
104127 matcher : [
105- "/((?!_next/static|_next/image|favicon.ico|site.webmanifest|sw.js|app-icons|app-screenshots|flags|fonts|images|repo-images|themes|openapi.yaml|~offline ).*)" ,
128+ "/((?!_next/static|_next/image|favicon.ico|site.webmanifest|sw.js|app-icons|app-screenshots|flags|fonts|images|repo-images|themes|openapi.yaml).*)" ,
106129 ] ,
107130} ;
0 commit comments