auth service
This commit is contained in:
@@ -16,27 +16,31 @@ namespace Webshop.Application.Services.Auth
|
||||
{
|
||||
public class AuthService : IAuthService
|
||||
{
|
||||
private readonly UserManager<ApplicationUser> _userManager; // << WICHTIG: ApplicationUser >>
|
||||
private readonly SignInManager<ApplicationUser> _signInManager; // << WICHTIG: ApplicationUser >>
|
||||
private readonly UserManager<ApplicationUser> _userManager;
|
||||
private readonly SignInManager<ApplicationUser> _signInManager;
|
||||
private readonly IConfiguration _configuration;
|
||||
private readonly RoleManager<IdentityRole> _roleManager;
|
||||
private readonly IResend _resend; // << NEU >>
|
||||
private readonly IResend _resend;
|
||||
private readonly ApplicationDbContext _context; // << NEU: Deklaration >>
|
||||
|
||||
public AuthService(
|
||||
UserManager<ApplicationUser> userManager,
|
||||
SignInManager<ApplicationUser> signInManager,
|
||||
IConfiguration configuration,
|
||||
RoleManager<IdentityRole> roleManager,
|
||||
IResend resend) // << NEU: IResend injizieren >>
|
||||
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<AuthResponseDto> 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()
|
||||
|
||||
Reference in New Issue
Block a user