import 'package:didvan/models/enums.dart'; import 'package:didvan/models/view/alert_data.dart'; import 'package:didvan/providers/core.dart'; import 'package:didvan/providers/user.dart'; import 'package:didvan/services/network/request.dart'; import 'package:didvan/services/network/request_helper.dart'; import 'package:didvan/utils/action_sheet.dart'; class AuthenticationState extends CoreProvier { int _currentPageIndex = 0; String username = ''; String password = ''; String _verificationCode = ''; bool isShowCustomDialog = true; set currentPageIndex(int value) { _currentPageIndex = value; notifyListeners(); } int get currentPageIndex => _currentPageIndex; Future confirmUsername() async { appState = AppState.isolatedBusy; final RequestService service = RequestService( RequestHelper.confirmUsername, useAutherization: false, body: {'username': username}, ); await service.post(); if (service.isSuccess && service.result['confirmed']) { appState = AppState.idle; currentPageIndex++; } else { appState = AppState.failed; ActionSheetUtils.showAlert(AlertData(message: service.errorMessage)); } } Future login(UserProvider userProvider) async { appState = AppState.isolatedBusy; final RequestService service = RequestService( RequestHelper.login, useAutherization: false, body: {'username': username, "password": password}, ); await service.post(); if (service.isSuccess && service.result['loggedIn']) { final token = service.result['token']; appState = AppState.idle; await userProvider.setAndGetToken(newToken: token); RequestService.token = token; await userProvider.getUserInfo(); return token; } else { appState = AppState.failed; ActionSheetUtils.showAlert(AlertData(message: service.errorMessage)); } return null; } Future sendOtpToken() async { final service = RequestService( '${RequestHelper.otp}?username=$username', useAutherization: false, ); await service.httpGet(); } Future verifyOtpToken(String token) async { appState = AppState.isolatedBusy; final service = RequestService( RequestHelper.otp, body: { 'code': token, 'username': username, }, useAutherization: false, ); await service.post(); appState = AppState.idle; if (service.isSuccess) { _verificationCode = token; currentPageIndex++; return true; } ActionSheetUtils.showAlert( AlertData( message: service.isSuccess ? service.result['message'] : 'کد وارد شده صحیح نمی‌باشد', ), ); return false; } Future resetPassword(String password) async { appState = AppState.isolatedBusy; final service = RequestService( RequestHelper.changePassword, body: { 'code': _verificationCode, 'username': username, 'password': password, }, useAutherization: false, ); await service.put(); if (service.isSuccess) { ActionSheetUtils.showAlert( AlertData( message: 'کلمه عبور با موفقیت تغییر کرد', aLertType: ALertType.success, ), ); return true; } ActionSheetUtils.showAlert(AlertData(message: service.errorMessage)); return false; } }