|
59 | 59 | policy.WithOrigins(allowedOrigins) |
60 | 60 | .AllowAnyHeader() |
61 | 61 | .AllowAnyMethod() |
62 | | - .AllowCredentials(); |
| 62 | + .AllowCredentials() |
| 63 | + .SetPreflightMaxAge(TimeSpan.FromMinutes(10)); // Cache preflight response |
63 | 64 | }); |
64 | 65 | }); |
65 | 66 |
|
|
189 | 190 | { |
190 | 191 | var logger = context.RequestServices.GetRequiredService<ILogger<Program>>(); |
191 | 192 | logger.LogInformation( |
192 | | - "CORS Request received - Origin: {Origin}, Method: {Method}, Path: {Path}", |
| 193 | + "CORS Request received - Origin: {Origin}, Method: {Method}, Path: {Path}, Has Credentials: {HasCredentials}", |
193 | 194 | context.Request.Headers["Origin"], |
194 | 195 | context.Request.Method, |
195 | | - context.Request.Path |
| 196 | + context.Request.Path, |
| 197 | + context.Request.Headers.ContainsKey("Cookie") |
196 | 198 | ); |
| 199 | + |
| 200 | + // Special logging for OPTIONS requests (preflight) |
| 201 | + if (context.Request.Method == "OPTIONS") |
| 202 | + { |
| 203 | + logger.LogInformation( |
| 204 | + "CORS Preflight (OPTIONS) - Origin: {Origin}, Access-Control-Request-Method: {RequestMethod}, Access-Control-Request-Headers: {RequestHeaders}", |
| 205 | + context.Request.Headers["Origin"], |
| 206 | + context.Request.Headers["Access-Control-Request-Method"], |
| 207 | + context.Request.Headers["Access-Control-Request-Headers"] |
| 208 | + ); |
| 209 | + } |
197 | 210 | } |
198 | 211 |
|
199 | 212 | await next(); |
|
204 | 217 | var logger = context.RequestServices.GetRequiredService<ILogger<Program>>(); |
205 | 218 | var allowOrigin = context.Response.Headers["Access-Control-Allow-Origin"].ToString(); |
206 | 219 | var allowCredentials = context.Response.Headers["Access-Control-Allow-Credentials"].ToString(); |
| 220 | + var allowMethods = context.Response.Headers["Access-Control-Allow-Methods"].ToString(); |
207 | 221 |
|
208 | 222 | logger.LogInformation( |
209 | 223 | "CORS Response - Origin: {Origin}, Method: {Method}, Path: {Path}, " + |
210 | 224 | "Access-Control-Allow-Origin: '{AllowOrigin}', " + |
211 | 225 | "Access-Control-Allow-Credentials: '{AllowCredentials}', " + |
| 226 | + "Access-Control-Allow-Methods: '{AllowMethods}', " + |
212 | 227 | "Status: {StatusCode}", |
213 | 228 | context.Request.Headers["Origin"], |
214 | 229 | context.Request.Method, |
215 | 230 | context.Request.Path, |
216 | 231 | string.IsNullOrEmpty(allowOrigin) ? "(not set)" : allowOrigin, |
217 | 232 | string.IsNullOrEmpty(allowCredentials) ? "(not set)" : allowCredentials, |
| 233 | + string.IsNullOrEmpty(allowMethods) ? "(not set)" : allowMethods, |
218 | 234 | context.Response.StatusCode |
219 | 235 | ); |
220 | 236 | } |
|
251 | 267 | var config = context.RequestServices.GetRequiredService<IConfiguration>(); |
252 | 268 | var frontendUrl = config.GetSection("Cors:AllowedOrigins").Get<string[]>()?[0] ?? "http://localhost:3000"; |
253 | 269 |
|
| 270 | + // Log the authentication state BEFORE processing |
| 271 | + logger.LogInformation( |
| 272 | + "OAuth callback received. IsAuthenticated: {IsAuth}, Request scheme: {Scheme}, Host: {Host}", |
| 273 | + context.User.Identity?.IsAuthenticated ?? false, |
| 274 | + context.Request.Scheme, |
| 275 | + context.Request.Host |
| 276 | + ); |
| 277 | + |
254 | 278 | if (!context.User.Identity?.IsAuthenticated ?? true) |
255 | 279 | { |
256 | 280 | logger.LogWarning( |
|
290 | 314 | ); |
291 | 315 | } |
292 | 316 |
|
| 317 | + // Log the cookies that will be set |
| 318 | + var setCookieHeaders = context.Response.Headers["Set-Cookie"].ToArray(); |
| 319 | + logger.LogInformation( |
| 320 | + "OAuth callback setting cookies. Set-Cookie headers count: {Count}, Values: [{Cookies}]", |
| 321 | + setCookieHeaders.Length, |
| 322 | + string.Join(" | ", setCookieHeaders.Select(h => h != null ? h.Substring(0, Math.Min(100, h.Length)) + "..." : "null")) |
| 323 | + ); |
| 324 | + |
293 | 325 | // Return an HTML page that will handle the redirect and ensure cookie is set |
294 | 326 | var html = $@" |
295 | 327 | <!DOCTYPE html> |
296 | 328 | <html> |
297 | 329 | <head> |
298 | 330 | <title>Authentication Successful</title> |
299 | 331 | <script> |
| 332 | + console.log('OAuth callback success page loaded'); |
| 333 | + console.log('Cookies:', document.cookie); |
| 334 | + |
300 | 335 | // Give the browser a moment to set the cookie, then redirect |
301 | 336 | setTimeout(function() {{ |
| 337 | + console.log('Redirecting to frontend...'); |
302 | 338 | window.location.href = '{frontendUrl}/auth/callback'; |
303 | | - }}, 100); |
| 339 | + }}, 500); // Increased to 500ms for better reliability |
304 | 340 | </script> |
305 | 341 | </head> |
306 | 342 | <body> |
307 | 343 | <p>Authentication successful! Redirecting...</p> |
| 344 | + <p>If you are not redirected, <a href='{frontendUrl}/auth/callback'>click here</a>.</p> |
308 | 345 | </body> |
309 | 346 | </html>"; |
310 | 347 |
|
|
354 | 391 | var cookieName = "JobHelper.Auth"; |
355 | 392 | var hasCookie = cookieHeader.Contains(cookieName); |
356 | 393 |
|
| 394 | + // Parse and log all cookies |
| 395 | + var allCookies = context.Request.Cookies.Select(c => $"{c.Key}={c.Value.Substring(0, Math.Min(20, c.Value.Length))}...").ToList(); |
| 396 | + |
357 | 397 | logger.LogInformation( |
358 | | - "Auth check - IsAuthenticated: {IsAuth}, Cookie Present: {CookiePresent}, Has JobHelper Cookie: {HasCookie}, Claims Count: {ClaimsCount}, Origin: {Origin}, Referer: {Referer}", |
| 398 | + "Auth check - IsAuthenticated: {IsAuth}, Cookie Present: {CookiePresent}, Has JobHelper Cookie: {HasCookie}, " + |
| 399 | + "Claims Count: {ClaimsCount}, Origin: {Origin}, Referer: {Referer}, All Cookies: [{Cookies}]", |
359 | 400 | isAuth, |
360 | 401 | !string.IsNullOrEmpty(cookieHeader), |
361 | 402 | hasCookie, |
362 | 403 | context.User.Claims.Count(), |
363 | 404 | context.Request.Headers["Origin"].ToString(), |
364 | | - context.Request.Headers["Referer"].ToString() |
| 405 | + context.Request.Headers["Referer"].ToString(), |
| 406 | + string.Join(", ", allCookies) |
365 | 407 | ); |
366 | 408 |
|
367 | 409 | if (!isAuth) |
368 | 410 | { |
369 | 411 | logger.LogWarning( |
370 | 412 | "Authentication check failed - user not authenticated. " + |
371 | | - "Cookie Present: {CookiePresent}, Has JobHelper Cookie: {HasCookie}, Claims Count: {ClaimsCount}, Request Path: {RequestPath}, User Agent: {UserAgent}", |
| 413 | + "Cookie Present: {CookiePresent}, Has JobHelper Cookie: {HasCookie}, Claims Count: {ClaimsCount}, " + |
| 414 | + "Request Path: {RequestPath}, User Agent: {UserAgent}, Cookie Header Length: {CookieLength}", |
372 | 415 | !string.IsNullOrEmpty(cookieHeader), |
373 | 416 | hasCookie, |
374 | 417 | context.User.Claims.Count(), |
375 | 418 | context.Request.Path, |
376 | | - context.Request.Headers["User-Agent"].ToString() |
| 419 | + context.Request.Headers["User-Agent"].ToString(), |
| 420 | + cookieHeader.Length |
377 | 421 | ); |
378 | 422 | return Results.Json(new { isAuthenticated = false }, statusCode: 401); |
379 | 423 | } |
|
536 | 580 | .Produces(500); |
537 | 581 |
|
538 | 582 |
|
539 | | -app.MapPut("/api/resumes/{resumeId:guid}", async (Guid resumeId, Resume resume, IResumeService resumeService) => |
| 583 | +app.MapPut("/api/resumes/update/{resumeId:guid}", async (Guid resumeId, Resume resume, IResumeService resumeService) => |
540 | 584 | { |
541 | 585 | try |
542 | 586 | { |
|
0 commit comments