Fix dev-mode auth: auto-authenticate on /api/auth/me
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:
@@ -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) {
|
||||
|
||||
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user