2015-12-15 17 views
1

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

調用雲碼功能:

@IBAction func verifyCodeButtonTapped(sender: AnyObject) { 
    var verificationCode: String = verificationCodeTextField.text! 
    let textFieldText = verificationCodeTextField.text ?? "" 

    if verificationCode.utf16.count != 4 { 
     displayAlert("Error", message: "You must entert the 4 digit verification code sent yo your phone") 
    } else { 
     let params = ["verifyPhoneNumber" : 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

Twilio開發者傳道這裏。

在解析代碼中,您期望request.params.phoneVerificationCode,但是當您從iOS調用雲端函數時,您的文件爲let params = ["verifyPhoneNumber" : textFieldText]

所以,要麼把上面一行

let params = ["phoneVerificationCode" : textFieldText] 

,使其雲代碼匹配。或者將您的雲代碼更改爲

if (verificationCode == request.params.verifyPhoneNumber) { 

以便它與iOS代碼匹配。

+0

解決了我的問題!謝謝! – midgyb

相關問題