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

@@ -2,6 +2,8 @@
using Webshop.Application.DTOs.Auth;
using Webshop.Application.Services.Auth;
using Microsoft.AspNetCore.Authorization;
using System.ComponentModel.DataAnnotations;
using Webshop.Application.Services.Customers;
namespace Webshop.Api.Controllers.Auth // Beachten Sie den Namespace
{
@@ -10,18 +12,33 @@ namespace Webshop.Api.Controllers.Auth // Beachten Sie den Namespace
public class AuthController : ControllerBase
{
private readonly IAuthService _authService;
private readonly ICustomerService _customerService;
public AuthController(IAuthService authService)
public class ResendEmailConfirmationRequestDto
{
_authService = authService;
[Required]
[EmailAddress]
public string Email { get; set; } = string.Empty;
}
[HttpPost("register")] // /api/v1/auth/register (für Kunden)
[AllowAnonymous] // Jeder darf sich registrieren
public AuthController(IAuthService authService, ICustomerService customerService)
{
_authService = authService;
_customerService = customerService;
}
[HttpPost("register")]
[AllowAnonymous]
public async Task<IActionResult> Register([FromBody] RegisterRequestDto request)
{
if (!ModelState.IsValid) return BadRequest(ModelState);
var result = await _authService.RegisterUserAsync(request);
// Wenn Registrierung erfolgreich, aber E-Mail-Bestätigung aussteht, sollte der Token leer sein
if (result.IsAuthSuccessful && string.IsNullOrEmpty(result.Token))
{
return Ok(new { Message = result.ErrorMessage, Email = result.Email }); // Sende Status und Info, dass Mail gesendet
}
if (!result.IsAuthSuccessful) return BadRequest(new { Message = result.ErrorMessage });
return Ok(result);
}
@@ -45,5 +62,36 @@ namespace Webshop.Api.Controllers.Auth // Beachten Sie den Namespace
if (!result.IsAuthSuccessful) return Unauthorized(new { Message = result.ErrorMessage });
return Ok(result);
}
[HttpGet("confirm-email")] // Für Registrierungsbestätigung
[AllowAnonymous]
public async Task<IActionResult> ConfirmEmail([FromQuery] string userId, [FromQuery] string token)
{
var (success, errorMessage) = await _authService.ConfirmEmailAsync(userId, token);
if (!success) return BadRequest(new { Message = errorMessage });
return Ok(new { Message = "E-Mail-Adresse erfolgreich bestätigt. Sie können sich jetzt anmelden!" });
}
[HttpPost("resend-email-confirmation")]
[AllowAnonymous]
public async Task<IActionResult> ResendEmailConfirmation([FromBody] ResendEmailConfirmationRequestDto request)
{
if (string.IsNullOrEmpty(request.Email)) return BadRequest(new { Message = "E-Mail ist erforderlich." });
var (success, errorMessage) = await _authService.ResendEmailConfirmationAsync(request.Email);
if (!success) return BadRequest(new { Message = errorMessage });
return Ok(new { Message = errorMessage });
}
[HttpGet("change-email-confirm")] // Für E-Mail-Änderungsbestätigung
[AllowAnonymous]
public async Task<IActionResult> ConfirmEmailChange([FromQuery] string userId, [FromQuery] string newEmail, [FromQuery] string token)
{
var (success, errorMessage) = await _customerService.ConfirmEmailChangeAsync(userId, newEmail, token); // << Jetzt korrekt >>
if (!success) return BadRequest(new { Message = errorMessage });
return Ok(new { Message = "Ihre E-Mail-Adresse wurde erfolgreich geändert und bestätigt!" });
}
}
}

View File

@@ -8,9 +8,20 @@ using Webshop.Application.DTOs.Customers; // UpdateCustomerProfileDto
using Webshop.Application.Services;
using System.Threading.Tasks;
using Webshop.Application.Services.Customers;
using System.ComponentModel.DataAnnotations;
namespace Webshop.Api.Controllers.Customer
{
public class ChangeEmailRequestDto
{
[Required(ErrorMessage = "Neue E-Mail ist erforderlich.")]
[EmailAddress(ErrorMessage = "Ung<6E>ltiges E-Mail-Format.")]
public string NewEmail { get; set; } = string.Empty;
[Required(ErrorMessage = "Aktuelles Passwort ist erforderlich.")]
public string CurrentPassword { get; set; } = string.Empty;
}
[ApiController]
[Route("api/v1/customer/[controller]")] // z.B. /api/v1/customer/profile
[Authorize(Roles = "Customer")]
@@ -66,6 +77,19 @@ namespace Webshop.Api.Controllers.Customer
return Ok(new { Message = "Profil erfolgreich aktualisiert." });
}
// << ENTFERNT: UpdateContactInfo Endpoint >>
[HttpPost("change-email-request")] // /api/v1/customer/profile/change-email-request
public async Task<IActionResult> ChangeEmailRequest([FromBody] ChangeEmailRequestDto request) // DTO erstellen
{
if (!ModelState.IsValid) return BadRequest(ModelState);
var userId = User.FindFirstValue(ClaimTypes.NameIdentifier);
if (string.IsNullOrEmpty(userId)) return Unauthorized(new { Message = "Benutzer-ID nicht im Token gefunden." });
var (success, errorMessage) = await _customerService.ChangeEmailAsync(userId, request.NewEmail, request.CurrentPassword);
if (!success) return BadRequest(new { Message = errorMessage });
return Ok(new { Message = errorMessage }); // Sendet Info, dass Mail gesendet wurde
}
}
}