【问题标题】:Verification code always invalid Swift Parse验证码总是无效的 Swift Parse
【发布时间】:2015-12-14 23:02:23
【问题描述】:

我正在使用短信验证来验证用户。我的问题是,当我输入代码以验证时,我得到了无效代码。我一辈子都想不通为什么。

调用云代码函数:

@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.");
    }
});

【问题讨论】:

  • 为什么需要使用 .utf16? x &lt; 4 || x &gt; 4 也可以写成 x != 4。另外,您没有向我们展示验证码发送部分。

标签: ios swift parse-platform parse-cloud-code


【解决方案1】:

您的参数名称在 iOS 和 JS 代码之间不匹配。

verificationCodephoneVerificationCode

改变

let params = ["verificationCode" : textFieldText]

要使用相同的参数名称:

let params = ["phoneVerificationCode" : textFieldText]

编辑

我在代码中看到的其他问题:

iOS 代码的前两行根据 textField 的文本值创建一个变量和一个常量。去掉verificationCode 变量,只使用textFieldText 常量。

在您检查代码是否等效之前,我会在云代码中添加更多错误状态。首先检查参数是否存在以及预期的类型和长度:

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
}

然后你可以继续检查requestCodeverificationCode是否等价。

【讨论】:

  • 我改变了它,但仍然得到无效的代码!不过很好!
  • @m1234 我已经编辑了我的答案,以添加我在原始问题中的示例代码中看到的更多问题。
  • 感谢我已根据您的建议更新了我的代码,但我仍然收到无效代码
猜你喜欢
  • 1970-01-01
  • 2021-06-13
  • 1970-01-01
  • 1970-01-01
  • 2016-01-15
  • 1970-01-01
  • 2013-09-21
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多