Authentication

interface Authentication

Manages user authentication and session lifecycle.

This interface provides the core functions for user authentication, including signing in with various methods, signing out, and managing the session state. Implementations of this interface are responsible for communicating with the AdvantaHealth backend services and securely storing session information.

Key Operations

  • Sign in with credentials: Authenticate using a username and password via authenticateWithCredentials.

  • Sign in or create with external ID: Authenticate or provision a user based on an organization-specific identifier using signInOrCreateMember. This is typically used for systems that do not rely on eligibility files.

  • Sign out: Terminate the current session and clear all local authentication data with logout.

Usage Notes

  • All functions are suspend and must be invoked from a coroutine.

  • Network or server errors are propagated as exceptions (Throwable) and should be handled by the caller.

Common Scenarios

  1. Initial App Launch: Attempt to refresh an existing session. If it fails, direct the user to the sign-in screen.

  2. User Sign-In: Call authenticateWithCredentials with user-provided credentials. On success, navigate to the main part of the application.

  3. SSO/External ID Flow: Use signInOrCreateMember to authenticate a user with an identifier provided by an external system (e.g., an employee ID).

Properties

Link copied to clipboard

Observable session lifecycle state - the canonical place to observe whether a member is signed in.

Functions

Link copied to clipboard
abstract suspend fun authenticateWithCredentials(username: String, password: String): AFResult

Attempts to authenticate the user with username and password.

Link copied to clipboard
abstract suspend fun authenticateWithTokens(accessToken: String, refreshToken: String): AFResult

Authenticates by accepting tokens obtained outside the SDK ("BYO tokens" / host-injected flow).

Link copied to clipboard
abstract fun isLoggedIn(): Boolean

Checks whether there is an active authenticated session by inspecting the persisted access token.

Link copied to clipboard
abstract suspend fun logout(): AFResult

Signs the user out and clears local session state.

Link copied to clipboard
abstract suspend fun signInOrCreateMember(externalUserId: String, email: String? = null, firstName: String? = null, middleName: String? = null, lastName: String? = null, gender: String? = null, dateOfBirth: String? = null, phoneNumber: String? = null, address1: String? = null, address2: String? = null, city: String? = null, state: String? = null, zip: String? = null, attributes: Map<String, String?>? = null): AFResult

Attempts to authenticate a member using their externalUserId.

Link copied to clipboard
abstract fun subscribeToSessionState(onState: (SessionState) -> Unit, onError: (Throwable) -> Unit = {}): FlowSubscription

Swift-friendly callback wrapper around sessionState. Delivers state updates on the main thread; call FlowSubscription.close to stop receiving updates (e.g. in deinit / viewDidDisappear).