Skip to content

Instantly share code, notes, and snippets.

@Sathasivamthirumoorthi
Created July 25, 2023 06:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Sathasivamthirumoorthi/bf69f929333ef07d74a690b3b19ab9ec to your computer and use it in GitHub Desktop.
Save Sathasivamthirumoorthi/bf69f929333ef07d74a690b3b19ab9ec to your computer and use it in GitHub Desktop.
public async Task<ServiceResponse<ResetPasswordDto>> ResetPassword(ResetPasswordDto request)
{
ServiceResponse<ResetPasswordDto> response = new ServiceResponse<ResetPasswordDto>();
// Check if the provided email is not null.
if (request.Email is not null)
{
// Try to find a user with the given email in the database.
var user = await _dbContext.Users.FirstOrDefaultAsync(
u => u.Email!.ToLower() == request.Email.ToLower()
);
// Check if a user with the given email was found.
if (user != null)
{
if (user.IsVerified == false)
{
response.Success = false;
response.Message = "User Not Verified";
return response;
}
if (user.OtpExpiration > DateTimeOffset.UtcNow)
{
CreatePasswordHash(
request.NewPassword,
out byte[] passwordHash,
out byte[] passwordSalt
);
user.PasswordHash = passwordHash;
user.PasswordSalt = passwordSalt;
user.Otp = null;
user.OtpExpiration = null;
await _dbContext.SaveChangesAsync();
response.Message = "Password changed successfully.";
}
else
{
response.Success = false;
response.Message = "Your OTP is expired.";
return response;
}
}
else
{
response.Success = false;
response.Message = "Invalid Email Address.";
}
}
else
{
response.Success = false;
response.Message = "Email is Required.";
}
return response;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment