- Backend gibt nur Token zurück statt fertigen Link - Frontend baut Link via window.location.origin zusammen - Zeigt korrekte Domain/Port statt internes http://127.0.0.1 Backend: - AdminEndpoint: POST /api/admin/users/{userId}/login-link gibt { token } - AuthEndpoint: GET /api/auth/autologin?token=xxx setzt Cookie + redirect - AuthMiddleware: autologin-Route von Auth ausgenommen Frontend: - api.js: apiAdminGetLoginLink() Funktion - admin-page.js: Login-as-User Button → Login-Link Button mit Modal - form.css: Styling für Login-Link Modal
48 lines
1.4 KiB
C#
48 lines
1.4 KiB
C#
using System.Threading.Tasks;
|
|
using Gott.Services;
|
|
using Microsoft.AspNetCore.Http;
|
|
|
|
namespace Gott.Middleware;
|
|
|
|
public class AuthMiddleware
|
|
{
|
|
private readonly RequestDelegate _next;
|
|
|
|
public AuthMiddleware(RequestDelegate next)
|
|
{
|
|
_next = next;
|
|
}
|
|
|
|
public async Task InvokeAsync(HttpContext ctx, AuthService auth, DatabaseService db)
|
|
{
|
|
var path = ctx.Request.Path.Value ?? "";
|
|
|
|
// Static files, login and telegram webhook don't require auth
|
|
var isTelegramWebhook = path.StartsWith("/api/telegram/webhook/") && ctx.Request.Method == "POST";
|
|
var isSharedGet = path.StartsWith("/api/shared/") && ctx.Request.Method == "GET";
|
|
var isAutologin = path == "/api/auth/autologin" && ctx.Request.Method == "GET";
|
|
if (!path.StartsWith("/api/") || path == "/api/auth/login" || isTelegramWebhook || isSharedGet || isAutologin)
|
|
{
|
|
await _next(ctx);
|
|
return;
|
|
}
|
|
|
|
var token = ctx.Request.Cookies["gott_token"];
|
|
var userId = auth.ValidateToken(token);
|
|
|
|
if (userId == null)
|
|
{
|
|
ctx.Response.StatusCode = 401;
|
|
await ctx.Response.WriteAsJsonAsync(new { error = "Not authenticated" });
|
|
return;
|
|
}
|
|
|
|
ctx.Items["UserId"] = userId;
|
|
|
|
var user = await db.GetUser(userId);
|
|
ctx.Items["IsAdmin"] = user?.IsAdmin ?? false;
|
|
|
|
await _next(ctx);
|
|
}
|
|
}
|