Skip to content

Commit dea204f

Browse files
authored
refactor(auth): change AuthClientID to Int to better visiblity of client usage (#656)
Co-authored-by: Guilherme Souza <[email protected]>
1 parent fd53cc6 commit dea204f

File tree

3 files changed

+27
-6
lines changed

3 files changed

+27
-6
lines changed

Sources/Auth/AuthClient.swift

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,22 @@ import Helpers
1414
import WatchKit
1515
#endif
1616

17-
typealias AuthClientID = UUID
17+
typealias AuthClientID = Int
18+
19+
struct AuthClientLoggerDecorator: SupabaseLogger {
20+
let clientID: AuthClientID
21+
let decoratee: any SupabaseLogger
22+
23+
func log(message: SupabaseLogMessage) {
24+
var message = message
25+
message.additionalContext["client_id"] = .integer(clientID)
26+
decoratee.log(message: message)
27+
}
28+
}
1829

1930
public final class AuthClient: Sendable {
20-
let clientID = AuthClientID()
31+
static let globalClientID = LockIsolated(0)
32+
let clientID: AuthClientID
2133

2234
private var api: APIClient { Dependencies[clientID].api }
2335
var configuration: AuthClient.Configuration { Dependencies[clientID].configuration }
@@ -71,13 +83,21 @@ public final class AuthClient: Sendable {
7183
/// - Parameters:
7284
/// - configuration: The client configuration.
7385
public init(configuration: Configuration) {
86+
clientID = AuthClient.globalClientID.withValue {
87+
$0 += 1
88+
return $0
89+
}
90+
7491
Dependencies[clientID] = Dependencies(
7592
configuration: configuration,
7693
http: HTTPClient(configuration: configuration),
7794
api: APIClient(clientID: clientID),
7895
codeVerifierStorage: .live(clientID: clientID),
7996
sessionStorage: .live(clientID: clientID),
80-
sessionManager: .live(clientID: clientID)
97+
sessionManager: .live(clientID: clientID),
98+
logger: configuration.logger.map {
99+
AuthClientLoggerDecorator(clientID: clientID, decoratee: $0)
100+
}
81101
)
82102

83103
Task { @MainActor in observeAppLifecycleChanges() }

Sources/Auth/Internal/Dependencies.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,10 @@ struct Dependencies: Sendable {
1515

1616
var urlOpener: URLOpener = .live
1717
var pkce: PKCE = .live
18+
var logger: (any SupabaseLogger)?
1819

1920
var encoder: JSONEncoder { configuration.encoder }
2021
var decoder: JSONDecoder { configuration.decoder }
21-
var logger: (any SupabaseLogger)? { configuration.logger }
2222
}
2323

2424
extension Dependencies {

Sources/Helpers/SupabaseLogger.swift

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public struct SupabaseLogMessage: Codable, CustomStringConvertible, Sendable {
3131
public let function: String
3232
public let line: UInt
3333
public let timestamp: TimeInterval
34-
public let additionalContext: JSONObject
34+
public var additionalContext: JSONObject
3535

3636
@usableFromInline
3737
init(
@@ -55,7 +55,8 @@ public struct SupabaseLogMessage: Codable, CustomStringConvertible, Sendable {
5555
}
5656

5757
public var description: String {
58-
let date = ISO8601DateFormatter.iso8601.value.string(from: Date(timeIntervalSince1970: timestamp))
58+
let date = ISO8601DateFormatter.iso8601.value.string(
59+
from: Date(timeIntervalSince1970: timestamp))
5960
let file = fileID.split(separator: ".", maxSplits: 1).first.map(String.init) ?? fileID
6061
var description = "\(date) [\(level)] [\(system)] [\(file).\(function):\(line)] \(message)"
6162
if !additionalContext.isEmpty {

0 commit comments

Comments
 (0)