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