Single-session login policy + fix deck editor card search

Auth (single-session policy):
- AuthGrpcHandler.Login now calls RevokeAllSessionsForUserAsync before
  CreateSessionAsync. When a user logs in on a new device, all existing
  sessions are revoked in Redis (token keys deleted) and marked RevokedAt
  in Postgres. The old device will fail auth.hello on its next reconnect
  and return to the login screen automatically.

Admin (PrebuiltDeckEditor):
- SearchCardsAsync and SearchArchSpiritsAsync were using .Contains() which
  compiles to a case-sensitive PostgreSQL LIKE. Changed to EF.Functions.ILike
  so card names can be found regardless of capitalisation.

Generated with [Claude Code](https://claude.ai/code)
via [Happy](https://happy.engineering)

Co-Authored-By: Claude <noreply@anthropic.com>
Co-Authored-By: Happy <yesreply@happy.engineering>
This commit is contained in:
dingo
2026-02-28 21:52:36 -05:00
co-authored by Claude Happy
parent 775cf65899
commit 10c0593120
2 changed files with 40 additions and 2 deletions
@@ -294,8 +294,9 @@ else
private async Task SearchCardsAsync()
{
if (string.IsNullOrWhiteSpace(_cardSearch)) return;
var term = _cardSearch.Trim();
_cardResults = await CatalogDb.CardDefinitions
.Where(c => c.Name.Contains(_cardSearch))
.Where(c => EF.Functions.ILike(c.Name, $"%{term}%"))
.OrderBy(c => c.Name)
.Take(10)
.ToListAsync();
@@ -304,8 +305,9 @@ else
private async Task SearchArchSpiritsAsync()
{
if (string.IsNullOrWhiteSpace(_archSearch)) return;
var term = _archSearch.Trim();
_archResults = await CatalogDb.CardDefinitions
.Where(c => c.CardType == CardType.ArchSpirit && c.Name.Contains(_archSearch))
.Where(c => c.CardType == CardType.ArchSpirit && EF.Functions.ILike(c.Name, $"%{term}%"))
.OrderBy(c => c.Name)
.Take(10)
.ToListAsync();
@@ -72,6 +72,11 @@ public sealed class AuthGrpcHandler : AuthService.AuthServiceBase
user.LastLoginAt = DateTimeOffset.UtcNow;
await _userManager.UpdateAsync(user);
// Single-session policy: revoke all active sessions before issuing a new one.
// This ensures only one device is logged in at a time. The old device will
// receive an auth failure on its next auth.hello and return to the login screen.
await RevokeAllSessionsForUserAsync(user.Id);
var token = await CreateSessionAsync(user.Id, context);
_logger.LogInformation("Login succeeded for account {AccountId}", user.Id);
return new LoginResponse
@@ -348,6 +353,37 @@ public sealed class AuthGrpcHandler : AuthService.AuthServiceBase
// ── Helpers ───────────────────────────────────────────────────────────────
/// <summary>
/// Revokes all active sessions for the given account in both Redis and Postgres.
/// Called before issuing a new session token so only one device is ever logged in.
/// </summary>
private async Task RevokeAllSessionsForUserAsync(Guid accountId)
{
var now = DateTimeOffset.UtcNow;
var cache = _redis.GetDatabase();
var userKey = RedisKeys.UserSessions(accountId.ToString());
var members = await cache.SetMembersAsync(userKey);
foreach (var member in members)
await cache.KeyDeleteAsync(RedisKeys.Session(member!));
await cache.KeyDeleteAsync(userKey);
var sessions = await _db.AccountSessions
.Where(s => s.AccountId == accountId && s.RevokedAt == null && s.ExpiresAt > now)
.ToListAsync();
foreach (var s in sessions)
s.RevokedAt = now;
if (sessions.Count > 0)
{
await _db.SaveChangesAsync();
_logger.LogInformation(
"Revoked {Count} existing session(s) for account {AccountId} on new login",
sessions.Count, accountId);
}
}
/// <summary>
/// Generates a cryptographically random session token, persists the hash to Postgres,
/// and caches the raw token → accountId mapping in Redis.