ThunderIDState
ThunderIDState is the reactive authentication state object provided by the ThunderIDSwiftUI package. It is an ObservableObject that holds the current user, loading state, and initialization status, and exposes the underlying ThunderIDClient for direct API calls.
ThunderIDState is injected into the SwiftUI environment by the .thunderIDProvider(config:) modifier. Any view in the hierarchy can read it using @EnvironmentObject.
Setup
Apply .thunderIDProvider(config:) to your root view in your App struct:
import SwiftUI
import ThunderIDSwiftUI
@main
struct MyApp: App {
var body: some Scene {
WindowGroup {
ContentView()
.thunderIDProvider(config: ThunderIDConfig(
baseUrl: "https://localhost:8090",
clientId: "<your-client-id>",
afterSignInUrl: "io.thunderid.b2c://callback",
afterSignOutUrl: "io.thunderid.b2c://logout"
))
}
}
}
Access ThunderIDState in any child view:
struct ProfileView: View {
@EnvironmentObject var state: ThunderIDState
var body: some View {
Text(state.user?.displayName ?? "Not signed in")
}
}
Properties
| Property | Type | Description |
|---|---|---|
user | User? | The currently authenticated user. nil when not signed in. |
isSignedIn | Bool | true when user is not nil. |
isLoading | Bool | true while the SDK is initializing or a sign-in/sign-out operation is in progress. |
isInitialized | Bool | true after the SDK has completed initialization (whether or not sign-in succeeded). |
error | String? | A human-readable error message if initialization or the last auth operation failed. nil on success. |
client | ThunderIDClient | The underlying ThunderIDClient instance for direct API calls. |
i18n | ThunderIDI18n | The localization instance. Use setLocale(_:) to change the active language. |
Methods
refresh()
Re-fetches the current sign-in state from the token store and updates user. Call this after a sign-in or sign-out operation that you perform manually via state.client.
func refresh() async
Task {
_ = try? await state.client.signOut()
await state.refresh()
}
SignOutButton and SignIn call refresh() automatically. You only need to call it directly when driving auth operations through state.client yourself.
setLocale(_:)
Changes the active locale for all ThunderIDSwiftUI components. The selected locale is persisted to UserDefaults.
func setLocale(_ locale: String)
Button("Switch to French") {
state.setLocale("fr-FR")
}
Example: Conditional Rendering
Use state.isInitialized, state.isLoading, and state.isSignedIn to drive your root navigation:
struct RootView: View {
@EnvironmentObject var state: ThunderIDState
var body: some View {
Group {
if !state.isInitialized || state.isLoading {
ProgressView("Loading...")
} else if let error = state.error {
Text("Configuration error: \(error)")
.foregroundStyle(.red)
} else if state.isSignedIn {
HomeView()
} else {
AuthView()
}
}
}
}
Example: Accessing the Client Directly
Use state.client to call any ThunderIDClient method:
struct TokenDebugView: View {
@EnvironmentObject var state: ThunderIDState
@State private var tokenClaims: String = ""
var body: some View {
Text(tokenClaims)
.task {
if let token = try? await state.client.getAccessToken() {
let claims: [String: AnyCodable]? = try? state.client.decodeJwtToken(token)
tokenClaims = "\(claims ?? [:])"
}
}
}
}