11using Microsoft . AspNetCore . Mvc ;
22using ValuationBackend . Models ;
33using ValuationBackend . Services ;
4+ using System . Threading . Tasks ;
45
56namespace ValuationBackend . Controllers
67{
@@ -9,10 +10,12 @@ namespace ValuationBackend.Controllers
910 public class AuthController : ControllerBase
1011 {
1112 private readonly IAuthService _authService ;
13+ private readonly PasswordResetService _passwordResetService ;
1214
13- public AuthController ( IAuthService authService )
15+ public AuthController ( IAuthService authService , PasswordResetService passwordResetService )
1416 {
1517 _authService = authService ;
18+ _passwordResetService = passwordResetService ;
1619 }
1720
1821 [ HttpPost ( "login" ) ]
@@ -46,14 +49,34 @@ public async Task<IActionResult> Logout([FromBody] LogoutRequest request)
4649 return Ok ( new { msg = "success" } ) ;
4750 }
4851
49- [ HttpPost ( "forgot-password" ) ]
50- public async Task < IActionResult > ForgotPassword ( [ FromBody ] ForgotPasswordRequest request )
52+ // --- New Password Reset Endpoints ---
53+
54+ [ HttpPost ( "request-password-reset" ) ]
55+ public async Task < IActionResult > RequestPasswordReset ( [ FromBody ] EmailDto dto )
5156 {
52- var result = await _authService . ForgotPasswordAsync ( request . Username ) ;
53- if ( ! result )
54- return NotFound ( new { msg = "User not found" } ) ;
57+ await _passwordResetService . RequestPasswordResetAsync ( dto . Email ) ;
58+ return Ok ( new { message = "If the email exists, an OTP has been sent." } ) ;
59+ }
5560
56- return Ok ( new { msg = "success" } ) ;
61+ [ HttpPost ( "verify-otp" ) ]
62+ public async Task < IActionResult > VerifyOtp ( [ FromBody ] OtpDto dto )
63+ {
64+ var valid = await _passwordResetService . VerifyOtpAsync ( dto . Email , dto . Otp ) ;
65+ if ( ! valid ) return BadRequest ( new { message = "Invalid or expired OTP." } ) ;
66+ return Ok ( new { message = "OTP verified." } ) ;
5767 }
68+
69+ [ HttpPost ( "reset-password" ) ]
70+ public async Task < IActionResult > ResetPassword ( [ FromBody ] ResetPasswordDto dto )
71+ {
72+ var success = await _passwordResetService . ResetPasswordAsync ( dto . Email , dto . Otp , dto . NewPassword ) ;
73+ if ( ! success ) return BadRequest ( new { message = "Invalid OTP or email." } ) ;
74+ return Ok ( new { message = "Password reset successful." } ) ;
75+ }
76+
77+ // --- DTOs for password reset ---
78+ public class EmailDto { public string Email { get ; set ; } }
79+ public class OtpDto { public string Email { get ; set ; } public string Otp { get ; set ; } }
80+ public class ResetPasswordDto { public string Email { get ; set ; } public string Otp { get ; set ; } public string NewPassword { get ; set ; } }
5881 }
5982}
0 commit comments