https://github.com/keitaoouchi/rxmoyaauthenticatable
Make your API token refreshable automatically.
https://github.com/keitaoouchi/rxmoyaauthenticatable
alamofire moya oauth2 rxswift
Last synced: 8 months ago
JSON representation
Make your API token refreshable automatically.
- Host: GitHub
- URL: https://github.com/keitaoouchi/rxmoyaauthenticatable
- Owner: keitaoouchi
- License: mit
- Created: 2018-05-02T04:07:41.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2018-05-06T14:17:16.000Z (over 7 years ago)
- Last Synced: 2024-03-15T07:49:15.292Z (over 1 year ago)
- Topics: alamofire, moya, oauth2, rxswift
- Language: Swift
- Homepage:
- Size: 33.2 KB
- Stars: 13
- Watchers: 1
- Forks: 2
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# RxMoyaAuthenticatable
[](https://github.com/Carthage/Carthage)
[](https://cocoapods.org/pods/RxMoyaAuthenticatable)
[](https://cocoapods.org/pods/RxMoyaAuthenticatable)
[](https://cocoapods.org/pods/RxMoyaAuthenticatable)
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:
```swift
public protocol RefreshableAuthentication {
var isRefreshNeeded: Bool { get }
var accessToken: String { get }
func refresh() -> Single
static func find() -> Self?
}
```
For example:
```swift
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 {
let refreshToken = self.refreshToken
return
SpotifyAuthAPI
.sharedProvider
.rx
.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
}
}.do(
onSuccess: { authentication in
SpotifyAuthenticationStore.save(authentication: authentication)
},
onError: { error in
print(error)
}
)
}
}
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](https://github.com/keitaoouchi/RxMoyaAuthenticatable/blob/master/Example/RxMoyaAuthenticatable/SpotifyAuthentication.swift))
Using this and your API, accessToken will automatically refresh when `isRefreshNeeded` returns True:
```swift
let provider = MoyaProvider(
requestClosure: RxMoyaAuthenticatable().requestClosure
)
```
## Example
To run the example project, clone the repo, and see [Example/Readme.md](https://github.com/keitaoouchi/RxMoyaAuthenticatable/blob/master/Example/README.md).
In this example, we implemented Spotify's [Authorization Code Flow](https://beta.developer.spotify.com/documentation/general/guides/authorization-guide/).
## Requirements
| Target | Version |
|-------------------|----------|
| iOS | => 11.0 |
| Swift | => 4.0 |
| Moya/RxSwift | => 11.0 |
## Installation
RxMoyaAuthenticatable is available through [CocoaPods](https://cocoapods.org) or [Carthage](https://github.com/Carthage/Carthage).
### CocoaPods
```ruby
pod 'RxMoyaAuthenticatable'
```
### Carthage
```ruby
github "Moya/Moya"
github "keitaoouchi/RxMoyaAuthenticatable"
```
## Author
keitaoouchi, keita.oouchi@gmail.com
## License
RxMoyaAuthenticatable is available under the MIT license. See the LICENSE file for more info.