♻️ Better updating device name
This commit is contained in:
		| @@ -427,34 +427,38 @@ public class AccountCurrentController( | ||||
|         public string? Label { get; set; } | ||||
|         public string UserAgent { get; set; } = null!; | ||||
|         public string DeviceId { get; set; } = null!; | ||||
|         public List<Session> Sessions { get; set; } = new(); | ||||
|         public ChallengePlatform Platform { get; set; } | ||||
|         public List<Session> Sessions { get; set; } = []; | ||||
|     } | ||||
|  | ||||
|     [HttpGet("devices")] | ||||
|     [Authorize] | ||||
|     public async Task<ActionResult<List<AuthorizedDevice>>> GetDevices() | ||||
|     { | ||||
|         if (HttpContext.Items["CurrentUser"] is not Account currentUser) | ||||
|         { | ||||
|             return Unauthorized(); | ||||
|         } | ||||
|         if (HttpContext.Items["CurrentUser"] is not Account currentUser || | ||||
|             HttpContext.Items["CurrentSession"] is not Session currentSession) return Unauthorized(); | ||||
|          | ||||
|         Response.Headers.Append("X-Auth-Session", currentSession.Id.ToString()); | ||||
|  | ||||
|         // Group sessions by the related DeviceId, then create an AuthorizedDevice for each group. | ||||
|         var deviceGroups = await db.AuthSessions | ||||
|             .Where(s => s.Account.Id == currentUser.Id) | ||||
|             // Include the challenge if you need it to access DeviceId | ||||
|             .Include(s => s.Challenge) | ||||
|             .GroupBy(s => s.Challenge.DeviceId!) | ||||
|             .Select(g => new AuthorizedDevice | ||||
|             { | ||||
|                 DeviceId = g.Key!, | ||||
|                 UserAgent = g.First(x => x.Challenge.UserAgent != null).Challenge.UserAgent!, | ||||
|                 Label = g.Where(x => string.IsNullOrWhiteSpace(x.Label)).Select(x => x.Label).FirstOrDefault(), | ||||
|                 Platform = g.First().Challenge.Platform!, | ||||
|                 Label = g.Where(x => !string.IsNullOrWhiteSpace(x.Label)).Select(x => x.Label).FirstOrDefault(), | ||||
|                 Sessions = g | ||||
|                     .OrderByDescending(x => x.LastGrantedAt) | ||||
|                     .ToList() | ||||
|             }) | ||||
|             .ToListAsync(); | ||||
|         deviceGroups = deviceGroups | ||||
|             .OrderByDescending(s => s.Sessions.First().LastGrantedAt) | ||||
|             .ToList(); | ||||
|  | ||||
|         return Ok(deviceGroups); | ||||
|     } | ||||
|   | ||||
| @@ -328,11 +328,17 @@ public class AccountService( | ||||
|              .Where(s => s.Id == sessionId && s.AccountId == account.Id) | ||||
|              .FirstOrDefaultAsync(); | ||||
|          if (session is null) throw new InvalidOperationException("Session was not found."); | ||||
|         | ||||
|          session.Label = label; | ||||
|          await db.SaveChangesAsync(); | ||||
|  | ||||
|          await cache.RemoveAsync($"{DysonTokenAuthHandler.AuthCachePrefix}{session.Id}"); | ||||
|          await db.AuthChallenges | ||||
|              .Where(s => s.DeviceId == session.Challenge.DeviceId) | ||||
|              .ExecuteUpdateAsync(p => p.SetProperty(s => s.DeviceId, label)); | ||||
|  | ||||
|          var sessions = await db.AuthSessions | ||||
|              .Include(s => s.Challenge) | ||||
|              .Where(s => s.AccountId == session.Id && s.Challenge.DeviceId == session.Challenge.DeviceId) | ||||
|              .ToListAsync(); | ||||
|          foreach(var item in sessions) | ||||
|             await cache.RemoveAsync($"{DysonTokenAuthHandler.AuthCachePrefix}{item.Id}"); | ||||
|  | ||||
|          return session; | ||||
|     } | ||||
|   | ||||
		Reference in New Issue
	
	Block a user