summaryrefslogtreecommitdiffstats
path: root/api
diff options
context:
space:
mode:
Diffstat (limited to 'api')
-rw-r--r--api/WhatApi/Middleware/UserLastSeenMiddleware.cs10
-rw-r--r--api/WhatApi/Program.cs3
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();