From a9adaff3ebb2d44a42ff6f51e020cb4329571a2e Mon Sep 17 00:00:00 2001 From: "Tizian.Breuch" Date: Fri, 25 Jul 2025 11:34:37 +0200 Subject: [PATCH] auth fix --- .../Services/Auth/AuthService.cs | 28 ++++++++++++++----- 1 file changed, 21 insertions(+), 7 deletions(-) diff --git a/Webshop.Application/Services/Auth/AuthService.cs b/Webshop.Application/Services/Auth/AuthService.cs index 04cc161..0b02838 100644 --- a/Webshop.Application/Services/Auth/AuthService.cs +++ b/Webshop.Application/Services/Auth/AuthService.cs @@ -8,19 +8,22 @@ using System.Text; using Webshop.Application.DTOs.Auth; using System.Threading.Tasks; using System.Collections.Generic; +using Webshop.Domain.Entities; // <-- WICHTIG: Das Using für Ihre neue Klasse hinzufügen namespace Webshop.Application.Services.Auth { - public class AuthService : IAuthService // Sicherstellen, dass IAuthService implementiert wird + public class AuthService : IAuthService { - private readonly UserManager _userManager; - private readonly SignInManager _signInManager; + // Ändern Sie hier IdentityUser zu ApplicationUser + private readonly UserManager _userManager; + private readonly SignInManager _signInManager; private readonly IConfiguration _configuration; private readonly RoleManager _roleManager; public AuthService( - UserManager userManager, - SignInManager signInManager, + // Ändern Sie auch hier die Typen im Konstruktor + UserManager userManager, + SignInManager signInManager, IConfiguration configuration, RoleManager roleManager) { @@ -38,7 +41,13 @@ namespace Webshop.Application.Services.Auth return new AuthResponseDto { IsAuthSuccessful = false, ErrorMessage = "E-Mail ist bereits registriert." }; } - var user = new IdentityUser { Email = request.Email, UserName = request.Email }; + // Erstellen Sie hier eine Instanz Ihrer neuen ApplicationUser-Klasse + var user = new ApplicationUser + { + Email = request.Email, + UserName = request.Email, + CreatedDate = DateTimeOffset.UtcNow // Setzen Sie das neue Feld! + }; var result = await _userManager.CreateAsync(user, request.Password); if (!result.Succeeded) @@ -47,6 +56,7 @@ namespace Webshop.Application.Services.Auth return new AuthResponseDto { IsAuthSuccessful = false, ErrorMessage = errors }; } + // Der Rest der Logik bleibt gleich, da die Rollenverwaltung nicht vom User-Typ abhängt if (!await _roleManager.RoleExistsAsync("Customer")) { await _roleManager.CreateAsync(new IdentityRole("Customer")); @@ -68,6 +78,7 @@ namespace Webshop.Application.Services.Auth public async Task LoginUserAsync(LoginRequestDto request) { + // Diese Methode funktioniert ohne Änderungen, da der _userManager jetzt vom richtigen Typ ist. var user = await _userManager.FindByEmailAsync(request.Email); if (user == null) { @@ -95,6 +106,7 @@ namespace Webshop.Application.Services.Auth public async Task LoginAdminAsync(LoginRequestDto request) { + // Diese Methode profitiert direkt von der Korrektur in LoginUserAsync. var authResponse = await LoginUserAsync(request); if (!authResponse.IsAuthSuccessful) { @@ -102,6 +114,7 @@ namespace Webshop.Application.Services.Auth } var user = await _userManager.FindByEmailAsync(request.Email); + // Stellt sicher, dass der User gefunden wurde und die Rolle "Admin" hat. if (user == null || !await _userManager.IsInRoleAsync(user, "Admin")) { return new AuthResponseDto { IsAuthSuccessful = false, ErrorMessage = "Keine Berechtigung." }; @@ -110,7 +123,8 @@ namespace Webshop.Application.Services.Auth return authResponse; } - private async Task GenerateJwtToken(IdentityUser user, IList roles) + // Ändern Sie hier den Parameter-Typ zu ApplicationUser + private async Task GenerateJwtToken(ApplicationUser user, IList roles) { var claims = new List {