Im trying to use phone number verification in my app. During sign up the user will be asked for their phone number and be sent a verification code and they will enter the code and if it matches they can proceed. I have the cloud code ready but I'm not sure how exactly to call this from the app. Any help would be appreciated
What I want to be able to do is when they enter their phone number and press continue, first check that the phone number is a valid phone number, then send the sms code. finally segue to a verification view where they enter the code, if it doesn't match display an alert, if it does, log the user in
Cloud code:
var twilioAccountSid = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx';
var twilioAuthToken = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx';
var twilioPhoneNumber = '+1555-555-5555';
var secretPasswordToken = '1234';
var twilio = require('twilio')(twilioAccountSid, twilioAuthToken);
Parse.Cloud.define("sendVerificationCode", function(request, response) {
var verificationCode = Math.floor(Math.random()*999999);
var user = Parse.User.current();
user.set("phoneVerificationCode", verificationCode);
user.save();
twilio.sendSms({
From: twilioPhoneNumber,
To: request.params.phoneNumber,
Body: "Your verification code is " + verificationCode + "."
}, function(err, responseData) {
if (err) {
response.error(err);
} else {
response.success("Success");
}
});
});
Parse.Cloud.define("verifyPhoneNumber", function(request, response) {
var user = Parse.User.current();
var verificationCode = user.get("phoneVerificationCode");
if (verificationCode == request.params.phoneVerificationCode) {
user.set("phoneNumber", request.params.phoneNumber);
user.save();
response.success("Success");
} else {
response.error("Invalid verification code.");
}
});
This is where i call the cloud code:
@IBOutlet weak var continueButton: UIButton!
@IBOutlet weak var phoneNumberTextField: UITextField!
@IBAction func continueButtonTapped(sender: AnyObject) {
var phoneNumber = self.phoneNumberTextField.text
if phoneNumber?.utf16.count < 10 {
displayAlert("Error", message: "You must enter a valid 10 digit US phone number including area code")
} else {
let params = phoneNumber
PFCloud.callFunctionInBackground("sendVerificationCode", withParameters: params, block: { (object: AnyObject?, error) -> Void in
if error == nil {
self.performSegueWithIdentifier("showVerifyUserView", sender: self)
} else {
// Do error handling
}
})
}
}