66 lines
1.8 KiB
Dart
66 lines
1.8 KiB
Dart
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
|
import 'package:supabase_flutter/supabase_flutter.dart';
|
|
|
|
import '../../../../core/supabase/supabase_providers.dart';
|
|
import '../../data/services/auth_service.dart';
|
|
|
|
/// Provider for the AuthService.
|
|
final authServiceProvider = Provider<AuthService>((ref) {
|
|
final client = ref.watch(supabaseProvider);
|
|
return AuthService(client);
|
|
});
|
|
|
|
/// Provider for the current user's authentication state.
|
|
final authStateProvider = StreamProvider<AuthState>((ref) {
|
|
return ref.watch(authServiceProvider).onAuthStateChange;
|
|
});
|
|
|
|
/// Controller for authentication actions.
|
|
final authControllerProvider = AutoDisposeAsyncNotifierProvider<AuthController, void>(
|
|
AuthController.new,
|
|
);
|
|
|
|
class AuthController extends AutoDisposeAsyncNotifier<void> {
|
|
@override
|
|
Future<void> build() async {}
|
|
|
|
Future<void> login({required String email, required String password}) async {
|
|
state = const AsyncLoading();
|
|
state = await AsyncValue.guard(() async {
|
|
await ref.read(authServiceProvider).login(
|
|
email: email.trim(),
|
|
password: password,
|
|
);
|
|
});
|
|
}
|
|
|
|
Future<void> signup({
|
|
required String email,
|
|
required String password,
|
|
required String username,
|
|
}) async {
|
|
state = const AsyncLoading();
|
|
state = await AsyncValue.guard(() async {
|
|
await ref.read(authServiceProvider).signUp(
|
|
email: email.trim(),
|
|
password: password,
|
|
username: username.trim(),
|
|
);
|
|
});
|
|
}
|
|
|
|
Future<void> logout() async {
|
|
state = const AsyncLoading();
|
|
state = await AsyncValue.guard(() async {
|
|
await ref.read(authServiceProvider).logout();
|
|
});
|
|
}
|
|
|
|
Future<void> forgotPassword(String email) async {
|
|
state = const AsyncLoading();
|
|
state = await AsyncValue.guard(() async {
|
|
await ref.read(authServiceProvider).forgotPassword(email.trim());
|
|
});
|
|
}
|
|
}
|