Reputation: 1454
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
}
})
}
}
Upvotes: 1
Views: 2473
Reputation: 4654
You should use this method:
public class func callFunctionInBackground(function: String, withParameters parameters: [NSObject : AnyObject]?, block: PFIdResultBlock?)
You can call your function this way:
PFCloud.callFunctionInBackground("sendVerificationCode",
withParameters: /* any needed params here, for example mobile phone*/)
{ [unowned self](object:AnyObject?, error:NSError?) -> Void in
guard error == nil else {
//handle error
return
}
// everything ok
// notify your app that code is send successfully
}
Then when user enters the code do the same:
PFCloud.callFunctionInBackground("verifyPhoneNumber",
withParameters: /* any needed params here, for example verification code*/)
{ [unowned self](object:AnyObject?, error:NSError?) -> Void in
guard error == nil else {
//handle wrong notification code
return
}
// code is ok, notify your app verification is complete
}
Don't forget to deploy your code to parse with command line utility + take into account that common way to send parameters to cloud code is to send dictionaries
Update, how to send parameters:
PFCloud.callFunctionInBackground("functionName",
withParameters: ["phoneNumber" : phoneNumber)
{ [unowned self](object:AnyObject?, error:NSError?) -> Void in
...
}
Upvotes: 3
Reputation: 3154
Assuming you've deployed your code to Parse (command line "parse deploy"), you call cloud functions using PFCloud.callFunctionInBackground(). See https://www.parse.com/docs/ios/api/Classes/PFCloud.html
Upvotes: 0