juice_auth library

Authentication lifecycle management for Juice applications.

juice_auth provides a foundation bloc for authentication state, token management, and session lifecycle. It decouples auth logic from any specific provider (Firebase, Supabase, custom API) via the AuthProvider interface.

Quick Start

// 1. Register
BlocScope.register<AuthBloc>(
  () => AuthBloc(config: AuthConfig(
    providers: {'email': MyAuthProvider()},
  )),
  lifecycle: BlocLifecycle.permanent,
);

// 2. Login
BlocScope.get<AuthBloc>().loginWithEmail('user@example.com', 'pass');

// 3. React
class AuthGate extends StatelessJuiceWidget<AuthBloc> {
  AuthGate({super.key}) : super(groups: {AuthGroups.status});
  @override
  Widget onBuild(BuildContext context, StreamStatus status) {
    return bloc.state.isAuthenticated ? HomeScreen() : LoginScreen();
  }
}

Rebuild Groups

  • auth:status — login, logout, session expiry
  • auth:user — user profile changes
  • auth:session — token refresh, session update
  • auth:error — auth error occurred

Classes

ApiKeyCredentials
API key credentials (for service-to-service auth).
AuthBloc
Primary bloc for authentication lifecycle management.
AuthConfig
Configuration for AuthBloc.
AuthCredentials
Base class for provider-specific credentials.
AuthEvent
Base class for all auth events.
AuthGroups
Rebuild groups for targeted UI updates.
AuthProvider
Contract that any auth backend must implement.
AuthResult
Returned by AuthProvider after successful authentication or refresh.
AuthSession
Represents an active authentication session.
AuthState
Immutable authentication state.
AuthUser
Represents an authenticated user.
BiometricCredentials
Biometric unlock credentials (re-auth with stored token).
EmailCredentials
Email + password credentials.
InitializeAuthEvent
Initialize AuthBloc with configuration.
LoginEvent
Login with a specific provider and credentials.
LogoutEvent
Logout and clear all session data.
OAuthCredentials
OAuth credentials (from platform OAuth flow).
RefreshTokenEvent
Manually trigger a token refresh.
TokenExpiryEvent
Internal: emitted when the refresh timer fires before token expiry.
UpdateUserEvent
Update user profile in state (after profile edit, etc.).

Enums

AuthStatus
Authentication status.

Exceptions / Errors

AuthError
Base class for auth errors.
AuthProviderException
Exception thrown by AuthProvider implementations.
NoRefreshTokenError
No refresh token available to refresh with.
ProviderAuthError
Provider returned an error (wrong password, account locked, etc.).
RateLimitedError
Too many login attempts — rate limited.
RefreshFailedError
Token refresh failed.
RestoreFailedError
Session restore failed.
StorageAuthError
Secure storage operation failed.
UnknownProviderError
Unknown provider name in LoginEvent.