initAuth method
Initialize authentication by restoring the session from storage. Call this method when your application loads to restore user sessions.
Example:
await db.auth.initAuth();
if (db.auth.isAuthenticated()) {
print('User is logged in: ${db.auth.getUser()?.email}');
}
Implementation
Future<void> initAuth() async {
final token = await authStore?.getToken();
if (token != null && token.isNotEmpty) {
_token = token;
// Try to fetch current user
try {
await getCurrentUser();
} catch (e) {
// Token might be invalid, clear it
_token = null;
await authStore?.setToken('');
}
} else {
_token = null;
_user = null;
}
// Trigger auth state change callback
_callbacks.onAuthStateChange?.call(_user, _token);
}