Swiftpack.co -  Q42/OAuthTokenManager as Swift Package
Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
Small library which handles the refreshing and invalidating of OAuth2 tokens
.package(url: "https://github.com/Q42/OAuthTokenManager.git", from: "0.1.0")



import OAuthTokenManager

// First, define your AccessToken and RefeshToken structs

struct AccessToken {
  let token: String  

struct RefreshToken {
  let token: String  

// Subclass the TokenManager

class MyTokenManager: TokenManager<AccessToken, RefreshToken {}

let tokenManager = MyTokenManager(
  accessToken: nil,
  refreshToken: nil,

// register the following handlers

manager.didUpdateTokens = { accessToken, refreshToken in
  // store the tokens in keychain

manager.didRequireRefresh = { (refreshToken, completion) in
  // call your API to refresh the accessToken, e.g
  let accessToken = AccessToken(token: "foo")
  completion(.success(accessToken, refreshToken))
  // or if you have a unauthorized error:
  // or if you have anothoer error

manager.didRequireLogin = { completion in
  // show a login screen to the user
  let accessToken = AccessToken(token: "foo")
  let refreshToken = RefreshToken(token: "bar")
  // when successful
  completion(.success(accessToken, refreshToken))

  // when cancelled
  // when an error occured

// now you can use the manager to perform actions which require a valid access token

struct MyResult {}

manager.withAccessToken(action: { (token, completion) in
  // perform api call

  // or when you receive a unauthorized error

  // or when you receive another error

}) { (result: ActionResult<MyResult>) in
  // here you can prosses

  switch result {
  case let .success(value):
  case let .error(error):


Stars: 1
Last commit: 1 week ago

Ad: Job Offers

iOS Software Engineer @ Perry Street Software
Perry Street Software is Jack’d and SCRUFF. We are two of the world’s largest gay, bi, trans and queer social dating apps on iOS and Android. Our brands reach more than 20 million members worldwide so members can connect, meet and express themselves on a platform that prioritizes privacy and security. We invest heavily into SwiftUI and using Swift Packages to modularize the codebase.

Release Notes

Maintenance update
1 week ago
  • Fix Swift compiler warning
  • Update Swift tools version to 5.4

Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco | API