Skip to main content

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_EXPIRED with 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​

StateMeaning
LOGGED_OUTNo session: the initial state, and the state after logout() or reconfigure()
LOGGED_INA member signed in, or a saved session was restored at launch
SESSION_EXPIREDThe 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​

CallWhat it is for
AFCore.authentication().sessionStateStateFlow<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​

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()
}
}
}
}

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.