2025-05-10 12:16:27 +03:30
|
|
|
import 'dart:async';
|
|
|
|
|
|
|
|
|
|
import 'package:flutter/material.dart';
|
2025-05-14 15:01:03 +03:30
|
|
|
import 'package:rasadyar_auth/auth.dart';
|
2025-05-17 15:24:06 +03:30
|
|
|
import 'package:rasadyar_auth/data/common/dio_error_handler.dart';
|
|
|
|
|
import 'package:rasadyar_auth/data/models/request/login_request/login_request_model.dart';
|
|
|
|
|
import 'package:rasadyar_auth/data/models/response/auth/auth_response_model.dart';
|
2025-05-14 15:01:03 +03:30
|
|
|
import 'package:rasadyar_auth/data/repositories/auth_repository_imp.dart';
|
2025-05-17 15:24:06 +03:30
|
|
|
import 'package:rasadyar_auth/data/services/token_storage_service.dart';
|
|
|
|
|
import 'package:rasadyar_auth/presentation/widget/captcha/logic.dart';
|
2025-05-10 12:16:27 +03:30
|
|
|
import 'package:rasadyar_core/core.dart';
|
|
|
|
|
|
|
|
|
|
enum AuthType { useAndPass, otp }
|
|
|
|
|
|
|
|
|
|
enum AuthStatus { init }
|
|
|
|
|
|
|
|
|
|
enum OtpStatus { init, sent, verified, reSend }
|
|
|
|
|
|
2025-05-11 11:49:51 +03:30
|
|
|
class AuthLogic extends GetxController {
|
2025-05-17 15:24:06 +03:30
|
|
|
GlobalKey<FormState> formKey = GlobalKey<FormState>();
|
|
|
|
|
|
2025-05-10 12:16:27 +03:30
|
|
|
Rx<GlobalKey<FormState>> formKeyOtp = GlobalKey<FormState>().obs;
|
|
|
|
|
Rx<GlobalKey<FormState>> formKeySentOtp = GlobalKey<FormState>().obs;
|
|
|
|
|
Rx<TextEditingController> phoneNumberController = TextEditingController().obs;
|
|
|
|
|
Rx<TextEditingController> passwordController = TextEditingController().obs;
|
|
|
|
|
Rx<TextEditingController> phoneOtpNumberController =
|
|
|
|
|
TextEditingController().obs;
|
|
|
|
|
Rx<TextEditingController> otpCodeController = TextEditingController().obs;
|
2025-05-14 15:01:03 +03:30
|
|
|
|
2025-05-17 15:24:06 +03:30
|
|
|
var captchaController = Get.find<CaptchaWidgetLogic>();
|
2025-05-10 12:16:27 +03:30
|
|
|
|
|
|
|
|
RxnString phoneNumber = RxnString(null);
|
2025-05-17 15:24:06 +03:30
|
|
|
RxBool isLoading = false.obs;
|
|
|
|
|
TokenStorageService tokenStorageService = diAuth.get<TokenStorageService>();
|
|
|
|
|
|
2025-05-10 12:16:27 +03:30
|
|
|
Rx<AuthType> authType = AuthType.useAndPass.obs;
|
|
|
|
|
Rx<AuthStatus> authStatus = AuthStatus.init.obs;
|
|
|
|
|
Rx<OtpStatus> otpStatus = OtpStatus.init.obs;
|
|
|
|
|
|
|
|
|
|
RxInt secondsRemaining = 120.obs;
|
2025-05-17 17:07:44 +03:30
|
|
|
int wsd = 120;
|
2025-05-10 12:16:27 +03:30
|
|
|
Timer? _timer;
|
|
|
|
|
|
2025-05-14 15:01:03 +03:30
|
|
|
AuthRepositoryImpl authRepository = diAuth.get<AuthRepositoryImpl>();
|
|
|
|
|
|
2025-05-10 12:16:27 +03:30
|
|
|
void startTimer() {
|
|
|
|
|
_timer?.cancel();
|
|
|
|
|
secondsRemaining.value = 120;
|
|
|
|
|
|
|
|
|
|
_timer = Timer.periodic(const Duration(seconds: 1), (timer) {
|
|
|
|
|
if (secondsRemaining.value > 0) {
|
|
|
|
|
secondsRemaining.value--;
|
|
|
|
|
} else {
|
|
|
|
|
timer.cancel();
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void stopTimer() {
|
|
|
|
|
_timer?.cancel();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
String get timeFormatted {
|
|
|
|
|
final minutes = secondsRemaining.value ~/ 60;
|
|
|
|
|
final seconds = secondsRemaining.value % 60;
|
|
|
|
|
return '${minutes.toString().padLeft(2, '0')}:${seconds.toString().padLeft(2, '0')}';
|
|
|
|
|
}
|
|
|
|
|
|
2025-05-14 15:01:03 +03:30
|
|
|
@override
|
|
|
|
|
void onInit() {
|
|
|
|
|
super.onInit();
|
2025-05-17 15:24:06 +03:30
|
|
|
tokenStorageService.init();
|
2025-05-14 15:01:03 +03:30
|
|
|
}
|
|
|
|
|
|
2025-05-10 12:16:27 +03:30
|
|
|
@override
|
|
|
|
|
void onReady() {
|
|
|
|
|
// TODO: implement onReady
|
|
|
|
|
super.onReady();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@override
|
|
|
|
|
void onClose() {
|
|
|
|
|
_timer?.cancel();
|
|
|
|
|
super.onClose();
|
|
|
|
|
}
|
2025-05-14 15:01:03 +03:30
|
|
|
|
2025-05-17 15:24:06 +03:30
|
|
|
bool _isFormValid() {
|
|
|
|
|
final isCaptchaValid =
|
|
|
|
|
captchaController.formKey.currentState?.validate() ?? false;
|
|
|
|
|
final isFormValid = formKey.currentState?.validate() ?? false;
|
|
|
|
|
return isCaptchaValid && isFormValid;
|
|
|
|
|
}
|
2025-05-14 15:01:03 +03:30
|
|
|
|
2025-05-17 15:24:06 +03:30
|
|
|
LoginRequestModel _buildLoginRequest() {
|
|
|
|
|
final phone = phoneNumberController.value.text;
|
|
|
|
|
final pass = passwordController.value.text;
|
|
|
|
|
final code = captchaController.textController.value.text;
|
|
|
|
|
final key = captchaController.captchaKey.value;
|
|
|
|
|
|
|
|
|
|
return LoginRequestModel.createWithCaptcha(
|
|
|
|
|
username: phone,
|
|
|
|
|
password: pass,
|
|
|
|
|
captchaCode: code,
|
|
|
|
|
captchaKey: key!,
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Future<void> submitLoginForm() async {
|
|
|
|
|
if (!_isFormValid()) return;
|
|
|
|
|
|
|
|
|
|
final loginRequestModel = _buildLoginRequest();
|
|
|
|
|
isLoading.value = true;
|
|
|
|
|
await safeCall<AuthResponseModel?>(
|
|
|
|
|
call: () => authRepository.login(authRequest: loginRequestModel.toJson()),
|
2025-05-17 17:07:44 +03:30
|
|
|
onSuccess: (result) async {
|
2025-05-17 15:24:06 +03:30
|
|
|
await tokenStorageService.saveRefreshToken(result!.refresh!);
|
|
|
|
|
await tokenStorageService.saveAccessToken(result!.access!);
|
|
|
|
|
//Get.offAndToNamed(Routes.home);
|
|
|
|
|
},
|
|
|
|
|
onError: (error, stackTrace) {
|
|
|
|
|
if (error is DioException) {
|
|
|
|
|
diAuth.get<DioErrorHandler>().handle(error);
|
|
|
|
|
}
|
|
|
|
|
captchaController.getCaptcha();
|
|
|
|
|
},
|
|
|
|
|
);
|
|
|
|
|
isLoading.value = false;
|
|
|
|
|
}
|
2025-05-17 17:07:44 +03:30
|
|
|
|
|
|
|
|
adder() {
|
|
|
|
|
tokenStorageService.tsss.value = (wsd++).toString();
|
|
|
|
|
|
|
|
|
|
}
|
2025-05-10 12:16:27 +03:30
|
|
|
}
|