JWT-Authentifizierung & GameState-Verbesserungen
Neuer AuthController mit Endpunkten für Registrierung, Login und Nutzerinfo (/register, /login, /me) implementiert. Auth-spezifische DTOs hinzugefügt. GameHub mit [Authorize] geschützt und Spielteilnahme validiert. GameService liefert jetzt Rundeninfos, aktuelle Rundennummer und verbleibende Rundendauer. Migrations-Ordner im Projekt angelegt.
This commit is contained in:
@@ -1,3 +1,4 @@
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.SignalR;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using SlipItIn.Server.Data;
|
||||
@@ -7,6 +8,7 @@ using System.Security.Claims;
|
||||
|
||||
namespace SlipItIn.Server.Hubs;
|
||||
|
||||
[Authorize]
|
||||
public class GameHub : Hub
|
||||
{
|
||||
private readonly IGameService _gameService;
|
||||
@@ -43,6 +45,14 @@ public class GameHub : Hub
|
||||
throw new UnauthorizedAccessException("Player does not belong to authenticated user");
|
||||
}
|
||||
|
||||
private async Task ValidateUserInGameAsync(int gameId, int authUserId)
|
||||
{
|
||||
using var context = _contextFactory.CreateDbContext();
|
||||
var isParticipant = await context.Players.AnyAsync(p => p.GameId == gameId && p.UserId == authUserId);
|
||||
if (!isParticipant)
|
||||
throw new UnauthorizedAccessException("User is not part of this game");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Speichert die ConnectionId für einen Spieler in der Datenbank
|
||||
/// </summary>
|
||||
@@ -329,9 +339,16 @@ public class GameHub : Hub
|
||||
{
|
||||
try
|
||||
{
|
||||
var userId = GetAuthenticatedUserId();
|
||||
await ValidateUserInGameAsync(gameId, userId);
|
||||
|
||||
var gameState = await _gameService.GetGameStateAsync(gameId);
|
||||
await Clients.Caller.SendAsync("GameStateUpdated", gameState);
|
||||
}
|
||||
catch (UnauthorizedAccessException ex)
|
||||
{
|
||||
await Clients.Caller.SendAsync("Error", new { Message = ex.Message });
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Error requesting game state");
|
||||
|
||||
Reference in New Issue
Block a user