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!" });
}
}
}