first commit
This commit is contained in:
65
lib/features/auth/presentation/providers/auth_provider.dart
Normal file
65
lib/features/auth/presentation/providers/auth_provider.dart
Normal file
@@ -0,0 +1,65 @@
|
||||
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());
|
||||
});
|
||||
}
|
||||
}
|
||||
80
lib/features/auth/presentation/providers/auth_providers.dart
Normal file
80
lib/features/auth/presentation/providers/auth_providers.dart
Normal file
@@ -0,0 +1,80 @@
|
||||
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';
|
||||
|
||||
final authServiceProvider = Provider<AuthService>((ref) {
|
||||
final client = ref.watch(supabaseProvider);
|
||||
return AuthService(client);
|
||||
});
|
||||
|
||||
final authStateChangesProvider = StreamProvider<AuthState>((ref) {
|
||||
final service = ref.watch(authServiceProvider);
|
||||
return service.authStateChanges();
|
||||
});
|
||||
|
||||
final currentSessionProvider = StreamProvider<Session?>((ref) async* {
|
||||
final service = ref.watch(authServiceProvider);
|
||||
yield service.currentSession;
|
||||
await for (final event in service.authStateChanges()) {
|
||||
yield event.session;
|
||||
}
|
||||
});
|
||||
|
||||
final currentUserProvider = Provider<User?>((ref) {
|
||||
final service = ref.watch(authServiceProvider);
|
||||
return service.currentUser;
|
||||
});
|
||||
|
||||
final authControllerProvider =
|
||||
AutoDisposeAsyncNotifierProvider<AuthController, void>(
|
||||
AuthController.new,
|
||||
);
|
||||
|
||||
class AuthController extends AutoDisposeAsyncNotifier<void> {
|
||||
@override
|
||||
Future<void> build() async {}
|
||||
|
||||
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> 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> 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());
|
||||
});
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user