RxMoyaAuthenticatable 0.2.0

RxMoyaAuthenticatable 0.2.0

Maintained by Keita Oouchi.

  • By
  • keita.oouchi


Carthage compatible Version License Platform

Make your API token refreshable with RxMoyaAuthenticatable!

RxMoyaAuthenticatable provides standard requestClosure which refresh accessToken automatically on your needs!

This requestClosure intercept your requests, execute followings:

  • suspend all requests in a serial dispatch queue,
  • find persisted authentication,
  • refresh found authentication if needed,
  • resume other requests in a serial queue.

How to use

Adapt RefreshableAuthentication protocol to your Authentication entity:

public protocol RefreshableAuthentication {
  var isRefreshNeeded: Bool { get }
  var accessToken: String { get }
  func refresh() -> Single<Self>

  static func find() -> Self?

For example:

import RxSwift
import Moya
import RxMoyaAuthenticatable

struct SpotifyAuthentication: RefreshableAuthentication, Decodable {

  let accessToken: String
  var refreshToken: String!
  var createdAt: Date = Date()
  let expiresIn: Int

  var isRefreshNeeded: Bool {
    let threshold = TimeInterval(self.expiresIn / 2)
    let result = self.expiresAt < Date().addingTimeInterval(threshold)
    return result

  static func find() -> SpotifyAuthentication? {
    return SpotifyAuthenticationStore.find()

  func refresh() -> Single<SpotifyAuthentication> {
    let refreshToken = self.refreshToken
        .request(.refreshToken(refreshToken: self.refreshToken))
        .map { response -> SpotifyAuthentication in
          if var authentication = try? JSONDecoder().decode(SpotifyAuthentication.self, from: response.data) {
            authentication.refreshToken = refreshToken
            return authentication
          } else {
            throw SpotifyAPIError.mappingError
          onSuccess: { authentication in
            SpotifyAuthenticationStore.save(authentication: authentication)
          onError: { error in

extension SpotifyAuthentication {

  enum CodingKeys: String, CodingKey {
    case accessToken = "access_token"
    case refreshToken = "refresh_token"
    case expiresIn = "expires_in"

  var expiresAt: Date {
    return createdAt.addingTimeInterval(TimeInterval(expiresIn))

(See example implementation using Spotify's API here)

Using this and your API, accessToken will automatically refresh when isRefreshNeeded returns True:

let provider = MoyaProvider<SpotifyAPI>(
  requestClosure: RxMoyaAuthenticatable<SpotifyAPI, SpotifyAuthentication>().requestClosure


To run the example project, clone the repo, and see Example/Readme.md. In this example, we implemented Spotify's Authorization Code Flow.


Target Version
iOS => 11.0
Swift => 4.0
Moya/RxSwift => 11.0


RxMoyaAuthenticatable is available through CocoaPods or Carthage.


pod 'RxMoyaAuthenticatable'


github "Moya/Moya"
github "keitaoouchi/RxMoyaAuthenticatable"


keitaoouchi, [email protected]


RxMoyaAuthenticatable is available under the MIT license. See the LICENSE file for more info.