From e25e54b98a940c078e30c60ba1e563f5b1dfbf5f Mon Sep 17 00:00:00 2001 From: Rick Falkvinge Date: Wed, 8 Apr 2026 19:03:55 +0200 Subject: [PATCH] Fix dev-mode auth: auto-authenticate on /api/auth/me MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 --- frontend/src/api/client.ts | 1 - src/UsingCash.Api/Endpoints/AuthEndpoints.cs | 11 +++++++++-- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/frontend/src/api/client.ts b/frontend/src/api/client.ts index e6471887..b04d01b4 100644 --- a/frontend/src/api/client.ts +++ b/frontend/src/api/client.ts @@ -7,7 +7,6 @@ async function request(path: string, options?: RequestInit): Promise { ...options, }) if (res.status === 401) { - window.location.href = '/login' throw new Error('Unauthorized') } if (!res.ok) { diff --git a/src/UsingCash.Api/Endpoints/AuthEndpoints.cs b/src/UsingCash.Api/Endpoints/AuthEndpoints.cs index dde340e9..0ade6ad4 100644 --- a/src/UsingCash.Api/Endpoints/AuthEndpoints.cs +++ b/src/UsingCash.Api/Endpoints/AuthEndpoints.cs @@ -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("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);