Skip to content

Auth Providers

Source: pharmanet/lib/core/providers/auth_provider.dart

authStateProvider

final authStateProvider = StreamProvider<AuthState?>((ref) {
  return Supabase.instance.client.auth.onAuthStateChange;
});

Streams Supabase auth state changes. Emits AuthState with session and event on login/logout/token refresh.

currentUserProvider

final currentUserProvider = Provider<User?>((ref) {
  final authState = ref.watch(authStateProvider);
  return authState.value?.session?.user;
});

Derives the current Supabase User from the auth state stream.

userRoleProvider

Source: pharmanet/lib/core/providers/user_provider.dart

final userRoleProvider = NotifierProvider<UserRoleNotifier, UserRole>(UserRoleNotifier.new);
enum UserRole { public, seller }
Method Description
setRole(role) Set role from login/registration
clearRole() Reset to public on logout

The role determines which RootPage view is shown: - UserRole.public → Customer home - UserRole.seller → Pharmacist dashboard

Usage

final authState = ref.watch(authStateProvider);
final user = ref.watch(currentUserProvider);
final role = ref.watch(userRoleProvider);