Fix dev-mode auth: auto-authenticate on /api/auth/me
CI / docker (push) Has been cancelled
CI / build-and-test (push) Has started running

The auth/me endpoint was excluded from AuthMiddleware (as all /api/auth/
paths are), so dev-mode auto-auth never ran for it. Fixed by checking
DevMode config directly in the /me handler.

Also removed the aggressive window.location redirect from the API client
— the React router handles 401s via the RequireAuth wrapper instead.

Made-with: Cursor
This commit is contained in:
2026-04-08 19:03:55 +02:00
parent 45f026b4f4
commit e25e54b98a
2 changed files with 9 additions and 3 deletions
-1
View File
@@ -7,7 +7,6 @@ async function request<T>(path: string, options?: RequestInit): Promise<T> {
...options,
})
if (res.status === 401) {
window.location.href = '/login'
throw new Error('Unauthorized')
}
if (!res.ok) {
+9 -2
View File
@@ -37,9 +37,16 @@ public static class AuthEndpoints
return Results.Ok(new { message = "Logged out" });
});
group.MapGet("/me", async (HttpContext ctx, UserRepository userRepo) =>
group.MapGet("/me", async (HttpContext ctx, UserRepository userRepo, IConfiguration config) =>
{
var userId = ctx.GetUserId();
var userIdStr = ctx.Request.Cookies["uc_session"];
int userId = 0;
if (!string.IsNullOrEmpty(userIdStr) && int.TryParse(userIdStr, out var parsed))
userId = parsed;
else if (config.GetValue<bool>("DevMode"))
userId = 1;
if (userId == 0) return Results.Json(new { error = "unauthorized" }, statusCode: 401);
var user = await userRepo.GetUser(userId);
if (user == null) return Results.Json(new { error = "unauthorized" }, statusCode: 401);