2015-12-14 12 views
1

我使用短信驗證來驗證用戶。我的問題是,當我輸入代碼來驗證我得到無效的代碼。我不能爲了我的生活找出原因。驗證碼永遠無效快速解析

調用雲碼功能:

@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

爲什麼你需要使用.utf16? 'x < 4 || x > 4''可以寫成'x!= 4'。此外,您並未向我們顯示驗證碼發送部分。 – jcaron

回答

1

你的參數名稱是iOS和JS代碼之間的不匹配。

verificationCode VS phoneVerificationCode

變化

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是等同的。

+0

我改變了,但仍然得到無效的代碼!雖然有好點子! – m1234

+0

@ m1234我已編輯我的答案,以添加更多問題,我在原始問題中看到示例代碼。 – Brad

+0

謝謝我已經更新了我的代碼給你的建議,我仍然得到無效的代碼 – m1234