Created
July 25, 2023 06:19
-
-
Save Sathasivamthirumoorthi/4e5cb203c1d57be52a4b04069a147bc2 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
public async Task<ServiceResponse<string>> Verify(string email, string otp) | |
{ | |
ServiceResponse<string> response = new ServiceResponse<string>(); | |
// Check if the provided email is valid. | |
if (!IsEmailValid(email)) | |
{ | |
response.Success = false; | |
response.Message = "Invalid email address."; | |
return response; | |
} | |
var user = await _dbContext.Users.FirstOrDefaultAsync( | |
u => u.Email!.ToLower() == email.ToLower() | |
); | |
// Check if a user with the given email was found. | |
if (user != null) | |
{ | |
// Check if the user has exceeded the maximum OTP resend limit. | |
if (user.OtpResendCount >= 3) | |
{ | |
response.Success = false; | |
response.Message = "Maximum OTP resend limit reached."; | |
return response; | |
} | |
// Check if the OTP provided matches the OTP stored for the user. | |
if (user.Otp == otp) | |
{ | |
if (user.OtpExpiration > DateTimeOffset.UtcNow) | |
{ | |
user.Otp = null; | |
user.IsVerified = true; | |
await _dbContext.SaveChangesAsync(); | |
response.Success = true; | |
response.Message = "OTP verification successful."; | |
return response; | |
} | |
else | |
{ | |
response.Success = false; | |
response.Message = "Your Otp has been expired , Please try again"; | |
} | |
} | |
else | |
{ | |
response.Success = false; | |
response.Message = "Invalid OTP , Please try again"; | |
} | |
} | |
else | |
{ | |
response.Success = false; | |
response.Message = "Invalid Email , Please try again"; | |
} | |
return response; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment