public IActionResult verifyOTP(VerficationViewModel model) { DTO dto = new DTO(); try { IdentityUser user = repository.User.FindOneByCondition(u => u.PhoneNumber.Equals(model.phone)); phoneOTP phoneOTP = repository.PhoneOTP.FindOneByCondition(o => o.user_Id == user.Id); if (phoneOTP.OTP == model.OTP && phoneOTP.phone == model.phone && phoneOTP.isValid == true) { user.PhoneNumberConfirmed = true; phoneOTP.isValid = false; repository.save(); string token = getToken(user).Value; return(Ok(token)); } else { dto.success = false; dto.message = "OTP is invalid"; return(BadRequest(dto)); } } catch (Exception ex) { logger.Error(ex.ToString()); logger.Warning(ex.StackTrace); dto.success = false; dto.message = "Operation has failed"; return(BadRequest(dto)); } }
public IActionResult sendOTP(string userId, string phone) { try { TwilioClient.Init(twilioSettings.AccountSid, twilioSettings.AuthToken); string allowedNums = "0123456789"; Random random = new Random(); char[] chars = new char[6]; for (int i = 0; i < chars.Length; i++) { chars[i] = allowedNums[(int)(chars.Length * random.NextDouble())]; } string OTP = new string(chars); var message = MessageResource.Create( body: $"Aphrie Verification Code is : {OTP}", from: new Twilio.Types.PhoneNumber("+18133287387"), to: new Twilio.Types.PhoneNumber($"+2{phone}") ); phoneOTP phoneOTP = new phoneOTP() { user_Id = userId, OTP = int.Parse(OTP), phone = phone, isValid = true }; repository.PhoneOTP.Add(phoneOTP); repository.save(); return(Ok(message)); } catch (Exception ex) { logger.Error(ex.ToString()); logger.Warning(ex.StackTrace); DTO dto = new DTO() { success = false, message = "Sending OTP Code has failed" }; return(BadRequest(dto)); } }