Add user authorization service
This commit is contained in:
parent
703d4751e2
commit
76cb56d819
2 changed files with 28 additions and 0 deletions
7
DrinkRateAPI/Exceptions/Exceptions.cs
Normal file
7
DrinkRateAPI/Exceptions/Exceptions.cs
Normal file
|
@ -0,0 +1,7 @@
|
|||
namespace DrinkRateAPI.Exceptions;
|
||||
|
||||
public class DrinkRateException : Exception;
|
||||
|
||||
public class NotFoundException : DrinkRateException;
|
||||
|
||||
public class UnauthorizedException : DrinkRateException;
|
21
DrinkRateAPI/Services/ApplicationUserService.cs
Normal file
21
DrinkRateAPI/Services/ApplicationUserService.cs
Normal 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;
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue