diff options
| author | ivar <i@oiee.no> | 2025-10-26 22:58:52 +0100 |
|---|---|---|
| committer | ivar <i@oiee.no> | 2025-10-26 22:58:52 +0100 |
| commit | e4fb2a3c493cca456f1515bdaad39499eafc3348 (patch) | |
| tree | 961aaa0aef410de292b6cd28c8cd7f4f59762cd3 /api | |
| parent | 842502e82c4ddfea05a5f77c361aaa27f75afc42 (diff) | |
| download | what-e4fb2a3c493cca456f1515bdaad39499eafc3348.tar.xz what-e4fb2a3c493cca456f1515bdaad39499eafc3348.zip | |
Implement UserLastSeenMiddleware
Diffstat (limited to 'api')
| -rw-r--r-- | api/WhatApi/Middleware/UserLastSeenMiddleware.cs | 10 | ||||
| -rw-r--r-- | api/WhatApi/Program.cs | 3 |
2 files changed, 12 insertions, 1 deletions
diff --git a/api/WhatApi/Middleware/UserLastSeenMiddleware.cs b/api/WhatApi/Middleware/UserLastSeenMiddleware.cs index ef1b685..e52f890 100644 --- a/api/WhatApi/Middleware/UserLastSeenMiddleware.cs +++ b/api/WhatApi/Middleware/UserLastSeenMiddleware.cs @@ -1,8 +1,18 @@ +using System.Security.Claims; + namespace WhatApi.Middleware; public class UserLastSeenMiddleware(RequestDelegate next, Database db) { public async Task InvokeAsync(HttpContext context) { + var userIdString = context.User.FindFirstValue(ClaimTypes.NameIdentifier); + if (Guid.TryParse(userIdString, out var userId)) { + var user = await db.Users.FirstOrDefaultAsync(c => c.Id == userId); + if (user is not null) { + user.LastSeen = DateTimeOffset.UtcNow; + await db.SaveChangesAsync(); + } + } await next(context); } }
\ No newline at end of file diff --git a/api/WhatApi/Program.cs b/api/WhatApi/Program.cs index 884240d..27ceaf9 100644 --- a/api/WhatApi/Program.cs +++ b/api/WhatApi/Program.cs @@ -7,6 +7,7 @@ global using NetTopologySuite.Geometries; global using Microsoft.AspNetCore.Http.Extensions; global using Microsoft.AspNetCore.Mvc; global using NetTopologySuite; +using WhatApi.Middleware; namespace WhatApi; @@ -38,11 +39,11 @@ public partial class Program var db = scope.ServiceProvider.GetRequiredService<Database>(); Seed(db); #endif - app.UseRouting(); app.UseForwardedHeaders(); app.UseCors(); app.MapStaticAssets(); + app.UseMiddleware<UserLastSeenMiddleware>(); app.MapRazorPages(); app.MapControllers(); app.Run(); |
