Skip to content

Commit

Permalink
Fix token retrieval right after creating it
Browse files Browse the repository at this point in the history
  • Loading branch information
LaurentTreguier committed Sep 18, 2024
1 parent c056f65 commit e0863e4
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 6 deletions.
4 changes: 1 addition & 3 deletions Fyreplace/Data/AuthenticationMiddleware.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ import HTTPTypes
import OpenAPIRuntime

struct AuthenticationMiddleware: ClientMiddleware {
private let keychain = Keychain(service: "connection.token")

func intercept(
_ request: HTTPRequest,
body: HTTPBody?,
Expand All @@ -13,7 +11,7 @@ struct AuthenticationMiddleware: ClientMiddleware {
next: @Sendable (HTTPRequest, HTTPBody?, URL) async throws -> (HTTPResponse, HTTPBody?)
) async throws -> (HTTPResponse, HTTPBody?) {
var request = request
let token = keychain.get()
let token = KeychainCache.shared(for: "connection.token").value

if !token.isEmpty {
request.headerFields[.authorization] = "Bearer \(token)"
Expand Down
7 changes: 4 additions & 3 deletions Fyreplace/Data/Keychain.swift
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ struct KeychainStorage: DynamicProperty {

init(_ key: String) {
keychain = .init(service: key)
cache = .shared(for: key, defaultValue: keychain.get())
cache = .shared(for: key)
}
}

Expand All @@ -94,12 +94,13 @@ class KeychainCache: ObservableObject {
value = defaultValue
}

static func shared(for key: String, defaultValue: String) -> KeychainCache {
static func shared(for key: String) -> KeychainCache {
if let instance = instances[key] {
return instance
}

let instance = KeychainCache(key: key, defaultValue: defaultValue)
let keychain = Keychain(service: key)
let instance = KeychainCache(key: key, defaultValue: keychain.get())
instances[key] = instance
return instance
}
Expand Down

0 comments on commit e0863e4

Please sign in to comment.