添加受益人Swift后刷新令牌

时间:2018-09-19 13:19:29

标签: ios iphone swift api token

你好,我添加用户时刷新令牌时遇到问题

首先,我实现了获取用户个人资料的功能

//API Get Profile
func loadMemberProfil(completion: ((_ sub : [String: AnyObject]) -> Void)!) {
    //API get profile and Bearer token
    let token = HPWSLoginManager.shared().saveSuccessResponse.token
    let url = URL(string: "http://51.38.36.76:40/api/v1/profile")
    var request = URLRequest(url: url!)
    request.httpMethod = "GET"
    request.addValue("Bearer \(token!)", forHTTPHeaderField: "Authorization")
    //get information in token
    URLSession.shared.dataTask(with: request) { (data, response, error) in
        guard let data = data else { return }
        do {
            let json = try JSONSerialization.jsonObject(with: data, options: .mutableContainers) as! [String: AnyObject]
            let sub = json["sub"] as! [String: AnyObject]
            if completion != nil{
                completion(sub)
            }
        } catch {
            print("error")
        }
        }.resume()
}

第二个添加用户的功能

// add Beneficiary
func addBeneficiary() {
    var firstNameBeneficiary: String?
    var lastNameBeneficiary: String?
    var numberBeneficiary: String?

    // add value beneficiary
    firstNameBeneficiary = self.firstName.text
    lastNameBeneficiary = self.lastName.text
    numberBeneficiary = self.phoneViewModel?.phoneNumberCompletDisplay()

    let json: [String: Any] = [
        "usernumber": "+224625259239",
        "secretcode": "5555",
        "firstname": firstNameBeneficiary!,
        "lastname": lastNameBeneficiary!,
        "beneficiarynumber": numberBeneficiary!
    ]
    let jsonData = try? JSONSerialization.data(withJSONObject: json)

    //API Add beneficiary and Bearer token
    let token = HPWSLoginManager.shared().saveSuccessResponse.token
    let url = URL(string: "http://51.38.36.76:40/api/v1/addBeneficiary")
    var request = URLRequest(url: url!)
    request.httpMethod = "POST"
    request.addValue("Bearer \(token!)", forHTTPHeaderField: "Authorization")
    request.addValue("application/json", forHTTPHeaderField: "Content-Type")

    request.httpBody = jsonData

    let task = URLSession.shared.dataTask(with: request) { data, response, error in
        do {
            let responseJSON = try JSONSerialization.jsonObject(with: jsonData!, options: []) as! [String: AnyObject]
            print(responseJSON)
        } catch {
            print("error")
        }
    }
    task.resume()
}

最后,我检索用户输入的信息以通过其秘密代码提交验证(但秘密代码调用了自动更改令牌的登录功能)

@IBAction func continueClicked(sender: UIButton) {
    let loginPasswordViewController = HPLoginPasswordViewController(nibNameOrParentNib: nil)
    loginPasswordViewController.keyNotifSuccess = kNotifLoginSucces
    loginPasswordViewController.keyNotifFailed = kNotifLoginPasswordError

    loginPasswordViewController.completion = { (sucess, error) -> Void in
        if sucess {
            self.beneficiarySubject.onNext(HPBeneficiaryModel(firstName: self.firstName.text, lastName: self.lastName.text, tel: self.phoneViewModel?.phoneNumberCompletDisplay(), type: .beneficiary))
            self.navigationController?.popViewController(animated: false)
            self.addBeneficiary()
        }
    }
        // validation secret code
    self.loadMemberProfil { (sub) in
        DispatchQueue.main.async {
            self.navigationController?.pushViewController(loginPasswordViewController, animated: true)
            HPWSLoginManager.shared().phoneNumber = sub["usernumber"] as? String
        }
    }
}

所以我认为刷新令牌的问题是因为在控制台上出现错误403(密码错误)有人可以帮助我吗?

0 个答案:

没有答案