刷新令牌的正确方法

时间:2018-09-12 05:14:06

标签: ios swift alamofire

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)
        }
    }
}

2 个答案:

答案 0 :(得分:5)

为解决这个问题,我创建了一个类,我们将从中调用每个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(在您的情况下为“刷新令牌”)(可以是来自APIService.swift的任何请求)。

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

答案 1 :(得分:0)

您可以创建通用的复习类:

protocol IRefresher {
    associatedtype RefreshTarget: IRefreshing

    var target: RefreshTarget? { get }

    func launch(repeats: Bool, timeInterval: TimeInterval)
    func invalidate()
}

class Refresher<T: IRefreshing>: IRefresher {

    internal weak var target: T?
    private var timer: Timer?

    init(target: T?) {
        self.target = target
    }

    public func launch(repeats: Bool, timeInterval: TimeInterval) {
        timer = Timer.scheduledTimer(withTimeInterval: timeInterval, repeats: repeats) { [weak self] (timer) in
            self?.target?.refresh()
        }
    }

    public func invalidate() {
        timer?.invalidate()
    }
}

刷新目标协议:

protocol IRefreshing: class {
    func refresh()
}

定义新的类型别名:

typealias RequestManagerRefresher = Refresher<RequestManager>

现在创建刷新并存储:

class RequestManager {
    let refresher: AuthenticationRefresher

    init() {
        authenticationService.refresher = Refresher(target: self)
        refresher?.launchTimer(repeats: true, timeInterval: 15*60)
    }
}

并展开RequestManager:

extension RequestManager: IRefreshing {
    func refresh() {
        updateToken()
    }
}

每隔15分钟,您的RequestManager的令牌就会更新一次