From 76cb56d819ba9192d376faa81bb37f803dce693b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ji=C5=99=C3=AD=20Vrabec?= Date: Sun, 10 Aug 2025 11:20:18 +0200 Subject: [PATCH] Add user authorization service --- DrinkRateAPI/Exceptions/Exceptions.cs | 7 +++++++ .../Services/ApplicationUserService.cs | 21 +++++++++++++++++++ 2 files changed, 28 insertions(+) create mode 100644 DrinkRateAPI/Exceptions/Exceptions.cs create mode 100644 DrinkRateAPI/Services/ApplicationUserService.cs diff --git a/DrinkRateAPI/Exceptions/Exceptions.cs b/DrinkRateAPI/Exceptions/Exceptions.cs new file mode 100644 index 0000000..14c8432 --- /dev/null +++ b/DrinkRateAPI/Exceptions/Exceptions.cs @@ -0,0 +1,7 @@ +namespace DrinkRateAPI.Exceptions; + +public class DrinkRateException : Exception; + +public class NotFoundException : DrinkRateException; + +public class UnauthorizedException : DrinkRateException; \ No newline at end of file diff --git a/DrinkRateAPI/Services/ApplicationUserService.cs b/DrinkRateAPI/Services/ApplicationUserService.cs new file mode 100644 index 0000000..a2f735d --- /dev/null +++ b/DrinkRateAPI/Services/ApplicationUserService.cs @@ -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 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; + } +} \ No newline at end of file