Guide to managing authentication sessions in Para for Swift applications
Para Swift SDK provides robust session management, automatically tracking authentication states and ensuring secure, seamless interactions. Effective session management is critical for security, usability, and reliability of your application.
Session States
Para manages sessions using the ParaSessionState enum:
unknown: Initial state before the status is determined.
inactive: SDK initialized but no active session.
restoring: The SDK is restoring a saved session at launch.
active: Session is active but user not fully logged in.
activeLoggedIn: User is fully logged in with an active session.
Observing Session States
Utilize SwiftUI’s Combine or other state management tools to observe changes:
@StateObject private var paraManager: ParaManager.onChange(of: paraManager.sessionState) { newState in switch newState { case .activeLoggedIn: print("User authenticated") case .inactive: print("Session inactive") default: print("Session state: \(newState)") }}
Session Persistence
The SDK saves the session to the Keychain after each sign-in and restores it automatically when ParaManager starts, so a signed-in user stays signed in across app launches. While it restores, sessionState is .restoring; it then moves to .activeLoggedIn for a signed-in user, to .active when the restored session isn’t fully logged in, or to .inactive when there’s no saved session or the saved one can’t be restored. A saved session is only restored for the same environment and API key. logout() clears it.Because a restored user is already signed in, check sessionState (or isFullyLoggedIn()) before showing your sign-in screen. On Para’s updated authentication flow, the authenticate methods throw if a user is already signed in; call logout() first to sign in a different user.
Session Duration
Para session length is configured per API key and can be set up to 30 days through the Configuration section of the or CLI. The Para API enforces the configured duration. Signing a message or transaction, or calling the session keep-alive method, can extend an active session according to that configuration.
Key Session Methods
isSessionActive() async throws -> Bool: Checks if the session is currently valid before performing authenticated operations.
isFullyLoggedIn() async throws -> Bool: Checks if the user is fully logged in with an active session.
exportSession(excludeSigners: Bool = false) async throws -> String: Exports session state as a string that can be used for advanced integration scenarios. Pass excludeSigners: true to leave out signing capabilities.
logout() async throws: Clears the current session, removes all website data from the WebView and the saved session from the Keychain, and resets the session state to inactive.
Maintaining Active Sessions
For long-running applications, check session status before performing sensitive operations:
When a session has expired, Para recommends initiating a full authentication flow rather than trying to refresh the session.
For Swift applications, always call logout() before reinitiating authentication when a session has expired to ensure all stored data is properly cleared.
import ParaSwiftfunc handleSessionExpiration() async { do { // When session expires, first clear storage try await paraManager.logout() // Then redirect to authentication screen await MainActor.run { // Navigate to authentication screen } } catch { // Handle error }}
Background Security
Clear sensitive data when app goes to background by logging out: