feat(auth): confirmation email + forgot/reset password
#37: Email confirmation flow: - Nouveaux endpoints: POST /api/auth/confirm-email - Register envoie email de confirmation si SMTP configure + flag active - Login verifie EmailConfirmed si email.confirmation.required = true - Premier admin auto-confirme (evite chicken-and-egg) - Setting DB: email.confirmation.required (boolean) #38: Forgot/Reset password flow: - Nouveaux endpoints: POST /api/auth/forgot-password, POST /api/auth/reset-password - ForgotPassword genere token et envoie email via IEmailService - ResetPassword valide token et change le mot de passe - Pas de divulgation si l email n'existe pas Infrastructure email existante (IEmailService + SmtpClient) reutilisee.
This commit is contained in:
1 parent
ba985ed3c2
commit
663b872dc2
13 files changed
+312
-3
No files matched your search
@@ -5,6 +5,9 @@ using Heimdall.Application.Auth.Commands.Verify2fa;
|
||||
using Heimdall.Application.Auth.Commands.RefreshToken;
|
||||
using Heimdall.Application.Auth.Commands.ChangePassword;
|
||||
using Heimdall.Application.Auth.Commands.RegisterAdmin;
|
||||
using Heimdall.Application.Auth.Commands.ConfirmEmail;
|
||||
using Heimdall.Application.Auth.Commands.ForgotPassword;
|
||||
using Heimdall.Application.Auth.Commands.ResetPassword;
|
||||
using Heimdall.Application.Auth.Commands.Enable2fa;
|
||||
using Heimdall.Application.Auth.Commands.Disable2fa;
|
||||
using Heimdall.Application.Auth.Commands.Logout;
|
||||
@@ -222,6 +225,61 @@ public class AuthController : ControllerBase
|
||||
return StatusCode(result.StatusCode ?? 400, new { error = result.Error });
|
||||
}
|
||||
|
||||
// ─── Email Confirmation ───────────────────────────────────────
|
||||
|
||||
/// <summary>Confirm email address with token.</summary>
|
||||
[AllowAnonymous]
|
||||
[HttpPost("confirm-email")]
|
||||
[ProducesResponseType(StatusCodes.Status200OK)]
|
||||
[ProducesResponseType(typeof(void), StatusCodes.Status400BadRequest)]
|
||||
public async Task<IActionResult> ConfirmEmail([FromBody] ConfirmEmailRequest request, CancellationToken ct)
|
||||
{
|
||||
_logger.LogInformation("ConfirmEmail attempt for UserId={UserId}", request.UserId);
|
||||
var result = await _mediator.Send(new ConfirmEmailCommand(request.UserId, request.Token), ct);
|
||||
|
||||
if (result.IsSuccess)
|
||||
{
|
||||
_logger.LogInformation("Email confirmed for UserId={UserId}", request.UserId);
|
||||
return Ok(new { message = "Email confirmed successfully" });
|
||||
}
|
||||
|
||||
_logger.LogWarning("Email confirmation failed for UserId={UserId}: {Error}", request.UserId, result.Error);
|
||||
return StatusCode(result.StatusCode ?? 400, new { error = result.Error });
|
||||
}
|
||||
|
||||
// ─── Forgot & Reset Password ──────────────────────────────────
|
||||
|
||||
/// <summary>Send password reset link via email.</summary>
|
||||
[AllowAnonymous]
|
||||
[HttpPost("forgot-password")]
|
||||
[ProducesResponseType(StatusCodes.Status200OK)]
|
||||
public async Task<IActionResult> ForgotPassword([FromBody] ForgotPasswordRequest request, CancellationToken ct)
|
||||
{
|
||||
_logger.LogInformation("ForgotPassword request for {Email}", request.Email);
|
||||
await _mediator.Send(new ForgotPasswordCommand(request.Email), ct);
|
||||
return Ok(new { message = "If the email exists, a reset link has been sent." });
|
||||
}
|
||||
|
||||
/// <summary>Reset password with token.</summary>
|
||||
[AllowAnonymous]
|
||||
[HttpPost("reset-password")]
|
||||
[ProducesResponseType(StatusCodes.Status200OK)]
|
||||
[ProducesResponseType(typeof(void), StatusCodes.Status400BadRequest)]
|
||||
public async Task<IActionResult> ResetPassword([FromBody] ResetPasswordRequest request, CancellationToken ct)
|
||||
{
|
||||
_logger.LogInformation("ResetPassword attempt for {Email}", request.Email);
|
||||
var result = await _mediator.Send(new ResetPasswordCommand(request.Email, request.Token, request.NewPassword), ct);
|
||||
|
||||
if (result.IsSuccess)
|
||||
{
|
||||
_logger.LogInformation("Password reset successfully for {Email}", request.Email);
|
||||
return Ok(new { message = "Password reset successfully" });
|
||||
}
|
||||
|
||||
_logger.LogWarning("Password reset failed for {Email}: {Error}", request.Email, result.Error);
|
||||
return StatusCode(result.StatusCode ?? 400, new { error = result.Error });
|
||||
}
|
||||
|
||||
/// <summary>Logout — revoke the current refresh token.</summary>
|
||||
[Authorize]
|
||||
[HttpPost("logout")]
|
||||
|
||||
Reference in new issue
Block a user