Include username in auth cookie issuance

This commit is contained in:
2026-01-29 01:32:00 +01:00
parent 637451b485
commit 307fd1abda
2 changed files with 9 additions and 3 deletions

View File

@@ -59,7 +59,7 @@ public static class AuthEndpoints
db.Players.Add(player); db.Players.Add(player);
await db.SaveChangesAsync(); await db.SaveChangesAsync();
PlayerIdentityExtensions.IssuePlayerCookie(ctx, player.Id); PlayerIdentityExtensions.IssuePlayerCookie(ctx, player.Id, player.Username);
return Results.Ok(new { player.Id, player.Username, player.DisplayName, player.IsAdmin }); return Results.Ok(new { player.Id, player.Username, player.DisplayName, player.IsAdmin });
}); });
@@ -82,7 +82,7 @@ public static class AuthEndpoints
player.LastLoginAt = DateTimeOffset.UtcNow; player.LastLoginAt = DateTimeOffset.UtcNow;
await db.SaveChangesAsync(); await db.SaveChangesAsync();
PlayerIdentityExtensions.IssuePlayerCookie(ctx, player.Id); PlayerIdentityExtensions.IssuePlayerCookie(ctx, player.Id, player.Username);
return Results.Ok(new { player.Id, player.Username, player.DisplayName, player.IsAdmin }); return Results.Ok(new { player.Id, player.Username, player.DisplayName, player.IsAdmin });
}); });

View File

@@ -5,6 +5,7 @@ namespace GameList.Infrastructure;
public static class PlayerIdentityExtensions public static class PlayerIdentityExtensions
{ {
public const string PlayerCookieName = "player"; public const string PlayerCookieName = "player";
public const string PlayerUsernameCookieName = "player_username";
public static IApplicationBuilder UsePlayerIdentity(this IApplicationBuilder app) public static IApplicationBuilder UsePlayerIdentity(this IApplicationBuilder app)
{ {
@@ -28,10 +29,14 @@ public static class PlayerIdentityExtensions
return app; return app;
} }
public static void IssuePlayerCookie(HttpContext ctx, Guid playerId) public static void IssuePlayerCookie(HttpContext ctx, Guid playerId, string? username = null)
{ {
var options = BuildCookieOptions(ctx); var options = BuildCookieOptions(ctx);
ctx.Response.Cookies.Append(PlayerCookieName, playerId.ToString(), options); ctx.Response.Cookies.Append(PlayerCookieName, playerId.ToString(), options);
if (!string.IsNullOrWhiteSpace(username))
{
ctx.Response.Cookies.Append(PlayerUsernameCookieName, username, options);
}
ctx.Items[PlayerCookieName] = playerId; ctx.Items[PlayerCookieName] = playerId;
} }
@@ -40,6 +45,7 @@ public static class PlayerIdentityExtensions
var options = BuildCookieOptions(ctx); var options = BuildCookieOptions(ctx);
options.Expires = DateTimeOffset.UtcNow.AddDays(-1); options.Expires = DateTimeOffset.UtcNow.AddDays(-1);
ctx.Response.Cookies.Append(PlayerCookieName, string.Empty, options); ctx.Response.Cookies.Append(PlayerCookieName, string.Empty, options);
ctx.Response.Cookies.Append(PlayerUsernameCookieName, string.Empty, options);
ctx.Items.Remove(PlayerCookieName); ctx.Items.Remove(PlayerCookieName);
} }