Created
July 25, 2023 06:25
-
-
Save Sathasivamthirumoorthi/0e929ed2534dff859235b23f60c32e19 to your computer and use it in GitHub Desktop.
Authrepository.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Forgot Password method for resetting the password | |
public async Task<ServiceResponse<string>> ForgotPassword(string email) | |
{ | |
var response = new ServiceResponse<string>(); | |
var user = await _dbContext.Users.FirstOrDefaultAsync( | |
u => u.Email!.ToLower() == email.ToLower() | |
); | |
// Check if the provided email is valid. | |
if (!IsEmailValid(email)) | |
{ | |
response.Success = false; | |
response.Message = "Invalid email address."; | |
return response; | |
} | |
// Check if the provided email exists in the database. | |
if (!await EmailExists(email)) | |
{ | |
response.Success = false; | |
response.Message = "Email does not exists"; | |
return response; | |
} | |
//If email exists generate OTP | |
OtpGenerator otpGenerator = new OtpGenerator(); | |
string otp = otpGenerator.GenerateOtp(); | |
// Get the current Indian time | |
DateTimeOffset indianTime = DateTimeOffset.UtcNow.ToOffset( | |
TimeZoneInfo.FindSystemTimeZoneById("India Standard Time").BaseUtcOffset | |
); | |
// Add the expiration time in minutes | |
DateTimeOffset otpExpiration = indianTime.AddMinutes(3); | |
user.Otp = otp; | |
user.OtpExpiration = otpExpiration; | |
user.IsVerified = false; | |
await _dbContext.SaveChangesAsync(); | |
var otpMessage = new Message( | |
new string[] { email }, | |
"Stint360 - Password Reset OTP", | |
$"Dear {email},\n\nYou have requested a password reset for your Stint360 account.\n\nYour OTP (One-Time Password) is: {otp}" | |
+ $"\n\nPlease use this OTP to reset your password within the specified time limit.\n\nIf you did not request this password reset, " | |
+ $"please ignore this message.\n\nThank you!" | |
); | |
_emailSender.SendEmail(otpMessage); | |
response.Data = "Please check your email for OTP."; | |
return response; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment