title: Refreshing bearer token and using AccessTokenPlugin
body: <p>For many of my endpoints, I require a bearer token to be passed with the request to authenticate the user. Because of that, I also need to refresh the user's token if it expires.</p>
  
  <p>I found <a href="https://stackoverflow.com/questions/54936080/refreshing-auth-token-with-moya">this question</a> to start me along, but I'm still unsure about some things and how to implement them.</p>
  
  <p>As the answer to the question above suggests, I've created an extension for <code>MoyaProvider</code>:</p>
  
  <pre><code>extension MoyaProvider {
      convenience init(handleRefreshToken: Bool) {
          if handleRefreshToken {
              self.init(requestClosure: MoyaProvider.endpointResolver())
          } else {
              self.init()
          }
      }
  
      static func endpointResolver() -&gt; MoyaProvider&lt;Target&gt;.RequestClosure {
          return { (endpoint, closure) in
              //Getting the original request
              let request = try! endpoint.urlRequest()
  
              //assume you have saved the existing token somewhere                
              if (#tokenIsNotExpired#) {                   
                  // Token is valid, so just resume the original request
                  closure(.success(request))
                  return
              }
  
              //Do a request to refresh the authtoken based on refreshToken
              authenticationProvider.request(.refreshToken(params)) { result in
                  switch result {
                  case .success(let response):
                      let token = response.mapJSON()["token"]
                      let newRefreshToken = response.mapJSON()["refreshToken"]
                      //overwrite your old token with the new token
                      //overwrite your old refreshToken with the new refresh token
  
                      closure(.success(request)) // This line will "resume" the actual request, and then you can use AccessTokenPlugin to set the Authentication header
                  case .failure(let error):
                      closure(.failure(error)) //something went terrible wrong! Request will not be performed
                  }
              }
      }
  }
  </code></pre>
  
  <p>I've also created an extension for <code>TargetType</code>, which looks like this:</p>
  
  <pre><code>import Moya
  
  public protocol TargetTypeExtension: TargetType, AccessTokenAuthorizable {}
  
  public extension TargetTypeExtension {
  
      var baseURL: URL { return URL(string: Constants.apiUrl)! }
  
      var headers: [String: String]? { return nil }
  
      var method: Moya.Method { return .get }
  
      var authorizationType: AuthorizationType { return .bearer }
  
      var sampleData: Data { return Data() }
  }
  </code></pre>
  
  <p>Here is one such implementation of it:</p>
  
  <pre><code>import Moya
  
  public enum Posts {
      case likePost(postId: Int)
      case getComments(postId: Int)
  }
  
  extension Posts: TargetTypeExtension {
      public var path: String {
          switch self {
          case .likePost(_): return "posts/like"
          case .getComments(_): return "posts/comments"
          }
      }
  
      public var authorizationType: AuthorizationType {
          switch self {
          case .likePost(_): return .bearer
          case .getComments(_): return .none
          }
      }
  
      public var method: Moya.Method {
          switch self {
          case .likePost, .getComments:
              return .post
          }
      }
  
      public var task: Task {
          switch self {
          case let .likePost(postId):
              return .requestParameters(parameters: ["postId": postId], encoding: JSONEncoding.default)
          case let .getComments(postId):
              return .requestParameters(parameters: ["postId": postId], encoding: JSONEncoding.default)
          }
      }
  }
  </code></pre>
  
  <p>As you can see, my <code>likePost</code> request requires a bearer token, while my <code>getComments</code> request does not.</p>
  
  <p>So I have a few questions:</p>
  
  <ol>
  <li>How would I modify my <code>MoyaProvider</code> extension to utilize Moya's <code>AccessTokenPlugin</code>, <a href="https://github.com/Moya/Moya/blob/master/docs/Authentication.md#access-token-auth" rel="nofollow noreferrer">found here</a>?</li>
  <li>How would I modify my <code>MoyaProvider</code> extension to NOT require a bearer token if the <code>AuthorizationType</code> is <code>.none</code> for a specific request?</li>
  </ol>
  
  <p>Thanks!</p>