supabase_hooks 0.0.7 copy "supabase_hooks: ^0.0.7" to clipboard
supabase_hooks: ^0.0.7 copied to clipboard

Unofficial Flutter Supabase life cycle adaptation for flutter_hooks.

Unofficial Supabase life cycle adaptation for flutter_hooks. See more in the original package: https://github.com/supabase/supabase-flutter

Note: This library is completely experimental and not recommended for production usage.

The idea is the same as in LifeHook, except that you can override more methods, specific to Supabase Auth life cycle

AuthRequiredState useAuthRequiredState({
  required final NavigatorState navigator,
}) => use(
      LifeHook(
        debugLabel: 'AuthRequiredState',
        state: AuthRequiredState(
          navigator: navigator,
        ),
      ),
    );

class AuthRequiredState extends SupabaseAuthRequiredLifeState {
  AuthRequiredState({
    required this.navigator,
  });
  final NavigatorState navigator;
  @override
  void onUnauthenticated() {
    /// Users will be sent back to the LoginPage if they sign out.
    if (mounted) {
      /// Users will be sent back to the LoginPage if they sign out.
      navigator
          .pushNamedAndRemoveUntil('/login', (final route) => false);
    }
  }
}

or use

AuthState useAuthState({
  required NavigatorState navigator,
}) => use(
      LifeHook(
        debugLabel: 'AuthState',
        state: AuthState(
          navigator: navigator,
        ),
      ),
    );

class AuthState extends SupabaseAuthLifeState {
  AuthRequiredState({
    required this.navigator,
  });
  final NavigatorState navigator;
  @override
  void onUnauthenticated() {
    if (mounted) {
      navigator
          .pushNamedAndRemoveUntil('/', (final route) => false);
    }
  }

  @override
  void onAuthenticated(final Session session) {
    if (mounted) {
      navigator
          .pushNamedAndRemoveUntil('/', (final route) => false);
    }
  }

  @override
  void onPasswordRecovery(final Session session) {}

  @override
  void onErrorAuthenticating(final String message) {
    navigator.context.showErrorSnackBar(message: message);
  }
}