refactor: auth

This commit is contained in:
cogwheel0
2025-08-29 12:58:56 +05:30
parent abc40b3958
commit f0b9e8e2b0
6 changed files with 73 additions and 34 deletions

View File

@@ -5,35 +5,53 @@ import '../../../core/providers/app_providers.dart';
/// Unified auth providers using the new auth state manager
/// These replace the old auth providers for better efficiency
/// Login action provider (schedules side-effect outside provider build)
final loginActionProvider = Provider.family<Future<bool>, Map<String, String>>(
(ref, credentials) async {
final authManager = ref.read(authStateManagerProvider.notifier);
/// Imperative auth actions wrapper to avoid side-effects during provider build
class AuthActions {
final Ref _ref;
AuthActions(this._ref);
final username = credentials['username']!;
final password = credentials['password']!;
final rememberCredentials = credentials['remember'] == 'true';
AuthStateManager get _auth =>
_ref.read(authStateManagerProvider.notifier);
// Defer the mutation to avoid changing other providers during build
return Future(() => authManager.login(
Future<bool> login(
String username,
String password, {
bool rememberCredentials = false,
}) {
// Defer mutation to a microtask to avoid provider-build side-effects
return Future(() => _auth.login(
username,
password,
rememberCredentials: rememberCredentials,
));
},
);
}
/// Silent login action provider - returns a callback to run later
final silentLoginActionProvider = Provider<Future<bool> Function()>((ref) {
final authManager = ref.read(authStateManagerProvider.notifier);
return () => authManager.silentLogin();
});
Future<bool> loginWithApiKey(
String apiKey, {
bool rememberCredentials = false,
}) {
return Future(() => _auth.loginWithApiKey(
apiKey,
rememberCredentials: rememberCredentials,
));
}
/// Logout action provider - returns a callback to run later
final logoutActionProvider = Provider<Future<void> Function()>((ref) {
final authManager = ref.read(authStateManagerProvider.notifier);
return () => authManager.logout();
});
Future<bool> silentLogin() {
return Future(() => _auth.silentLogin());
}
Future<void> logout() {
return Future(() => _auth.logout());
}
Future<void> refresh() {
return Future(() => _auth.refresh());
}
}
final authActionsProvider = Provider<AuthActions>((ref) => AuthActions(ref));
// Legacy action providers have been replaced by `authActionsProvider`
/// Check if saved credentials exist
final hasSavedCredentialsProvider2 = FutureProvider<bool>((ref) async {
@@ -70,11 +88,7 @@ final authStatusProvider = Provider<AuthStatus>((ref) {
return ref.watch(authStateManagerProvider.select((state) => state.status));
});
/// Helper provider to trigger auth refresh - returns a callback
final refreshAuthProvider = Provider<Future<void> Function()>((ref) {
final authManager = ref.read(authStateManagerProvider.notifier);
return () => authManager.refresh();
});
// Use `ref.read(authActionsProvider).refresh()` instead of refresh providers
/// Provider to watch for auth state changes and update API service
final authApiIntegrationProvider = Provider<void>((ref) {