@@ -11,6 +11,7 @@ import { getIdToken } from "../utils/getIdToken";
1111import { OAuth2CodeExchangeResponse } from "@kinde-oss/kinde-typescript-sdk" ;
1212import { copyCookiesToRequest } from "../utils/copyCookiesToRequest" ;
1313import { getStandardCookieOptions } from "../utils/cookies/getStandardCookieOptions" ;
14+ import { isPublicPathMatch } from "../utils/isPublicPathMatch" ;
1415
1516const handleMiddleware = async ( req , options , onSuccess ) => {
1617 const { pathname } = req . nextUrl ;
@@ -53,34 +54,9 @@ const handleMiddleware = async (req, options, onSuccess) => {
5354 ? `${ loginPage } ?${ queryString } `
5455 : loginPage ;
5556
56- const isPublicPath = publicPaths . some ( ( p : string | RegExp ) => {
57- try {
58- // Handle RegExp objects
59- if ( p instanceof RegExp ) {
60- // Reset lastIndex to avoid global/sticky flag state issues
61- if ( p . global || p . sticky ) {
62- p . lastIndex = 0 ;
63- }
64- return p . test ( pathname ) ;
65- }
66-
67- // Handle string patterns (existing logic)
68- // explicit root path handling
69- // if we use startsWith and "/" is provided as a publicPath,
70- // we inadvertently match all paths because they all start with "/"
71- if ( p === "/" ) return pathname === "/" ;
72- return pathname . startsWith ( p ) ;
73- } catch ( error ) {
74- // Handle regex evaluation errors gracefully
75- if ( config . isDebugMode ) {
76- console . error (
77- `authMiddleware: error evaluating publicPath pattern:` ,
78- error ,
79- ) ;
80- }
81- return false ;
82- }
83- } ) ;
57+ // Use extracted utility for public path matching
58+ // eslint-disable-next-line @typescript-eslint/no-var-requires
59+ const isPublicPath = isPublicPathMatch ( pathname , publicPaths , config . isDebugMode ) ;
8460
8561 // getAccessToken will validate the token
8662 let kindeAccessToken = await getAccessToken ( req ) ;
@@ -109,7 +85,7 @@ const handleMiddleware = async (req, options, onSuccess) => {
10985 console . log ( "authMiddleware: access token expired, refreshing" ) ;
11086 }
11187
112- const sendResult = ( debugMessage : string ) => {
88+ const sendResult = ( debugMessage : string ) : NextResponse | undefined => {
11389 if ( config . isDebugMode ) {
11490 console . error ( debugMessage ) ;
11591 }
@@ -121,6 +97,7 @@ const handleMiddleware = async (req, options, onSuccess) => {
12197 ) ,
12298 ) ;
12399 }
100+ return undefined ;
124101 } ;
125102
126103 try {
@@ -135,7 +112,8 @@ const handleMiddleware = async (req, options, onSuccess) => {
135112 ) ;
136113 }
137114 } catch ( error ) {
138- return sendResult ( "authMiddleware: error refreshing tokens" ) ;
115+ const result = sendResult ( "authMiddleware: error refreshing tokens" ) ;
116+ if ( result ) return result ;
139117 }
140118
141119 try {
@@ -173,7 +151,8 @@ const handleMiddleware = async (req, options, onSuccess) => {
173151 console . log ( "authMiddleware: tokens refreshed and cookies updated" ) ;
174152 }
175153 } catch ( error ) {
176- sendResult ( "authMiddleware: error settings new token in cookie" ) ;
154+ const result = sendResult ( "authMiddleware: error settings new token in cookie" ) ;
155+ if ( result ) return result ;
177156 }
178157 }
179158
0 commit comments