didvan-app/lib/views/authentication/authentication_state.dart

131 lines
3.8 KiB
Dart

import 'package:didvan/main.dart';
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';
import 'package:flutter/foundation.dart';
import 'package:home_widget/home_widget.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<void> 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(navigatorKey.currentContext!)
.showAlert(AlertData(message: service.errorMessage));
}
}
Future<String?> 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'];
await userProvider.setAndGetToken(newToken: token);
RequestService.token = token;
if (!kIsWeb) {
HomeWidget.saveWidgetData("token", token.toString());
}
await userProvider.getUserInfo();
appState = AppState.idle;
return token;
} else {
appState = AppState.failed;
ActionSheetUtils(navigatorKey.currentContext!)
.showAlert(AlertData(message: service.errorMessage));
}
return null;
}
Future<void> sendOtpToken() async {
final service = RequestService(
'${RequestHelper.otp}?username=$username',
useAutherization: false,
);
await service.httpGet();
}
Future<bool> 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(navigatorKey.currentContext!).showAlert(
AlertData(
message: service.isSuccess
? service.result['message']
: 'کد وارد شده صحیح نمی‌باشد',
),
);
return false;
}
Future<bool> 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(navigatorKey.currentContext!).showAlert(
AlertData(
message: 'کلمه عبور با موفقیت تغییر کرد',
aLertType: ALertType.success,
),
);
return true;
}
ActionSheetUtils(navigatorKey.currentContext!)
.showAlert(AlertData(message: service.errorMessage));
return false;
}
}