This commit is contained in:
Tizian.Breuch
2025-07-29 15:40:47 +02:00
parent 93a1f2411b
commit 5f8b8f3971
12 changed files with 269 additions and 60 deletions

View File

@@ -10,6 +10,10 @@ using Webshop.Domain.Interfaces; // ICustomerRepository
using Webshop.Domain.Identity; // Für ApplicationUser
using System.Linq; // Für Select
using System.Collections.Generic; // Für IEnumerable
using System.Web;
using Resend;
using Microsoft.Extensions.Configuration;
using System.Net.Mail;
namespace Webshop.Application.Services.Customers
{
@@ -17,11 +21,15 @@ namespace Webshop.Application.Services.Customers
{
private readonly ICustomerRepository _customerRepository;
private readonly UserManager<ApplicationUser> _userManager;
private readonly IConfiguration _configuration; // << NEU: Für BaseUrl >>
private readonly IResend _resend; // << NEU >>
public CustomerService(ICustomerRepository customerRepository, UserManager<ApplicationUser> userManager)
{
_customerRepository = customerRepository;
_userManager = userManager;
_configuration = configuration;
_resend = resend;
}
public async Task<CustomerDto?> GetMyProfileAsync(string userId)
@@ -117,5 +125,63 @@ namespace Webshop.Application.Services.Customers
return (true, "Profil und Kontaktdaten erfolgreich aktualisiert.");
}
public async Task<(bool Success, string ErrorMessage)> ChangeEmailAsync(string userId, string newEmail, string currentPassword)
{
var user = await _userManager.FindByIdAsync(userId);
if (user == null) return (false, "Benutzer nicht gefunden.");
if (!await _userManager.CheckPasswordAsync(user, currentPassword))
{
return (false, "Falsches aktuelles Passwort.");
}
// Prüfen, ob die neue E-Mail bereits vergeben ist (außer sie ist die aktuelle E-Mail)
if (user.Email != newEmail && await _userManager.FindByEmailAsync(newEmail) != null)
{
return (false, "Die neue E-Mail-Adresse ist bereits registriert.");
}
var token = await _userManager.GenerateChangeEmailTokenAsync(user, newEmail);
var encodedToken = HttpUtility.UrlEncode(token);
var baseUrl = _configuration["App:BaseUrl"];
// Link für E-Mail-Bestätigung der Änderung
var confirmationLink = $"{baseUrl}/api/v1/auth/change-email-confirm?userId={user.Id}&newEmail={HttpUtility.UrlEncode(newEmail)}&token={encodedToken}";
var message = new EmailMessage();
message.From = "Your Webshop <no-reply@resend.dev>"; // << ANPASSEN >>
message.To.Add(newEmail);
message.Subject = "Bestätigen Sie Ihre E-Mail-Änderung für Your Webshop";
message.HtmlBody = $@"
<h1>E-Mail-Änderung Bestätigung</h1>
<p>Sie haben eine Änderung Ihrer E-Mail-Adresse beantragt. Bitte klicken Sie auf den folgenden Link, um dies zu bestätigen:</p>
<p><a href=""{confirmationLink}"">{confirmationLink}</a></p>
<p>Wenn Sie diese Änderung nicht angefordert haben, können Sie diese E-Mail ignorieren.</p>
<p>Vielen Dank!</p>";
await _resend.EmailSendAsync(message);
return (true, "Bestätigungs-E-Mail für die E-Mail-Änderung wurde gesendet. Bitte prüfen Sie Ihr Postfach.");
}
public async Task<(bool Success, string ErrorMessage)> ConfirmEmailChangeAsync(string userId, string newEmail, string token)
{
var user = await _userManager.FindByIdAsync(userId);
if (user == null) return (false, "Benutzer nicht gefunden.");
var result = await _userManager.ChangeEmailAsync(user, newEmail, HttpUtility.UrlDecode(token));
if (!result.Succeeded)
{
var errors = string.Join(" ", result.Errors.Select(e => e.Description));
return (false, $"E-Mail-Änderung konnte nicht bestätigt werden: {errors}");
}
// Optional: Bestätigung der Telefonnummer zurücksetzen, wenn E-Mail geändert wurde
// user.PhoneNumberConfirmed = false;
// await _userManager.UpdateAsync(user);
return (true, "E-Mail-Adresse erfolgreich geändert und bestätigt.");
}
}
}

View File

@@ -11,6 +11,7 @@ namespace Webshop.Application.Services.Customers
Task<CustomerDto?> GetMyProfileAsync(string userId);
Task<(bool Success, string ErrorMessage)> ChangePasswordAsync(string userId, ChangePasswordRequestDto request);
Task<(bool Success, string ErrorMessage)> UpdateMyProfileAsync(string userId, UpdateCustomerDto profileDto);
Task<(bool Success, string ErrorMessage)> ChangeEmailAsync(string userId, string newEmail, string currentPassword);
Task<(bool Success, string ErrorMessage)> ConfirmEmailChangeAsync(string userId, string newEmail, string token);
}
}