import 'dart:async'; import 'package:flutter_riverpod/flutter_riverpod.dart'; import '../data/auth_repository.dart'; import '../../../../core/network/dio_client.dart'; final authRepositoryProvider = Provider((ref) => AuthRepository()); class AuthController extends AsyncNotifier { late AuthRepository _repository; @override FutureOr build() { _repository = ref.watch(authRepositoryProvider); } Future login(String email, String password) async { state = const AsyncValue.loading(); try { final data = await _repository.login(email, password); await DioClient().saveToken(data['token']); state = const AsyncValue.data(null); return true; } catch (e, st) { state = AsyncValue.error(e, st); return false; } } Future signup(String email, String password) async { state = const AsyncValue.loading(); try { await _repository.signup(email, password); state = const AsyncValue.data(null); return true; } catch (e, st) { state = AsyncValue.error(e, st); return false; } } Future verifyOtp(String email, String otp) async { state = const AsyncValue.loading(); try { final data = await _repository.verifyOtp(email, otp); await DioClient().saveToken(data['token']); state = const AsyncValue.data(null); return true; } catch (e, st) { state = AsyncValue.error(e, st); return false; } } Future forgotPassword(String email) async { state = const AsyncValue.loading(); try { await _repository.forgotPassword(email); state = const AsyncValue.data(null); return true; } catch (e, st) { state = AsyncValue.error(e, st); return false; } } Future resetPassword( String email, String otp, String newPassword, ) async { state = const AsyncValue.loading(); try { await _repository.resetPassword(email, otp, newPassword); state = const AsyncValue.data(null); return true; } catch (e, st) { state = AsyncValue.error(e, st); return false; } } Future logout() async { try { await DioClient().clearToken(); } catch (_) {} } } final authControllerProvider = AsyncNotifierProvider(() { return AuthController(); });