我使用短信验证来验证用户。我的问题是,当我输入一个代码来验证我得到无效的代码。我不能为我的生活找出原因。
调用云代码功能:
@IBAction func verifyCodeButtonTapped(sender: AnyObject) {
var verificationCode: String = verificationCodeTextField.text!
let textFieldText = verificationCodeTextField.text ?? ""
if verificationCode.utf16.count < 4 || verificationCode.utf16.count > 4 {
displayAlert("Error", message: "You must entert the 4 digit verification code sent yo your phone")
} else {
let params = ["verificationCode" : textFieldText]
PFCloud.callFunctionInBackground("verifyPhoneNumber", withParameters: params, block: { (object: AnyObject?, error) -> Void in
if error == nil {
self.performSegueWithIdentifier("showVerifyCodeView", sender: self)
} else {
self.displayAlert("Sorry", message: "We couldnt verify you. Please check that you enterd the correct 4 digit code sent to your phone")
}
})
}
}
验证代码的云代码:
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.");
}
});
答案 0 :(得分:1)
您的参数名称在iOS和JS代码之间不匹配。
verificationCode
vs phoneVerificationCode
更改
let params = ["verificationCode" : textFieldText]
使用相同的参数名称:
let params = ["phoneVerificationCode" : textFieldText]
修改强>
我在代码中看到的其他问题:
iOS代码的前两行从textField的文本值创建变量和常量。摆脱verificationCode
变量,只使用textFieldText
常量。
在检查代码是否相同之前,我会向Cloud Code添加一些错误状态。首先检查参数是否存在以及预期的类型和长度:
var requestCode = request.params.phoneVerificationCode;
if ((typeof requestCode !== "string") || (requestCode.length !== 4)) {
// The verification code did not come through from the client
}
然后对用户对象的值执行相同的检查:
else if ((typeof verificationCode !== "string) || (verificationCode.length !== 4)) {
// There is not a verification code on the Parse User
}
然后,您可以继续检查requestCode
和verificationCode
是否相同。