AuthManager<TProfileModel extends TetherModel<TProfileModel>> class

Manages user authentication and profile data synchronization with Supabase and a local SQLite database.

The AuthManager handles:

  • User sign-up, sign-in (password, OTP), and sign-out.
  • Listening to Supabase authentication state changes.
  • Fetching the user's profile from a specified Supabase table upon successful authentication.
  • Storing and updating the user's profile in a local SQLite table.
  • Clearing local profile data on sign-out or authentication errors.
  • Providing ValueNotifiers for the current User and the user's profile (TProfileModel) to enable reactive UI updates.

It requires a SupabaseClient for interacting with Supabase, a SqliteConnection for local database operations, and details about the profile table (both in Supabase and locally), along with a fromJson factory for the profile model.

Example:

// Assuming MyProfileModel.fromJson exists and MyProfileModel extends TetherModel<MyProfileModel>
final authManager = AuthManager<MyProfileModel>(
  supabaseClient: supabaseInstance,
  localDb: localSqliteConnection,
  supabaseProfileTableName: 'profiles', // Name of your Supabase profiles table
  localProfileTableName: 'user_profile', // Name of your local SQLite profile table
  profileFromJsonFactory: MyProfileModel.fromJson,
  tableSchemas: myAppTableSchemas, // Map<String, SupabaseTableInfo>
);

// Listen to user changes
authManager.currentUserNotifier.addListener(() {
  print('Current Supabase User: \${authManager.currentUserNotifier.value}');
});

// Listen to profile changes
authManager.currentProfileNotifier.addListener(() {
  print('Current User Profile: \${authManager.currentProfileNotifier.value}');
});

// Sign in
try {
  await authManager.signInWithPassword(email: 'user@example.com', password: 'password');
} catch (e) {
  print('Sign-in error: $e');
}

// Sign out
await authManager.signOut();

Constructors

AuthManager.new({required SupabaseClient supabaseClient, required SqliteConnection localDb, required String supabaseProfileTableName, required String localProfileTableName, required FromJsonFactory<TProfileModel> profileFromJsonFactory, required Map<String, SupabaseTableInfo> tableSchemas})
Creates an instance of AuthManager.

Properties

currentProfileNotifier ValueNotifier<TProfileModel?>
Notifies listeners about changes to the current user's profile (TProfileModel).
final
currentSession → Session?
Gets the current Supabase Session.
no setter
currentUser → User?
Gets the current authenticated Supabase User.
no setter
currentUserNotifier ValueNotifier<User?>
Notifies listeners about changes to the current Supabase User.
final
hashCode int
The hash code for this object.
no setterinherited
onAuthStateChange Stream<AuthState>
A stream of AuthState changes from the Supabase client.
no setter
runtimeType Type
A representation of the runtime type of the object.
no setterinherited

Methods

dispose() → void
Disposes of the AuthManager and its resources.
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
resendOtp({required String email, required OtpType type}) Future<void>
Resends an OTP to the user's email for a specified OtpType.
signInWithPassword({required String email, required String password}) Future<AuthResponse>
Signs in an existing user with email and password.
signInWithPhone({String? email, String? phone, String? emailRedirectTo, bool? shouldCreateUser, Map<String, dynamic>? data, String? captchaToken, OtpChannel channel = OtpChannel.sms}) Future<void>
signOut() Future<void>
Signs out the current user.
signUp({required String email, required String password, Map<String, dynamic>? data}) Future<AuthResponse>
Signs up a new user with email and password.
toString() String
A string representation of this object.
inherited
verifyOtp({String? email, String? phone, String? token, required OtpType type, String? redirectTo, String? captchaToken, String? tokenHash}) Future<AuthResponse>
Signs in a user using a one-time password (OTP) received via email or other methods.

Operators

operator ==(Object other) bool
The equality operator.
inherited