Skip to content

Instantly share code, notes, and snippets.

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/4e5cb203c1d57be52a4b04069a147bc2 to your computer and use it in GitHub Desktop.
Save Sathasivamthirumoorthi/4e5cb203c1d57be52a4b04069a147bc2 to your computer and use it in GitHub Desktop.
Authrepository.cs
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