App Flow | Authentication¶
App Flow offers a robust and flexible authentication layer designed to manage user sessions and enforce secure access across the application.
State management is integrated using Riverpod, enabling shared application states, such as routing, to be easily accessed within the authentication logic.
Authentication Structure¶
The authentication architecture is built around the AuthService interface, which defines the core methods required for user authentication. By default, App Flow supports two implementations of AuthService by default, tailored to suit a production and testing environment:
SupabaseAuthService: For production use, integrated with Supabase's authentication API, configurable via the.envfile (refer to App Flow - Setup).FauxAuthService: A lightweight mock service for testing and development, operating independently of any backend.
AuthService Provider¶
The AuthService implementation is determined dynamically based on the presence of Supabase credentials. If credentials are missing, the FauxAuthService is used as a fallback.
Supported Methods¶
The AuthService interface defines the following methods for user authentication:
| Method | Description |
|---|---|
logIn |
Authenticates a user with email and password. |
logOut |
Ends the user's session. |
signUp |
Registers a new user. |
isLoggedIn |
Checks if the user is currently authenticated. |
onAuthStateChange |
Streams authentication state changes. |
initializeAuthListener |
Sets up routing behavior based on auth state changes. |
Authentication State Change Events¶
The AuthService interface includes an AuthState stream, which emits events corresponding to authentication state changes. The following events are emitted:
| Event | Description |
|---|---|
INITIAL_SESSION |
Triggered when an initial session is detected. |
PASSWORD_RECOVERY |
Triggered during password recovery flows. |
SIGNED_IN |
Triggered when a user successfully signs in. |
SIGNED_OUT |
Triggered when a user signs out. |
TOKEN_REFRESHED |
Triggered when an authentication token is refreshed. |
USER_UPDATED |
Triggered when user details are updated. |
USER_DELETED |
Triggered when a user account is deleted. |
MFA_CHALLENGE_VERIFIED |
Triggered when an MFA challenge is successfully verified. |
SupabaseAuthService¶
The SupabaseAuthService is production-ready implementation of AuthService that is integrated with Supabase for authentication.
For more information, refer to the Supabase Documentation:
Below is a snippet of the implemented logIn function.
FauxAuthService¶
The FauxAuthService provides a mock implementation of AuthService for testing purposes. It simulates user authentication without requiring a backend.
Below is a snippet of the implemented logIn function.
Authentication and Navigation¶
Auth Listener¶
Changes to the authentication state dynamically control navigation. This is handled in the implementation of initializeAuthListener. In the below example, signed-in users are redirected to the home page, while signed-out users are sent to the authentication page.
Authentication Guards¶
Route guards, such as AuthGuard, ensure secure navigation based on authentication status. Unauthorized users are redirected to the login page.
For more information refer to App Flow | Routing