Skip to content

Handle JWT auth and requests in apps using the Swift Composable Architecture (TCA).

Notifications You must be signed in to change notification settings

4rays/jwt-auth-client

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

26 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

JWTAuth

A dependency client that handles JWT auth in apps using the Swift Composable Architecture (TCA).

SimpleKeychain

The library uses the default keychain via the SimpleKeychain library. If you want to use a different keychain, you can achieve that via prepareDependencies, for instance in the initializer of your App.

init() {
  prepareDependencies {
    $0.simpleKeychain = SimpleKeychain(...)
  }
}

JWT Auth

A liveValue of the JWTAuthClient is required.

extension JWTAuthClient: DependencyKey {
  static let liveValue = Self(
    baseURL: {
      // Your base URL here
    },
    refresh: { token in
      // Your refresh logic here
    }
  )
}

Once defined, you can use the following methods:

public func sendAuthenticated<T>(...) async throws -> T where T: Decodable

public func sendAuthenticated<Success, Failure>(...) async throws -> Result<Success, Failure>
where Success: Decodable, Failure: Decodable

The refreshExpiredToken parameter is used to determine if the client should retry the request if the token is expired. If set to false, the client forwards the server error if the token is expired.

Auth Tokens Client

The AuthTokensClient is a client that handles the auth tokens via one of the following methods:

public var save: @Sendable (AuthTokens) async throws -> Void
public var load: @Sendable () async throws -> AuthTokens?
public var destroy: @Sendable () async throws -> Void

You can access the current cached session using the @Shared(.sessionTokens) macro.

Note

Unless you call the refreshExpiredTokens method on the JWTAuthClient, you need to manually call the load method of AuthTokensClient to load the tokens.

Keychain Client

The KeychainClient is a client that handles the keychain via one of the following methods:

public var save: @Sendable (_ value: String, _ as: Keys) async throws -> Void
public var load: @Sendable (_ key: Keys) async -> String?
public var delete: @Sendable (_ key: Keys) async -> Void
public var reset: @Sendable () async -> Void

About

Handle JWT auth and requests in apps using the Swift Composable Architecture (TCA).

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages