Authentication state
To check if a user is authenticated, you can use theauthState variable from the TurnkeyProvider.
authState can be one of the following:
AuthState.authenticated: The user has a stored session.AuthState.unauthenticated: The user does not have a stored session.AuthState.loading: The provider is initializing. Turnkey is checking for any existing stored sessions or restoring state. UI should show a splash or progress indicator.
authState in any widget using a Consumer<TurnkeyProvider>:
lib/widgets/auth_status.dart
onSessionSelected callback in TurnkeyConfig to handle post-authentication logic, such as redirecting.
snippet
Listening to provider updates
TurnkeyProvider is a ChangeNotifier, so you can attach listeners to respond to internal state changes (e.g., when user/wallets are fetched or when authState changes). This pattern is useful when you want to mirror provider data into local widget state.
Below is an example (adapted from the demo app) that keeps a local selectedWallet/selectedAccount in sync with the provider:
lib/screens/dashboard.dart
Tips
- Use
addListener/removeListenerfor one-off reactions to state changes.- Use
Consumer,Selector, orcontext.selectwhen you want automatic rebuilds based on specific slices of provider state.- Avoid calling
setStateinside your listener after the widget is disposed — always guard withif (!mounted) return;orif (!context.mounted) return;.