Skip to content

Commit

Permalink
Ensure SDK automatic configuration did happen before fetching current…
Browse files Browse the repository at this point in the history
…UserId
  • Loading branch information
andersio committed Jan 31, 2025
1 parent c8a038c commit 38b9f5e
Showing 1 changed file with 25 additions and 8 deletions.
33 changes: 25 additions & 8 deletions Sources/VitalCore/Core/Client/VitalClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,9 @@ public enum AuthenticateRequest {
authenticate: @Sendable (_ externalUserId: String) async throws -> AuthenticateRequest
) async throws {

// Make sure client has been setup & automaticConfiguration has been ran once
_ = shared

// Only one `identify` is allowed to run at any given time.
try await identifyParkingLot.semaphore.acquire()
defer { identifyParkingLot.semaphore.release() }
Expand Down Expand Up @@ -310,7 +313,7 @@ public enum AuthenticateRequest {
let claims: VitalSignInTokenClaims

do {
claims = try await Self._signIn(withRawToken: rawToken)
claims = try await Self._privateSignIn(withRawToken: rawToken)

} catch VitalJWTSignInError.alreadySignedIn {

Expand All @@ -319,7 +322,7 @@ public enum AuthenticateRequest {
// Sign-out the current user, then sign-in again.
await shared.signOut()

claims = try await Self._signIn(withRawToken: rawToken)
claims = try await Self._privateSignIn(withRawToken: rawToken)
}

authStrategy = .jwt(claims.environment)
Expand Down Expand Up @@ -347,11 +350,15 @@ public enum AuthenticateRequest {
withRawToken token: String,
configuration: Configuration = .init()
) async throws {
try await Self._signIn(withRawToken: token, configuration: configuration)

// Make sure client has been setup & automaticConfiguration has been ran once
_ = shared

try await Self._privateSignIn(withRawToken: token, configuration: configuration)
}

@discardableResult
internal static func _signIn(
internal static func _privateSignIn(
withRawToken token: String,
configuration: Configuration = .init()
) async throws -> VitalSignInTokenClaims {
Expand Down Expand Up @@ -409,7 +416,10 @@ public enum AuthenticateRequest {
}

public static var status: Status {
computeStatus(Self.shared)
// Make sure client has been initialized & automaticConfiguration has been ran once
let client = Self.shared

return computeStatus(client)
}

private static func computeStatus(_ client: VitalClient) -> Status {
Expand Down Expand Up @@ -449,14 +459,20 @@ public enum AuthenticateRequest {
}

public static var currentUserId: String? {
Current.startupParamsStorage.get()?.userId.uuidString
// Make sure client has been setup & automaticConfiguration has been ran once
_ = shared

return Current.startupParamsStorage.get()?.userId.uuidString
}

/// The last identified external user that is successfully processed by `identifyExternalUser`.
///
/// This is `nil` if you have never used `identifyExternalUser`, or if you have signed out the user explicitly.
public static var identifiedExternalUser: String? {
Current.startupParamsStorage.get()?.externalUserId
// Make sure client has been setup & automaticConfiguration has been ran once
_ = shared

return Current.startupParamsStorage.get()?.externalUserId
}

@objc(automaticConfigurationWithCompletion:)
Expand Down Expand Up @@ -487,12 +503,13 @@ public enum AuthenticateRequest {
@discardableResult
func evaluateParams() -> Bool {
if let params = Current.startupParamsStorage.get() {
VitalLogger.core.info("applied from startup params", source: "AutoConfig")

client.setConfiguration(
strategy: params.authStrategy,
configuration: Configuration(),
apiVersion: "v2"
)
VitalLogger.core.info("applied from startup params", source: "AutoConfig")
return true
} else {
return false
Expand Down

0 comments on commit 38b9f5e

Please sign in to comment.