Session Management
After a member signs in, AFCore owns the session: it stores it securely on the device, keeps it across app launches and refreshes tokens automatically. Your app only observes the session state and sends the member back to sign-in when a session can no longer be recovered.
What you can do
- Observe the session as a reactive stream on Android or with a callback on iOS.
- Route the first screen with a quick, local
isLoggedIn()check. - React to
SESSION_EXPIREDwith a clear "please sign in again" prompt. - Read the signed-in member's id with
AFCore.memberId(). - Sign the member out with
logout(), which clears their data from the device and stops their background work.
Session states
| State | Meaning |
|---|---|
LOGGED_OUT | No session: the initial state, and the state after logout() or reconfigure() |
LOGGED_IN | A member signed in, or a saved session was restored at launch |
SESSION_EXPIRED | The session could not be recovered; ask the member to sign in again |
Temporary problems such as no connection or a server error never end the session.
Key APIs
| Call | What it is for |
|---|---|
AFCore.authentication().sessionState | StateFlow<SessionState> with the current state and every change |
AFCore.shared.authentication().subscribeToSessionState(...) | Swift callback on the main thread; returns a FlowSubscription to close() |
AFCore.authentication().isLoggedIn() | Local, point-in-time check for initial routing |
AFCore.memberId() | The signed-in member's id, or null |
AFCore.authentication().logout() | End the session and clear the member's data |
authentication() throws before initialize(); guard early code with AFCore.isInitialized(). The top-level AFCore.sessionState and AFCore.isLoggedIn() are deprecated aliases of the same state.
Example
- Android (Kotlin)
- iOS (Swift)
lifecycleScope.launch {
repeatOnLifecycle(Lifecycle.State.STARTED) {
AFCore.authentication().sessionState.collect { state ->
when (state) {
SessionState.LOGGED_IN -> showHome()
SessionState.SESSION_EXPIRED -> showLogin("Your session has expired. Please sign in again.")
SessionState.LOGGED_OUT -> showLogin()
}
}
}
}
final class SessionObserver {
private var subscription: FlowSubscription?
func start() {
subscription = AFCore.shared.authentication().subscribeToSessionState(
onState: { [weak self] state in
switch state {
case .loggedIn: self?.showHome()
case .sessionExpired: self?.showLogin("Your session has expired. Please sign in again.")
default: self?.showLogin()
}
},
onError: { _ in }
)
}
deinit { subscription?.close() }
}
Close a FlowSubscription when you no longer need updates, typically in deinit.
Signing out
logout() removes the member's tokens, cached data and queued submissions, stops visit detection and SmartWalking auto-sync, and removes this install's push registration. If your app removes the push token itself, call messaging().deleteToken(...) before logout(). A different member signing in, or re-initializing for another environment, clears the previous member's data the same way. Clear your app's own member data (preferences, accepted terms) on sign-out too.
Learn more
See the API Reference. Complete integration guides are in AIDE Docs.