@@ -118,34 +118,45 @@ local function parse_cookies(cookie_string)
118118 return cookies
119119end
120120
121+ -- Allow OPTIONS requests to pass through without authentication for CORS preflight
122+ if ngx .var .request_method == " OPTIONS" then
123+ ngx .log (ngx .INFO , " 🚀 Line 105 - auth.lua:main() - Allowing OPTIONS preflight request: " , ngx .var .request_uri )
124+ return ngx .exit (ngx .HTTP_OK )
125+ end
126+
121127-- get request headers
122128local headers = ngx .req .get_headers ()
123129local bypass_header_value = headers [BYPASS_HEADER ]
124130
125131if ALLOW_BYPASS == " true" and bypass_header_value and bypass_header_value == BYPASS_HEADER_VALUE then
126- ngx .log (ngx .INFO , " Bypassing auth for request: " , ngx .var .request_uri )
132+ ngx .log (ngx .INFO , " 🔓 Line 112 - auth.lua:main() - Bypassing auth for request: " , ngx .var .request_uri )
127133 return ngx .exit (ngx .HTTP_OK )
128134end
129135
130136local cookies = parse_cookies (ngx .var .http_cookie )
131137local token = cookies [JWT_SALT ]
132138
133139if not token then
140+ ngx .log (ngx .ERR , " ❌ Line 119 - auth.lua:main() - No JWT token found in cookies for request: " , ngx .var .request_uri )
134141 return ngx .exit (ngx .HTTP_UNAUTHORIZED )
135142end
136143
137144local jwt_obj = validate_jwt (token )
138145if not jwt_obj then
146+ ngx .log (ngx .ERR , " ❌ Line 125 - auth.lua:validate_jwt() - Invalid JWT token for request: " , ngx .var .request_uri )
139147 return ngx .exit (ngx .HTTP_UNAUTHORIZED )
140148end
141149
142150local user_id = jwt_obj .payload .sub
143151
144152-- we need to set user_id in the request headers
145153ngx .req .set_header (" X-User-Id" , user_id )
154+ ngx .log (ngx .INFO , " ✅ Line 132 - auth.lua:main() - Authentication successful for user: " , user_id , " request: " , ngx .var .request_uri )
146155
147156if ENABLE_DB_CHECK == " true" then
148157 if not check_user (user_id ) then
158+ ngx .log (ngx .ERR , " ❌ Line 136 - auth.lua:check_user() - User not found in database: " , user_id , " request: " , ngx .var .request_uri )
149159 return ngx .exit (ngx .HTTP_FORBIDDEN )
150160 end
161+ ngx .log (ngx .INFO , " ✅ Line 139 - auth.lua:check_user() - Database check passed for user: " , user_id )
151162end
0 commit comments