V1 - Personal Account and Budgeting Done
Personal Account and Budgeting
This commit is contained in:
81
kifi-app/lib/features/auth/providers/auth_provider.dart
Normal file
81
kifi-app/lib/features/auth/providers/auth_provider.dart
Normal file
@@ -0,0 +1,81 @@
|
||||
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<void> {
|
||||
late AuthRepository _repository;
|
||||
|
||||
@override
|
||||
FutureOr<void> build() {
|
||||
_repository = ref.watch(authRepositoryProvider);
|
||||
}
|
||||
|
||||
Future<bool> login(String email, String password) async {
|
||||
state = const AsyncValue.loading();
|
||||
try {
|
||||
final data = await _repository.login(email, password);
|
||||
await DioClient().storage.write(key: 'jwt_token', value: data['token']);
|
||||
state = const AsyncValue.data(null);
|
||||
return true;
|
||||
} catch (e, st) {
|
||||
state = AsyncValue.error(e, st);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
Future<bool> 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<bool> verifyOtp(String email, String otp) async {
|
||||
state = const AsyncValue.loading();
|
||||
try {
|
||||
final data = await _repository.verifyOtp(email, otp);
|
||||
await DioClient().storage.write(key: 'jwt_token', value: data['token']);
|
||||
state = const AsyncValue.data(null);
|
||||
return true;
|
||||
} catch (e, st) {
|
||||
state = AsyncValue.error(e, st);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
Future<bool> 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<bool> 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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
final authControllerProvider = AsyncNotifierProvider<AuthController, void>(() {
|
||||
return AuthController();
|
||||
});
|
||||
Reference in New Issue
Block a user