resend integration

This commit is contained in:
Tizian.Breuch
2025-09-05 10:47:43 +02:00
parent 6b54666af3
commit fba87a1694
10 changed files with 283 additions and 175 deletions

View File

@@ -1,92 +1,149 @@
using Microsoft.AspNetCore.Mvc;
// src/Webshop.Api/Controllers/Auth/AuthController.cs
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System.Threading.Tasks;
using Webshop.Application;
using Webshop.Application.DTOs.Auth;
using Webshop.Application.DTOs.Email;
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
namespace Webshop.Api.Controllers.Auth
{
[ApiController]
[Route("api/v1/auth/[controller]")] // z.B. /api/v1/auth
[Route("api/v1/auth")]
public class AuthController : ControllerBase
{
private readonly IAuthService _authService;
private readonly ICustomerService _customerService;
public AuthController(IAuthService authService, ICustomerService customerService)
public AuthController(IAuthService authService)
{
_authService = authService;
_customerService = customerService;
}
[HttpPost("register")]
[AllowAnonymous]
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status400BadRequest)]
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))
if (result.Type == ServiceResultType.Success)
{
return Ok(new { Message = result.ErrorMessage, Email = result.Email }); // Sende Status und Info, dass Mail gesendet
return Ok(new { Message = "Registrierung erfolgreich. Bitte bestätigen Sie Ihre E-Mail-Adresse." });
}
if (!result.IsAuthSuccessful) return BadRequest(new { Message = result.ErrorMessage });
return Ok(result);
return BadRequest(new { Message = result.ErrorMessage });
}
[HttpPost("login/customer")] // /api/v1/auth/login/customer (für Kunden-Login)
[HttpPost("login/customer")]
[AllowAnonymous]
[ProducesResponseType(typeof(AuthResponseDto), StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status401Unauthorized)]
public async Task<IActionResult> LoginCustomer([FromBody] LoginRequestDto request)
{
if (!ModelState.IsValid) return BadRequest(ModelState);
var result = await _authService.LoginUserAsync(request);
if (!result.IsAuthSuccessful) return Unauthorized(new { Message = result.ErrorMessage });
return Ok(result);
if (result.Type == ServiceResultType.Success)
{
return Ok(result.Value);
}
return Unauthorized(new { Message = result.ErrorMessage });
}
[HttpPost("login/admin")] // /api/v1/auth/login/admin (für Admin-Dashboard Login)
[HttpPost("login/admin")]
[AllowAnonymous]
[ProducesResponseType(typeof(AuthResponseDto), StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status401Unauthorized)]
[ProducesResponseType(StatusCodes.Status403Forbidden)]
public async Task<IActionResult> LoginAdmin([FromBody] LoginRequestDto request)
{
if (!ModelState.IsValid) return BadRequest(ModelState);
var result = await _authService.LoginAdminAsync(request);
if (!result.IsAuthSuccessful) return Unauthorized(new { Message = result.ErrorMessage });
return Ok(result);
return result.Type switch
{
ServiceResultType.Success => Ok(result.Value),
ServiceResultType.Forbidden => Forbid(),
_ => Unauthorized(new { Message = result.ErrorMessage })
};
}
[HttpGet("confirm-email")] // Für Registrierungsbestätigung
[HttpGet("confirm-email")]
[AllowAnonymous]
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status400BadRequest)]
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!" });
var result = await _authService.ConfirmEmailAsync(userId, token);
if (result.Type == ServiceResultType.Success)
{
return Ok(new { Message = "E-Mail-Adresse erfolgreich bestätigt. Sie können sich jetzt anmelden." });
}
return BadRequest(new { Message = result.ErrorMessage });
}
[HttpPost("resend-email-confirmation")]
[AllowAnonymous]
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status400BadRequest)]
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 });
if (!ModelState.IsValid) return BadRequest(ModelState);
var result = await _authService.ResendEmailConfirmationAsync(request.Email);
if (result.Type == ServiceResultType.Success)
{
return Ok(new { Message = "Wenn ein Konto mit dieser E-Mail-Adresse existiert und noch nicht bestätigt wurde, wurde eine neue E-Mail gesendet." });
}
return BadRequest(new { Message = result.ErrorMessage });
}
[HttpGet("change-email-confirm")] // Für E-Mail-Änderungsbestätigung
// << NEUER ENDPUNKT FÜR PASSWORT VERGESSEN >>
[HttpPost("forgot-password")]
[AllowAnonymous]
public async Task<IActionResult> ConfirmEmailChange([FromQuery] string userId, [FromQuery] string newEmail, [FromQuery] string token)
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status400BadRequest)]
public async Task<IActionResult> ForgotPassword([FromBody] ForgotPasswordRequestDto request)
{
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!" });
if (!ModelState.IsValid) return BadRequest(ModelState);
await _authService.ForgotPasswordAsync(request);
return Ok(new { Message = "Wenn ein Konto mit dieser E-Mail-Adresse existiert, wurde eine E-Mail zum Zurücksetzen des Passworts gesendet." });
}
// << NEUER ENDPUNKT FÜR PASSWORT ZURÜCKSETZEN >>
[HttpPost("reset-password")]
[AllowAnonymous]
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status400BadRequest)]
public async Task<IActionResult> ResetPassword([FromBody] ResetPasswordDto request)
{
if (!ModelState.IsValid) return BadRequest(ModelState);
var result = await _authService.ResetPasswordAsync(request);
if (result.Type == ServiceResultType.Success)
{
return Ok(new { Message = "Ihr Passwort wurde erfolgreich zurückgesetzt." });
}
return BadRequest(new { Message = result.ErrorMessage });
}
// HINWEIS: Der 'change-email-confirm'-Endpunkt gehört logisch eher zum CustomerController,
// da er eine Aktion eines eingeloggten Benutzers ist.
}
}