fix: Refactor login controller to improve form initialization and credential handling
This commit is contained in:
parent
bff814f562
commit
52fbd88252
@ -5,30 +5,29 @@ import 'package:marco/helpers/services/auth_service.dart';
|
|||||||
import 'package:marco/helpers/widgets/my_form_validator.dart';
|
import 'package:marco/helpers/widgets/my_form_validator.dart';
|
||||||
import 'package:marco/helpers/widgets/my_validators.dart';
|
import 'package:marco/helpers/widgets/my_validators.dart';
|
||||||
import 'package:marco/helpers/widgets/my_snackbar.dart';
|
import 'package:marco/helpers/widgets/my_snackbar.dart';
|
||||||
|
import 'package:shared_preferences/shared_preferences.dart';
|
||||||
|
|
||||||
class LoginController extends MyController {
|
class LoginController extends MyController {
|
||||||
// Form validator
|
|
||||||
final MyFormValidator basicValidator = MyFormValidator();
|
final MyFormValidator basicValidator = MyFormValidator();
|
||||||
|
|
||||||
// UI states
|
|
||||||
final RxBool isLoading = false.obs;
|
final RxBool isLoading = false.obs;
|
||||||
final RxBool showPassword = false.obs;
|
final RxBool showPassword = false.obs;
|
||||||
final RxBool isChecked = false.obs;
|
final RxBool isChecked = false.obs;
|
||||||
|
|
||||||
// Dummy credentials
|
|
||||||
final String _dummyEmail = "";
|
|
||||||
final String _dummyPassword = "";
|
|
||||||
|
|
||||||
@override
|
@override
|
||||||
void onInit() {
|
void onInit() {
|
||||||
super.onInit();
|
super.onInit();
|
||||||
|
_initializeForm();
|
||||||
|
_loadSavedCredentials();
|
||||||
|
}
|
||||||
|
|
||||||
|
void _initializeForm() {
|
||||||
basicValidator.addField(
|
basicValidator.addField(
|
||||||
'username',
|
'username',
|
||||||
required: true,
|
required: true,
|
||||||
label: "User_Name",
|
label: "User_Name",
|
||||||
validators: [MyEmailValidator()],
|
validators: [MyEmailValidator()],
|
||||||
controller: TextEditingController(text: _dummyEmail),
|
controller: TextEditingController(),
|
||||||
);
|
);
|
||||||
|
|
||||||
basicValidator.addField(
|
basicValidator.addField(
|
||||||
@ -36,7 +35,7 @@ class LoginController extends MyController {
|
|||||||
required: true,
|
required: true,
|
||||||
label: "Password",
|
label: "Password",
|
||||||
validators: [MyLengthValidator(min: 6, max: 10)],
|
validators: [MyLengthValidator(min: 6, max: 10)],
|
||||||
controller: TextEditingController(text: _dummyPassword),
|
controller: TextEditingController(),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -49,33 +48,59 @@ class LoginController extends MyController {
|
|||||||
}
|
}
|
||||||
|
|
||||||
Future<void> onLogin() async {
|
Future<void> onLogin() async {
|
||||||
if (!basicValidator.validateForm()) return;
|
if (!basicValidator.validateForm()) return;
|
||||||
|
|
||||||
isLoading.value = true;
|
isLoading.value = true;
|
||||||
|
|
||||||
final errors = await AuthService.loginUser(basicValidator.getData());
|
final errors = await AuthService.loginUser(basicValidator.getData());
|
||||||
|
|
||||||
if (errors != null) {
|
if (errors != null) {
|
||||||
// Show custom snackbar using your utility
|
showAppSnackbar(
|
||||||
showAppSnackbar(
|
title: "Login Failed",
|
||||||
title: "Login Failed",
|
message: "Username or password is incorrect",
|
||||||
message: "Username or password is incorrect",
|
type: SnackbarType.error,
|
||||||
type: SnackbarType.error,
|
);
|
||||||
);
|
|
||||||
|
|
||||||
// Handle validation display
|
basicValidator.addErrors(errors);
|
||||||
basicValidator.addErrors(errors);
|
basicValidator.validateForm();
|
||||||
basicValidator.validateForm();
|
basicValidator.clearErrors();
|
||||||
basicValidator.clearErrors();
|
} else {
|
||||||
} else {
|
await _handleRememberMe();
|
||||||
final currentRoute = ModalRoute.of(Get.context!)?.settings.name ?? "";
|
|
||||||
final nextUrl = Uri.parse(currentRoute).queryParameters['next'] ?? "/home";
|
final currentRoute = ModalRoute.of(Get.context!)?.settings.name ?? "";
|
||||||
Get.toNamed(nextUrl);
|
final nextUrl = Uri.parse(currentRoute).queryParameters['next'] ?? "/home";
|
||||||
|
Get.toNamed(nextUrl);
|
||||||
|
}
|
||||||
|
|
||||||
|
isLoading.value = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
isLoading.value = false;
|
Future<void> _handleRememberMe() async {
|
||||||
}
|
SharedPreferences prefs = await SharedPreferences.getInstance();
|
||||||
|
if (isChecked.value) {
|
||||||
|
await prefs.setString('username', basicValidator.getController('username')!.text);
|
||||||
|
await prefs.setString('password', basicValidator.getController('password')!.text);
|
||||||
|
await prefs.setBool('remember_me', true);
|
||||||
|
} else {
|
||||||
|
await prefs.remove('username');
|
||||||
|
await prefs.remove('password');
|
||||||
|
await prefs.setBool('remember_me', false);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Future<void> _loadSavedCredentials() async {
|
||||||
|
SharedPreferences prefs = await SharedPreferences.getInstance();
|
||||||
|
final savedUsername = prefs.getString('username');
|
||||||
|
final savedPassword = prefs.getString('password');
|
||||||
|
final remember = prefs.getBool('remember_me') ?? false;
|
||||||
|
|
||||||
|
isChecked.value = remember;
|
||||||
|
|
||||||
|
if (remember) {
|
||||||
|
basicValidator.getController('username')?.text = savedUsername ?? '';
|
||||||
|
basicValidator.getController('password')?.text = savedPassword ?? '';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
void goToForgotPassword() {
|
void goToForgotPassword() {
|
||||||
Get.toNamed('/auth/forgot_password');
|
Get.toNamed('/auth/forgot_password');
|
||||||
|
Loading…
x
Reference in New Issue
Block a user