feat : auth for new module
This commit is contained in:
@@ -5,6 +5,8 @@ import 'package:rasadyar_auth/auth.dart';
|
||||
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';
|
||||
import 'package:rasadyar_auth/data/models/response/user_info/user_info_model.dart';
|
||||
import 'package:rasadyar_auth/data/models/response/user_profile_model/user_profile_model.dart';
|
||||
import 'package:rasadyar_auth/data/repositories/auth_repository_imp.dart';
|
||||
import 'package:rasadyar_auth/data/services/token_storage_service.dart';
|
||||
import 'package:rasadyar_auth/data/utils/safe_call.dart';
|
||||
@@ -34,6 +36,7 @@ class AuthLogic extends GetxController {
|
||||
|
||||
RxnString phoneNumber = RxnString(null);
|
||||
RxBool isLoading = false.obs;
|
||||
RxBool isDisabled = true.obs;
|
||||
TokenStorageService tokenStorageService = Get.find<TokenStorageService>();
|
||||
|
||||
Rx<AuthType> authType = AuthType.useAndPass.obs;
|
||||
@@ -108,7 +111,7 @@ class AuthLogic extends GetxController {
|
||||
);
|
||||
}
|
||||
|
||||
Future<void> submitLoginForm() async {
|
||||
/*Future<void> submitLoginForm() async {
|
||||
if (!_isFormValid()) return;
|
||||
iLog('module222 : ${_module.toString()}');
|
||||
final loginRequestModel = _buildLoginRequest();
|
||||
@@ -128,5 +131,73 @@ class AuthLogic extends GetxController {
|
||||
},
|
||||
);
|
||||
isLoading.value = false;
|
||||
}*/
|
||||
|
||||
Future<void> submitLoginForm2() async {
|
||||
if (!_isFormValid()) return;
|
||||
|
||||
isLoading.value = true;
|
||||
await safeCall<UserProfileModel?>(
|
||||
call: () => authRepository.login(
|
||||
authRequest: {
|
||||
"username": phoneNumberController.value.text,
|
||||
"password": passwordController.value.text,
|
||||
},
|
||||
),
|
||||
onSuccess: (result) async {
|
||||
await tokenStorageService.saveModule(_module);
|
||||
await tokenStorageService.saveAccessToken(result?.accessToken ?? '');
|
||||
await tokenStorageService.saveRefreshToken(result?.accessToken ?? '');
|
||||
},
|
||||
onError: (error, stackTrace) {
|
||||
if (error is DioException) {
|
||||
diAuth.get<DioErrorHandler>().handle(error);
|
||||
}
|
||||
captchaController.getCaptcha();
|
||||
},
|
||||
);
|
||||
isLoading.value = false;
|
||||
}
|
||||
|
||||
Future<void> getUserInfo(String value) async {
|
||||
isLoading.value = true;
|
||||
await safeCall<UserInfoModel?>(
|
||||
call: () async => await authRepository.getUserInfo(value),
|
||||
onSuccess: (result) async {
|
||||
if (result != null) {
|
||||
diAuth.registerSingleton<DioRemote>(
|
||||
DioRemote(baseUrl: result.backend),
|
||||
instanceName: 'newDioRemote',
|
||||
);
|
||||
await tokenStorageService.saveApiKey(result.apiKey ?? '');
|
||||
await tokenStorageService.saveBaseUrl(result.backend ?? '');
|
||||
}
|
||||
},
|
||||
onError: (error, stackTrace) {
|
||||
if (error is DioException) {
|
||||
diAuth.get<DioErrorHandler>().handle(error);
|
||||
}
|
||||
captchaController.getCaptcha();
|
||||
},
|
||||
);
|
||||
isLoading.value = false;
|
||||
}
|
||||
|
||||
GetSnackBar _errorSnackBar(String message) {
|
||||
return GetSnackBar(
|
||||
titleText: Text(
|
||||
'خطا',
|
||||
style: AppFonts.yekan14.copyWith(color: Colors.white),
|
||||
),
|
||||
messageText: Text(
|
||||
message,
|
||||
style: AppFonts.yekan12.copyWith(color: Colors.white),
|
||||
),
|
||||
backgroundColor: AppColor.error,
|
||||
margin: EdgeInsets.symmetric(horizontal: 12, vertical: 8),
|
||||
borderRadius: 12,
|
||||
duration: Duration(milliseconds: 3500),
|
||||
snackPosition: SnackPosition.TOP,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -27,7 +27,7 @@ class AuthPage extends GetView<AuthLogic> {
|
||||
}
|
||||
}, controller.authType),
|
||||
|
||||
SizedBox(height: 50),
|
||||
/* SizedBox(height: 50),
|
||||
RichText(
|
||||
text: TextSpan(
|
||||
children: [
|
||||
@@ -80,7 +80,7 @@ class AuthPage extends GetView<AuthLogic> {
|
||||
],
|
||||
),
|
||||
);
|
||||
}, controller.authType),
|
||||
}, controller.authType),*/
|
||||
],
|
||||
),
|
||||
),
|
||||
@@ -94,118 +94,109 @@ class AuthPage extends GetView<AuthLogic> {
|
||||
key: controller.formKey,
|
||||
child: Column(
|
||||
children: [
|
||||
ObxValue(
|
||||
(phoneController) =>
|
||||
RTextField(
|
||||
label: 'نام کاربری',
|
||||
maxLength: 11,
|
||||
maxLines: 1,
|
||||
controller: phoneController.value,
|
||||
keyboardType: TextInputType.text,
|
||||
initText: phoneController.value.text,
|
||||
onChanged: (value) {
|
||||
phoneController.value.text = value;
|
||||
phoneController.refresh();
|
||||
},
|
||||
prefixIcon: Padding(
|
||||
padding: const EdgeInsets.fromLTRB(0, 8, 6, 8),
|
||||
child: Assets.vec.callSvg.svg(
|
||||
width: 12,
|
||||
height: 12,
|
||||
),
|
||||
),
|
||||
suffixIcon:
|
||||
phoneController.value.text
|
||||
.trim()
|
||||
.isNotEmpty
|
||||
? clearButton(() {
|
||||
phoneController.value.clear();
|
||||
phoneController.refresh();
|
||||
RTextField(
|
||||
label: 'نام کاربری',
|
||||
maxLength: 11,
|
||||
maxLines: 1,
|
||||
controller: controller.phoneNumberController.value,
|
||||
keyboardType: TextInputType.number,
|
||||
initText: controller.phoneNumberController.value.text,
|
||||
onChanged: (value) async {
|
||||
controller.phoneNumberController.value.text = value;
|
||||
controller.phoneNumberController.refresh();
|
||||
if (value.length == 11) {
|
||||
await controller.getUserInfo(value);
|
||||
}
|
||||
},
|
||||
prefixIcon: Padding(
|
||||
padding: const EdgeInsets.fromLTRB(0, 8, 6, 8),
|
||||
child: Assets.vec.callSvg.svg(width: 12, height: 12),
|
||||
),
|
||||
suffixIcon:
|
||||
controller.phoneNumberController.value.text.trim().isNotEmpty
|
||||
? clearButton(() {
|
||||
controller.phoneNumberController.value.clear();
|
||||
controller.phoneNumberController.refresh();
|
||||
})
|
||||
: null,
|
||||
validator: (value) {
|
||||
if (value == null || value.isEmpty) {
|
||||
return '⚠️ شماره موبایل را وارد کنید';
|
||||
}
|
||||
/*else if (value.length < 11) {
|
||||
return '⚠️ شماره موبایل باید 11 رقم باشد';
|
||||
}*/
|
||||
return null;
|
||||
},
|
||||
style: AppFonts.yekan13,
|
||||
errorStyle: AppFonts.yekan13.copyWith(
|
||||
color: AppColor.redNormal,
|
||||
),
|
||||
labelStyle: AppFonts.yekan13,
|
||||
boxConstraints: const BoxConstraints(
|
||||
maxHeight: 40,
|
||||
minHeight: 40,
|
||||
maxWidth: 40,
|
||||
minWidth: 40,
|
||||
),
|
||||
),
|
||||
controller.phoneNumberController,
|
||||
: null,
|
||||
validator: (value) {
|
||||
if (value == null || value.isEmpty) {
|
||||
return '⚠️ شماره موبایل را وارد کنید';
|
||||
} else if (value.length < 11) {
|
||||
return '⚠️ شماره موبایل باید 11 رقم باشد';
|
||||
}
|
||||
return null;
|
||||
},
|
||||
style: AppFonts.yekan13,
|
||||
errorStyle: AppFonts.yekan13.copyWith(color: AppColor.redNormal),
|
||||
labelStyle: AppFonts.yekan13,
|
||||
boxConstraints: const BoxConstraints(
|
||||
maxHeight: 40,
|
||||
minHeight: 40,
|
||||
maxWidth: 40,
|
||||
minWidth: 40,
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 26),
|
||||
ObxValue(
|
||||
(passwordController) =>
|
||||
RTextField(
|
||||
label: 'رمز عبور',
|
||||
filled: false,
|
||||
controller: passwordController.value,
|
||||
variant: RTextFieldVariant.password,
|
||||
initText: passwordController.value.text,
|
||||
onChanged: (value) {
|
||||
passwordController.refresh();
|
||||
},
|
||||
validator: (value) {
|
||||
if (value == null || value.isEmpty) {
|
||||
return '⚠️ رمز عبور را وارد کنید';
|
||||
}
|
||||
return null;
|
||||
},
|
||||
style: AppFonts.yekan13,
|
||||
errorStyle: AppFonts.yekan13.copyWith(
|
||||
color: AppColor.redNormal,
|
||||
),
|
||||
labelStyle: AppFonts.yekan13,
|
||||
prefixIcon: Padding(
|
||||
padding: const EdgeInsets.fromLTRB(0, 8, 8, 8),
|
||||
child: Assets.vec.keySvg.svg(
|
||||
width: 12,
|
||||
height: 12,
|
||||
),
|
||||
),
|
||||
boxConstraints: const BoxConstraints(
|
||||
maxHeight: 34,
|
||||
minHeight: 34,
|
||||
maxWidth: 34,
|
||||
minWidth: 34,
|
||||
),
|
||||
),
|
||||
(passwordController) => RTextField(
|
||||
label: 'رمز عبور',
|
||||
filled: false,
|
||||
obscure: true,
|
||||
controller: passwordController.value,
|
||||
variant: RTextFieldVariant.password,
|
||||
initText: passwordController.value.text,
|
||||
onChanged: (value) {
|
||||
passwordController.refresh();
|
||||
},
|
||||
validator: (value) {
|
||||
if (value == null || value.isEmpty) {
|
||||
return '⚠️ رمز عبور را وارد کنید';
|
||||
}
|
||||
return null;
|
||||
},
|
||||
style: AppFonts.yekan13,
|
||||
errorStyle: AppFonts.yekan13.copyWith(
|
||||
color: AppColor.redNormal,
|
||||
),
|
||||
labelStyle: AppFonts.yekan13,
|
||||
prefixIcon: Padding(
|
||||
padding: const EdgeInsets.fromLTRB(0, 8, 8, 8),
|
||||
child: Assets.vec.keySvg.svg(width: 12, height: 12),
|
||||
),
|
||||
boxConstraints: const BoxConstraints(
|
||||
maxHeight: 34,
|
||||
minHeight: 34,
|
||||
maxWidth: 34,
|
||||
minWidth: 34,
|
||||
),
|
||||
),
|
||||
controller.passwordController,
|
||||
),
|
||||
SizedBox(height: 26),
|
||||
CaptchaWidget(),
|
||||
SizedBox(height: 23),
|
||||
ObxValue((data) {
|
||||
|
||||
Obx(() {
|
||||
return RElevated(
|
||||
text: 'ورود',
|
||||
isLoading: data.value,
|
||||
onPressed: () async {
|
||||
await controller.submitLoginForm();
|
||||
},
|
||||
isLoading: controller.isLoading.value,
|
||||
onPressed: controller.isDisabled.value
|
||||
? null
|
||||
: () async {
|
||||
await controller.submitLoginForm2();
|
||||
},
|
||||
width: Get.width,
|
||||
height: 48,
|
||||
);
|
||||
}, controller.isLoading),
|
||||
}),
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
/*
|
||||
/*
|
||||
Widget sendCodeForm() {
|
||||
return ObxValue((data) {
|
||||
return Form(
|
||||
|
||||
@@ -2,6 +2,7 @@ import 'dart:math';
|
||||
|
||||
import 'package:flutter/cupertino.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:rasadyar_auth/presentation/pages/auth/logic.dart';
|
||||
import 'package:rasadyar_auth/presentation/widget/clear_button.dart';
|
||||
import 'package:rasadyar_core/core.dart';
|
||||
|
||||
@@ -64,9 +65,19 @@ class CaptchaWidget extends GetView<CaptchaWidgetLogic> {
|
||||
validator: (value) {
|
||||
if (value == null || value.isEmpty) {
|
||||
return 'کد امنیتی را وارد کنید';
|
||||
} else if (controller.captchaKey.value != null && controller.captchaKey.value != value) {
|
||||
return 'کد امنیتی اشتباه است';
|
||||
}
|
||||
return null;
|
||||
},
|
||||
onChanged: (pass) {
|
||||
if(pass.length== 6) {
|
||||
if(controller.formKey.currentState?.validate()??false) {
|
||||
Get.find<AuthLogic>().isDisabled.value = false;
|
||||
|
||||
}
|
||||
}
|
||||
},
|
||||
style: AppFonts.yekan13,
|
||||
);
|
||||
}, controller.textController),
|
||||
@@ -82,10 +93,10 @@ class _CaptchaLinePainter extends CustomPainter {
|
||||
void paint(Canvas canvas, Size size) {
|
||||
final random = Random();
|
||||
final paint1 = Paint()
|
||||
..color = Color.fromRGBO(random.nextInt(255), random.nextInt(255), random.nextInt(255), 1)
|
||||
..color = Colors.deepOrange
|
||||
..strokeWidth = 2;
|
||||
final paint2 = Paint()
|
||||
..color = Color.fromRGBO(random.nextInt(255), random.nextInt(255), random.nextInt(255), 1)
|
||||
..color =Colors.blue
|
||||
..strokeWidth = 2;
|
||||
|
||||
// First line: top-left to bottom-right
|
||||
|
||||
Reference in New Issue
Block a user