Manage authentication with AuthStore
masterThe pb.authStore service manages the authenticated token and user record.
Key features:
- Logout: Call
pb.authStore.clear()to clear the current session. - Listening: Listen to the
onChangestream to react to auth changes. - Persistence: The default
AuthStoreis not persistent. To persist auth state across app restarts, useAsyncAuthStorewith a storage layer likeshared_preferencesorhive.
Example: Persistent Auth with shared_preferences
final prefs = await SharedPreferences.getInstance();
final store = AsyncAuthStore(
save: (String data) async => prefs.setString('pb_auth', data),
initial: prefs.getString('pb_auth'),
);
final pb = PocketBase('http://example.com', authStore: store);final store = AsyncAuthStore(
save: (String data) async => prefs.setString('pb_auth', data),
initial: prefs.getString('pb_auth'),
);
final pb = PocketBase('http://example.com', authStore: store);