diff --git a/Services/Auth/AuthService.cs b/Services/Auth/AuthService.cs index 7589cad..724c416 100644 --- a/Services/Auth/AuthService.cs +++ b/Services/Auth/AuthService.cs @@ -21,20 +21,42 @@ public sealed class AuthService : IAuthService private readonly IRedisManager _redis; private readonly ILogger _log; - private const string LuaLoginAtom = @" - -- KEYS[1] = online:acct:{accountId} - -- KEYS[2] = sess:{token} - -- ARGV[1] = token - -- ARGV[2] = ttlSeconds - -- ARGV[3] = accountId (string) + //private const string LuaLoginAtom = @" + // -- KEYS[1] = online:acct:{accountId} + // -- KEYS[2] = sess:{token} + // -- ARGV[1] = token + // -- ARGV[2] = ttlSeconds + // -- ARGV[3] = accountId (string) - if redis.call('EXISTS', KEYS[1]) == 1 then - return 0 + // if redis.call('EXISTS', KEYS[1]) == 1 then + // return 0 + // end + // redis.call('SET', KEYS[1], ARGV[1]) + // redis.call('SETEX', KEYS[2], ARGV[2], ARGV[3]) + // return 1 + // "; + private const string LuaLoginAtom = @" + -- KEYS[1] = online:acct:{accountId} + -- KEYS[2] = sess:{token} + -- ARGV[1] = token + -- ARGV[2] = ttlSeconds + -- ARGV[3] = accountId + + local existingToken = redis.call('GET', KEYS[1]) + if existingToken then + local existingSessKey = 'sess:' .. existingToken + if redis.call('EXISTS', existingSessKey) == 1 then + return 0 -- truly already online end - redis.call('SET', KEYS[1], ARGV[1]) - redis.call('SETEX', KEYS[2], ARGV[2], ARGV[3]) - return 1 - "; + -- stale marker, clear it + redis.call('DEL', KEYS[1]) + end + + redis.call('SETEX', KEYS[1], ARGV[2], ARGV[1]) + redis.call('SETEX', KEYS[2], ARGV[2], ARGV[3]) + return 1 + "; + private const string LuaLogoutAtom = @" -- KEYS[1] = online:acct:{accountId}