Flutter: Listen to auth events

Receive a notification every time an auth event happens.

Examples

Listen to auth changes

final authSubscription = supabase.auth.onAuthStateChange.listen((data) \{
  final AuthChangeEvent event = data.event;
  final Session? session = data.session;

  print('event: $event, session: $session');

  switch (event) \{
    case AuthChangeEvent.initialSession:
    // handle initial session
    case AuthChangeEvent.signedIn:
    // handle signed in
    case AuthChangeEvent.signedOut:
    // handle signed out
    case AuthChangeEvent.passwordRecovery:
    // handle password recovery
    case AuthChangeEvent.tokenRefreshed:
    // handle token refreshed
    case AuthChangeEvent.userUpdated:
    // handle user updated
    case AuthChangeEvent.userDeleted:
    // handle user deleted
    case AuthChangeEvent.mfaChallengeVerified:
    // handle mfa challenge verified
  \}
\});

Listen to a specific event

final authSubscription = supabase.auth.onAuthStateChange.listen((data) \{
  final AuthChangeEvent event = data.event;
  if (event == AuthChangeEvent.signedIn) \{
    // handle signIn
  \}
\});

Unsubscribe from auth subscription

final authSubscription = supabase.auth.onAuthStateChange.listen((data) \{\});

authSubscription.cancel();