Add user authorization service

This commit is contained in:
Jiří Vrabec 2025-08-10 11:20:18 +02:00
parent 703d4751e2
commit 76cb56d819
2 changed files with 28 additions and 0 deletions

View file

@ -0,0 +1,7 @@
namespace DrinkRateAPI.Exceptions;
public class DrinkRateException : Exception;
public class NotFoundException : DrinkRateException;
public class UnauthorizedException : DrinkRateException;

View file

@ -0,0 +1,21 @@
using System.Security.Claims;
using DrinkRateAPI.Contexts;
using DrinkRateAPI.DbEntities;
using DrinkRateAPI.Exceptions;
using Microsoft.EntityFrameworkCore;
namespace DrinkRateAPI.Services;
public class ApplicationUserService(ApplicationDbContext context)
{
private ApplicationDbContext _context = context;
public async Task<DbUserProfile> UserProfileByApplicationUserAsync(ClaimsPrincipal identity)
{
var appUserId = identity.FindFirst(ClaimTypes.NameIdentifier)?.Value;
var profile = await _context.UserProfiles
.FirstAsync(x => x.ApplicationUserId.ToString() == appUserId)
?? throw new NotFoundException();
return profile;
}
}