This commit is contained in:
Tizian.Breuch
2025-07-25 11:34:37 +02:00
parent 407a3d731b
commit a9adaff3eb

View File

@@ -8,19 +8,22 @@ using System.Text;
using Webshop.Application.DTOs.Auth; using Webshop.Application.DTOs.Auth;
using System.Threading.Tasks; using System.Threading.Tasks;
using System.Collections.Generic; using System.Collections.Generic;
using Webshop.Domain.Entities; // <-- WICHTIG: Das Using für Ihre neue Klasse hinzufügen
namespace Webshop.Application.Services.Auth namespace Webshop.Application.Services.Auth
{ {
public class AuthService : IAuthService // Sicherstellen, dass IAuthService implementiert wird public class AuthService : IAuthService
{ {
private readonly UserManager<IdentityUser> _userManager; // Ändern Sie hier IdentityUser zu ApplicationUser
private readonly SignInManager<IdentityUser> _signInManager; private readonly UserManager<ApplicationUser> _userManager;
private readonly SignInManager<ApplicationUser> _signInManager;
private readonly IConfiguration _configuration; private readonly IConfiguration _configuration;
private readonly RoleManager<IdentityRole> _roleManager; private readonly RoleManager<IdentityRole> _roleManager;
public AuthService( public AuthService(
UserManager<IdentityUser> userManager, // Ändern Sie auch hier die Typen im Konstruktor
SignInManager<IdentityUser> signInManager, UserManager<ApplicationUser> userManager,
SignInManager<ApplicationUser> signInManager,
IConfiguration configuration, IConfiguration configuration,
RoleManager<IdentityRole> roleManager) RoleManager<IdentityRole> roleManager)
{ {
@@ -38,7 +41,13 @@ namespace Webshop.Application.Services.Auth
return new AuthResponseDto { IsAuthSuccessful = false, ErrorMessage = "E-Mail ist bereits registriert." }; 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); var result = await _userManager.CreateAsync(user, request.Password);
if (!result.Succeeded) if (!result.Succeeded)
@@ -47,6 +56,7 @@ namespace Webshop.Application.Services.Auth
return new AuthResponseDto { IsAuthSuccessful = false, ErrorMessage = errors }; 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")) if (!await _roleManager.RoleExistsAsync("Customer"))
{ {
await _roleManager.CreateAsync(new IdentityRole("Customer")); await _roleManager.CreateAsync(new IdentityRole("Customer"));
@@ -68,6 +78,7 @@ namespace Webshop.Application.Services.Auth
public async Task<AuthResponseDto> LoginUserAsync(LoginRequestDto request) public async Task<AuthResponseDto> LoginUserAsync(LoginRequestDto request)
{ {
// Diese Methode funktioniert ohne Änderungen, da der _userManager jetzt vom richtigen Typ ist.
var user = await _userManager.FindByEmailAsync(request.Email); var user = await _userManager.FindByEmailAsync(request.Email);
if (user == null) if (user == null)
{ {
@@ -95,6 +106,7 @@ namespace Webshop.Application.Services.Auth
public async Task<AuthResponseDto> LoginAdminAsync(LoginRequestDto request) public async Task<AuthResponseDto> LoginAdminAsync(LoginRequestDto request)
{ {
// Diese Methode profitiert direkt von der Korrektur in LoginUserAsync.
var authResponse = await LoginUserAsync(request); var authResponse = await LoginUserAsync(request);
if (!authResponse.IsAuthSuccessful) if (!authResponse.IsAuthSuccessful)
{ {
@@ -102,6 +114,7 @@ namespace Webshop.Application.Services.Auth
} }
var user = await _userManager.FindByEmailAsync(request.Email); 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")) if (user == null || !await _userManager.IsInRoleAsync(user, "Admin"))
{ {
return new AuthResponseDto { IsAuthSuccessful = false, ErrorMessage = "Keine Berechtigung." }; return new AuthResponseDto { IsAuthSuccessful = false, ErrorMessage = "Keine Berechtigung." };
@@ -110,7 +123,8 @@ namespace Webshop.Application.Services.Auth
return authResponse; return authResponse;
} }
private async Task<string> GenerateJwtToken(IdentityUser user, IList<string> roles) // Ändern Sie hier den Parameter-Typ zu ApplicationUser
private async Task<string> GenerateJwtToken(ApplicationUser user, IList<string> roles)
{ {
var claims = new List<Claim> var claims = new List<Claim>
{ {