刷新令牌的正确方法

问题描述 投票:2回答:1

getUser中有一个函数RequestManager class在我的VC中调用。

func getUser(onCompletion: @escaping (_ result: User?, error: String?) -> Void) {
    Alamofire.request(Router.getUser).responseJSON { (response) in
        // here is the work with response
    }
}

如果此请求返回403,则表示access_token已过期。我需要刷新令牌并重复我的VC请求。

现在的问题。

如何刷新令牌并以正确的方式重复请求?

MyViewControllergetUser方法处理错误和刷新令牌不是一个好主意,因为我有很多VCsrequest methods

我需要类似的东西:VC调用方法并获取User,即使令牌已过期,refreshToken也不能在所有请求方法中。

编辑

refreshToken方法

func refreshToken(onCompletion: @escaping (_ result: Bool?) -> Void) {
    Alamofire.request(Router.refreshToken).responseJSON { (response) in
        print(response)
        if response.response?.statusCode == 200 {
            guard let data = response.data else { return onCompletion(false) }
            let token = try? JSONDecoder().decode(Token.self, from: data)
            token?.setToken()
            onCompletion(true)
        } else {
            onCompletion(false)
        }
    }
}
ios swift alamofire
1个回答
6
投票

为了解决这个问题,我创建了一个类,我们将调用每个API,比如BaseService.swift

BaseService.swift:

import Foundation
import Alamofire
import iComponents

struct AlamofireRequestModal {
    var method: Alamofire.HTTPMethod
    var path: String
    var parameters: [String: AnyObject]?
    var encoding: ParameterEncoding
    var headers: [String: String]?

    init() {
        method = .get
        path = ""
        parameters = nil
        encoding = JSONEncoding() as ParameterEncoding
        headers = ["Content-Type": "application/json",
                   "X-Requested-With": "XMLHttpRequest",
                   "Cache-Control": "no-cache"]
    }
}

class BaseService: NSObject {

    func callWebServiceAlamofire(_ alamoReq: AlamofireRequestModal, success: @escaping ((_ responseObject: AnyObject?) -> Void), failure: @escaping ((_ error: NSError?) -> Void)) {

        // Create alamofire request
        // "alamoReq" is overridden in services, which will create a request here
        let req = Alamofire.request(alamoReq.path, method: alamoReq.method, parameters: alamoReq.parameters, encoding: alamoReq.encoding, headers: alamoReq.headers)

        // Call response handler method of alamofire
        req.validate(statusCode: 200..<600).responseJSON(completionHandler: { response in
            let statusCode = response.response?.statusCode

            switch response.result {
            case .success(let data):

                if statusCode == 200 {
                    Logs.DLog(object: "\n Success: \(response)")
                    success(data as AnyObject?)

                } else if statusCode == 403 {
                    // Access token expire
                    self.requestForGetNewAccessToken(alaomReq: alamoReq, success: success, failure: failure)

                } else {
                    let errorDict: [String: Any] = ((data as? NSDictionary)! as? [String: Any])!
                    Logs.DLog(object: "\n \(errorDict)")
                    failure(errorTemp as NSError?)
                }
            case .failure(let error):
                Logs.DLog(object: "\n Failure: \(error.localizedDescription)")
                failure(error as NSError?)
            }
        })
    }

}

extension BaseService {

    func getAccessToken() -> String {
        if let accessToken =  UserDefaults.standard.value(forKey: UserDefault.userAccessToken) as? String {
            return "Bearer " + accessToken
        } else {
            return ""
        }
    }

    // MARK: - API CALL
    func requestForGetNewAccessToken(alaomReq: AlamofireRequestModal, success: @escaping ((_ responseObject: AnyObject?) -> Void), failure: @escaping ((_ error: NSError?) -> Void) ) {

        UserModal().getAccessToken(success: { (responseObj) in
            if let accessToken = responseObj?.value(forKey: "accessToken") {
                UserDefaults.standard.set(accessToken, forKey: UserDefault.userAccessToken)
            }

            // override existing alaomReq (updating token in header)
            var request: AlamofireRequestModal = alaomReq
            request.headers = ["Content-Type": "application/json",
                               "X-Requested-With": "XMLHttpRequest",
                               "Cache-Control": "no-cache",
                               "X-Authorization": self.getAccessToken()]

            self.callWebServiceAlamofire(request, success: success, failure: failure)

        }, failure: { (_) in
            self.requestForGetNewAccessToken(alaomReq: alaomReq, success: success, failure: failure)
        })
    }

}

为了从这个调用中调用API,我们需要创建一个AlamofireRequestModal对象并用必要的参数覆盖它。

例如,我创建了一个文件APIService.swift,其中我们有一个getUserProfileData的方法。

APIService.swift:

import Foundation

let GET_USER_PROFILE_METHOD = "user/profile"

struct BaseURL {
    // Local Server
    static let urlString: String = "http://192.168.10.236: 8084/"
    // QAT Server
    // static let urlString: String = "http://192.171.286.74: 8080/"

    static let staging: String = BaseURL.urlString + "api/v1/"
}

class APIService: BaseService {

    func getUserProfile(success: @escaping ((_ responseObject: AnyObject?) -> Void), failure: @escaping ((_ error: NSError?) -> Void)) {

        var request: AlamofireRequestModal = AlamofireRequestModal()
        request.method = .get
        request.path = BaseURL.staging + GET_USER_PROFILE_METHOD
        request.headers = ["Content-Type": "application/json",
                           "X-Requested-With": "XMLHttpRequest",
                           "Cache-Control": "no-cache",
                           "X-Authorization": getAccessToken()]

        self.callWebServiceAlamofire(request, success: success, failure: failure)
    }

}

说明:

在代码块中:

else if statusCode == 403 {
    // Access token expire
    self.requestForGetNewAccessToken(alaomReq: alamoReq, success: success, failure: failure)
}

我使用请求调用getNewAccessToken API(在您的情况下称为refresh-token)(它可以是来自APIService.swift的任何请求)。

当我们获得新令牌时,我将其保存为user-defaults然后我将更新请求(我在刷新令牌API调用中作为参数获取的请求),并按原样传递成功和失败块。

© www.soinside.com 2019 - 2024. All rights reserved.