Skip to content

Commit 047c2cb

Browse files
Added more logs and fixed put
1 parent e82dc94 commit 047c2cb

1 file changed

Lines changed: 53 additions & 9 deletions

File tree

backend/Program.cs

Lines changed: 53 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,8 @@
5959
policy.WithOrigins(allowedOrigins)
6060
.AllowAnyHeader()
6161
.AllowAnyMethod()
62-
.AllowCredentials();
62+
.AllowCredentials()
63+
.SetPreflightMaxAge(TimeSpan.FromMinutes(10)); // Cache preflight response
6364
});
6465
});
6566

@@ -189,11 +190,23 @@
189190
{
190191
var logger = context.RequestServices.GetRequiredService<ILogger<Program>>();
191192
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}",
193194
context.Request.Headers["Origin"],
194195
context.Request.Method,
195-
context.Request.Path
196+
context.Request.Path,
197+
context.Request.Headers.ContainsKey("Cookie")
196198
);
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+
}
197210
}
198211

199212
await next();
@@ -204,17 +217,20 @@
204217
var logger = context.RequestServices.GetRequiredService<ILogger<Program>>();
205218
var allowOrigin = context.Response.Headers["Access-Control-Allow-Origin"].ToString();
206219
var allowCredentials = context.Response.Headers["Access-Control-Allow-Credentials"].ToString();
220+
var allowMethods = context.Response.Headers["Access-Control-Allow-Methods"].ToString();
207221

208222
logger.LogInformation(
209223
"CORS Response - Origin: {Origin}, Method: {Method}, Path: {Path}, " +
210224
"Access-Control-Allow-Origin: '{AllowOrigin}', " +
211225
"Access-Control-Allow-Credentials: '{AllowCredentials}', " +
226+
"Access-Control-Allow-Methods: '{AllowMethods}', " +
212227
"Status: {StatusCode}",
213228
context.Request.Headers["Origin"],
214229
context.Request.Method,
215230
context.Request.Path,
216231
string.IsNullOrEmpty(allowOrigin) ? "(not set)" : allowOrigin,
217232
string.IsNullOrEmpty(allowCredentials) ? "(not set)" : allowCredentials,
233+
string.IsNullOrEmpty(allowMethods) ? "(not set)" : allowMethods,
218234
context.Response.StatusCode
219235
);
220236
}
@@ -251,6 +267,14 @@
251267
var config = context.RequestServices.GetRequiredService<IConfiguration>();
252268
var frontendUrl = config.GetSection("Cors:AllowedOrigins").Get<string[]>()?[0] ?? "http://localhost:3000";
253269

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+
254278
if (!context.User.Identity?.IsAuthenticated ?? true)
255279
{
256280
logger.LogWarning(
@@ -290,21 +314,34 @@
290314
);
291315
}
292316

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+
293325
// Return an HTML page that will handle the redirect and ensure cookie is set
294326
var html = $@"
295327
<!DOCTYPE html>
296328
<html>
297329
<head>
298330
<title>Authentication Successful</title>
299331
<script>
332+
console.log('OAuth callback success page loaded');
333+
console.log('Cookies:', document.cookie);
334+
300335
// Give the browser a moment to set the cookie, then redirect
301336
setTimeout(function() {{
337+
console.log('Redirecting to frontend...');
302338
window.location.href = '{frontendUrl}/auth/callback';
303-
}}, 100);
339+
}}, 500); // Increased to 500ms for better reliability
304340
</script>
305341
</head>
306342
<body>
307343
<p>Authentication successful! Redirecting...</p>
344+
<p>If you are not redirected, <a href='{frontendUrl}/auth/callback'>click here</a>.</p>
308345
</body>
309346
</html>";
310347

@@ -354,26 +391,33 @@
354391
var cookieName = "JobHelper.Auth";
355392
var hasCookie = cookieHeader.Contains(cookieName);
356393

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+
357397
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}]",
359400
isAuth,
360401
!string.IsNullOrEmpty(cookieHeader),
361402
hasCookie,
362403
context.User.Claims.Count(),
363404
context.Request.Headers["Origin"].ToString(),
364-
context.Request.Headers["Referer"].ToString()
405+
context.Request.Headers["Referer"].ToString(),
406+
string.Join(", ", allCookies)
365407
);
366408

367409
if (!isAuth)
368410
{
369411
logger.LogWarning(
370412
"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}",
372415
!string.IsNullOrEmpty(cookieHeader),
373416
hasCookie,
374417
context.User.Claims.Count(),
375418
context.Request.Path,
376-
context.Request.Headers["User-Agent"].ToString()
419+
context.Request.Headers["User-Agent"].ToString(),
420+
cookieHeader.Length
377421
);
378422
return Results.Json(new { isAuthenticated = false }, statusCode: 401);
379423
}
@@ -536,7 +580,7 @@
536580
.Produces(500);
537581

538582

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) =>
540584
{
541585
try
542586
{

0 commit comments

Comments
 (0)