Finished adding user support and ability to update specific novels or set your last read chapter
This commit is contained in:
32
WebNovelPortalAPI/Middleware/EnsureUserCreatedMiddleware.cs
Normal file
32
WebNovelPortalAPI/Middleware/EnsureUserCreatedMiddleware.cs
Normal file
@@ -0,0 +1,32 @@
|
||||
using System.Security.Claims;
|
||||
using DBConnection.Repositories.Interfaces;
|
||||
using Microsoft.AspNetCore.Mvc.Filters;
|
||||
using Treestar.Shared.Models.DBDomain;
|
||||
|
||||
namespace WebNovelPortalAPI.Middleware;
|
||||
|
||||
public class EnsureUserCreatedMiddleware : IMiddleware
|
||||
{
|
||||
private readonly IUserRepository _userRepository;
|
||||
public const string UserIdItemName = "userId";
|
||||
public EnsureUserCreatedMiddleware(IUserRepository userRepository)
|
||||
{
|
||||
_userRepository = userRepository;
|
||||
}
|
||||
|
||||
public async Task InvokeAsync(HttpContext context, RequestDelegate next)
|
||||
{
|
||||
if (!context.User.Identity.IsAuthenticated)
|
||||
{
|
||||
await next(context);
|
||||
return;
|
||||
}
|
||||
var userEmail = context.User.Claims.FirstOrDefault(i => i.Type == ClaimTypes.Email)?.Value;
|
||||
var dbUser = await _userRepository.GetIncluded(u => u.Email == userEmail) ?? await _userRepository.Upsert(new User
|
||||
{
|
||||
Email = userEmail
|
||||
});
|
||||
context.Items[UserIdItemName] = dbUser.Id;
|
||||
await next(context);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user