Right way to refresh the token

To solve this, I created a class from which we will call every API, say 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)
        })
    }

}

For calling the API from this call, we need to create a object of AlamofireRequestModal and override it with necessary parameter.

For example I created a file APIService.swift in which we have a method for 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)
    }

}

Explanation:

In code block:

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

I call getNewAccessToken API (say refresh-token, in your case), with the request( it could be any request based from APIService.swift).

When we get new token I save it user-defaults then I will update the request( the one I am getting as a parameter in refresh-token API call), and will pass the success and failure block as it is.


You can create generic refresher class:

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

And the refresh target protocol:

protocol IRefreshing: class {
    func refresh()
}

Define new typealias:

typealias RequestManagerRefresher = Refresher<RequestManager>

Now create refresher and store it:

class RequestManager {
    let refresher: RequestManagerRefresher

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

And expand RequestManager:

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

Every 15 minutes your RequestManager's token will be updated


UPDATE

Of course, you also can change the update time. Create a static var that storing update time you need. For example inside the RequestManager:

class RequestManager {
    static var updateInterval: TimeInterval = 0

    let refresher: RequestManagerRefresher

    init() {
        refresher = Refresher(target: self)
        refresher?.launch(repeats: true, timeInterval: updateInterval)
    }
}

So now you can ask the token provider server for token update interval and set this value to updateInterval static var:

backendTokenUpdateIntervalRequest() { interval in
    RequestManager.updateInterval = interval
}