betterAuthStateProvider top-level property
The current AuthState, mirroring BetterAuthFlutter.authStateChanges.
The SDK stream replays the current state to every new listener, so this provider emits immediately on first watch — there is no waiting for the next transition. Use it to gate your app:
class AuthGate extends ConsumerWidget {
const AuthGate({super.key});
@override
Widget build(BuildContext context, WidgetRef ref) {
final auth = ref.watch(betterAuthStateProvider);
return auth.when(
data: (state) => switch (state) {
AuthInitial() || AuthLoading() => const SplashScreen(),
Authenticated(:final user) => HomeScreen(user: user),
Unauthenticated() => const SignInScreen(),
AuthError(:final error) => RetryScreen(error: error),
},
loading: () => const SplashScreen(),
error: (error, _) => RetryScreen(error: error),
);
}
}
For the common cases, currentUserProvider and isAuthenticatedProvider read straight through this provider, so there is still only one subscription to the SDK stream.
Implementation
final StreamProvider<AuthState> betterAuthStateProvider =
StreamProvider<AuthState>((ref) => BetterAuthFlutter.authStateChanges);