auth service

This commit is contained in:
Tizian.Breuch
2025-07-29 16:39:27 +02:00
parent 6682474a7f
commit a9318b9700

View File

@@ -16,27 +16,31 @@ namespace Webshop.Application.Services.Auth
{ {
public class AuthService : IAuthService public class AuthService : IAuthService
{ {
private readonly UserManager<ApplicationUser> _userManager; // << WICHTIG: ApplicationUser >> private readonly UserManager<ApplicationUser> _userManager;
private readonly SignInManager<ApplicationUser> _signInManager; // << WICHTIG: ApplicationUser >> private readonly SignInManager<ApplicationUser> _signInManager;
private readonly IConfiguration _configuration; private readonly IConfiguration _configuration;
private readonly RoleManager<IdentityRole> _roleManager; private readonly RoleManager<IdentityRole> _roleManager;
private readonly IResend _resend; // << NEU >> private readonly IResend _resend;
private readonly ApplicationDbContext _context; // << NEU: Deklaration >>
public AuthService( public AuthService(
UserManager<ApplicationUser> userManager, UserManager<ApplicationUser> userManager,
SignInManager<ApplicationUser> signInManager, SignInManager<ApplicationUser> signInManager,
IConfiguration configuration, IConfiguration configuration,
RoleManager<IdentityRole> roleManager, RoleManager<IdentityRole> roleManager,
IResend resend) // << NEU: IResend injizieren >> IResend resend,
ApplicationDbContext context) // << NEU: DbContext im Konstruktor injizieren >>
{ {
_userManager = userManager; _userManager = userManager;
_signInManager = signInManager; _signInManager = signInManager;
_configuration = configuration; _configuration = configuration;
_roleManager = roleManager; _roleManager = roleManager;
_resend = resend; _resend = resend;
_context = context; // << NEU: Initialisierung >>
} }
public async Task<AuthResponseDto> RegisterUserAsync(RegisterRequestDto request) public async Task<AuthResponseDto> RegisterUserAsync(RegisterRequestDto request)
{ {
var existingUser = await _userManager.FindByEmailAsync(request.Email); var existingUser = await _userManager.FindByEmailAsync(request.Email);
@@ -45,7 +49,7 @@ 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 ApplicationUser { Email = request.Email, UserName = request.Email, CreatedDate = DateTimeOffset.UtcNow }; // << ApplicationUser >> var user = new ApplicationUser { Email = request.Email, UserName = request.Email, CreatedDate = DateTimeOffset.UtcNow };
var result = await _userManager.CreateAsync(user, request.Password); var result = await _userManager.CreateAsync(user, request.Password);
if (!result.Succeeded) if (!result.Succeeded)
@@ -60,15 +64,26 @@ namespace Webshop.Application.Services.Auth
} }
await _userManager.AddToRoleAsync(user, "Customer"); await _userManager.AddToRoleAsync(user, "Customer");
// << NEU: E-Mail-Bestätigung senden >> // << NEU: HIER WIRD DAS CUSTOMER-PROFIL ERSTELLT UND GESPEICHERT >>
var customerProfile = new Webshop.Domain.Entities.Customer
{
Id = Guid.NewGuid(),
AspNetUserId = user.Id, // Verknüpfung zum ApplicationUser
FirstName = request.FirstName ?? string.Empty, // Vom Request-DTO
LastName = request.LastName ?? string.Empty, // Vom Request-DTO
};
_context.Customers.Add(customerProfile);
await _context.SaveChangesAsync(); // Speichere das neue Kundenprofil
// << ENDE NEUER TEIL >>
await SendEmailConfirmationEmail(user); await SendEmailConfirmationEmail(user);
// Initial kein Token, bis E-Mail bestätigt wurde (wenn options.SignIn.RequireConfirmedEmail = true)
return new AuthResponseDto return new AuthResponseDto
{ {
IsAuthSuccessful = true, IsAuthSuccessful = true,
ErrorMessage = "Registrierung erfolgreich. Bitte bestätigen Sie Ihre E-Mail-Adresse.", ErrorMessage = "Registrierung erfolgreich. Bitte bestätigen Sie Ihre E-Mail-Adresse.",
Token = "", // Kein Token, bis E-Mail bestätigt ist Token = "",
UserId = user.Id, UserId = user.Id,
Email = user.Email, Email = user.Email,
Roles = (await _userManager.GetRolesAsync(user)).ToList() Roles = (await _userManager.GetRolesAsync(user)).ToList()