From a9318b97003b9550eec51ba46614382928501d85 Mon Sep 17 00:00:00 2001 From: "Tizian.Breuch" Date: Tue, 29 Jul 2025 16:39:27 +0200 Subject: [PATCH] auth service --- .../Services/Auth/AuthService.cs | 39 +++++++++++++------ 1 file changed, 27 insertions(+), 12 deletions(-) diff --git a/Webshop.Application/Services/Auth/AuthService.cs b/Webshop.Application/Services/Auth/AuthService.cs index 9cffe7c..a2e76c0 100644 --- a/Webshop.Application/Services/Auth/AuthService.cs +++ b/Webshop.Application/Services/Auth/AuthService.cs @@ -16,27 +16,31 @@ namespace Webshop.Application.Services.Auth { public class AuthService : IAuthService { - private readonly UserManager _userManager; // << WICHTIG: ApplicationUser >> - private readonly SignInManager _signInManager; // << WICHTIG: ApplicationUser >> + private readonly UserManager _userManager; + private readonly SignInManager _signInManager; private readonly IConfiguration _configuration; private readonly RoleManager _roleManager; - private readonly IResend _resend; // << NEU >> + private readonly IResend _resend; + private readonly ApplicationDbContext _context; // << NEU: Deklaration >> public AuthService( - UserManager userManager, - SignInManager signInManager, - IConfiguration configuration, - RoleManager roleManager, - IResend resend) // << NEU: IResend injizieren >> + UserManager userManager, + SignInManager signInManager, + IConfiguration configuration, + RoleManager roleManager, + IResend resend, + ApplicationDbContext context) // << NEU: DbContext im Konstruktor injizieren >> { _userManager = userManager; _signInManager = signInManager; _configuration = configuration; _roleManager = roleManager; _resend = resend; + _context = context; // << NEU: Initialisierung >> } + public async Task RegisterUserAsync(RegisterRequestDto request) { 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." }; } - 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); if (!result.Succeeded) @@ -60,15 +64,26 @@ namespace Webshop.Application.Services.Auth } 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); - // Initial kein Token, bis E-Mail bestätigt wurde (wenn options.SignIn.RequireConfirmedEmail = true) return new AuthResponseDto { IsAuthSuccessful = true, ErrorMessage = "Registrierung erfolgreich. Bitte bestätigen Sie Ihre E-Mail-Adresse.", - Token = "", // Kein Token, bis E-Mail bestätigt ist + Token = "", UserId = user.Id, Email = user.Email, Roles = (await _userManager.GetRolesAsync(user)).ToList()