Forgot and Reset password methods (before Sendgrid implementation) - UserController
Sat Jun 29 2024 19:03:45 GMT+0000 (Coordinated Universal Time)
Saved by @iamkatmakhafola
//// Forgot password
//[HttpPost("ForgotPassword")]
//public async Task<IActionResult> ForgotPassword(ForgotPasswordViewModel model)
//{
// try
// {
// if (string.IsNullOrEmpty(model.Email))
// {
// return BadRequest("Email is required.");
// }
// var user = await _userManager.FindByEmailAsync(model.Email);
// if (user == null)
// {
// return Ok("Do not exist.");
// }
// var token = await _userManager.GeneratePasswordResetTokenAsync(user);
// var resetLink = Url.Action("ResetPassword", "User",
// new { token, email = user.Email },
// protocol: HttpContext.Request.Scheme);
// var mailtoLink = $"mailto:{model.Email}?subject=Password%20Reset&body=Reset%20your%20password%20by%20clicking%20the%20following%20link:%20{WebUtility.UrlEncode(resetLink)}";
// await SendResetPasswordEmail(model.Email, resetLink); // Send reset password email
// return Ok("Please check your email for password reset instructions.");
// }
// catch (Exception ex)
// {
// _logger.LogError(ex, "Error sending reset password email.");
// return StatusCode(StatusCodes.Status500InternalServerError, "Error sending reset password email.");
// }
//}
//[HttpPost]
//[Route("ForgotPassword")]
//public IActionResult ForgotPassword(ForgotPasswordViewModel model)
//{
// try
// {
// if (string.IsNullOrEmpty(model.Email))
// {
// return BadRequest("Email is required.");
// }
// var resetLink = $"mailto:{WebUtility.UrlEncode(model.Email)}?subject=Reset%20Password&body=Click%20the%20following%20link%20to%20reset%20your%20password:%20[RESET_LINK_HERE]";
// // Redirect the user to the mailto link
// return Redirect(resetLink);
// }
// catch (Exception)
// {
// return BadRequest("Failed");
// }
//}
// Reset password
//[HttpPost("ResetPassword")]
//public async Task<IActionResult> ResetPassword(ResetPasswordViewModel model)
//{
// if (!ModelState.IsValid)
// {
// return BadRequest("Invalid data.");
// }
// var user = await _userManager.FindByEmailAsync(model.Email);
// if (user == null)
// {
// return NotFound("User not found.");
// }
// var result = await _userManager.ResetPasswordAsync(user, model.Token, model.Password);
// if (result.Succeeded)
// {
// return Ok("Password has been reset successfully.");
// }
// return BadRequest("Error while resetting the password.");
//}
//private async Task SendResetPasswordEmail(string email, string resetLink)
//{
// try
// {
// var smtpClient = new SmtpClient(_configuration["Smtp:Host"])
// {
// Port = int.Parse(_configuration["Smtp:Port"]),
// Credentials = new NetworkCredential(_configuration["Smtp:Username"], _configuration["Smtp:Password"]),
// EnableSsl = bool.Parse(_configuration["Smtp:EnableSsl"]),
// };
// var mailMessage = new MailMessage
// {
// From = new MailAddress(_configuration["Smtp:From"]),
// Subject = "Reset Password",
// Body = $"<h4>Reset your password by <a href='{resetLink}'>clicking here</a></h4>",
// IsBodyHtml = true,
// };
// mailMessage.To.Add(email);
// await smtpClient.SendMailAsync(mailMessage);
// }
// catch (SmtpException ex)
// {
// _logger.LogError(ex, "Error sending reset password email.");
// throw;
// }
//}



Comments