feat(otp): implement email saving and loading functionality in OTPController

This commit is contained in:
Vaibhav Surve 2025-07-09 16:40:23 +05:30
parent 5b5030ec36
commit 1e1bcc3aa4
2 changed files with 45 additions and 4 deletions

View File

@ -25,6 +25,7 @@ class OTPController extends GetxController {
void onInit() {
super.onInit();
timer.value = 0;
_loadSavedEmail();
logSafe("[OTPController] Initialized");
}
@ -53,7 +54,6 @@ class OTPController extends GetxController {
"[OTPController] OTP send failed",
level: LogLevel.warning,
error: result['error'],
);
showAppSnackbar(
title: "Error",
@ -85,6 +85,7 @@ class OTPController extends GetxController {
if (success) {
email.value = userEmail;
isOTPSent.value = true;
await _saveEmailIfRemembered(userEmail);
_startTimer();
_clearOTPFields();
}
@ -144,7 +145,7 @@ class OTPController extends GetxController {
Get.offAllNamed('/home');
} else {
final error = result['error'] ?? "Failed to verify OTP";
logSafe("[OTPController] OTP verification failed", level: LogLevel.warning, error: error, );
logSafe("[OTPController] OTP verification failed", level: LogLevel.warning, error: error);
showAppSnackbar(
title: "Error",
message: error,
@ -189,10 +190,32 @@ class OTPController extends GetxController {
for (final node in focusNodes) {
node.unfocus();
}
// Optionally remove saved email
LocalStorage.removeToken('otp_email');
}
bool _validateEmail(String email) {
final regex = RegExp(r'^[\w-\.]+@([\w-]+\.)+[\w-]{2,}$');
return regex.hasMatch(email);
}
/// Save email to local storage if "remember me" is set
Future<void> _saveEmailIfRemembered(String email) async {
final remember = LocalStorage.getBool('remember_me') ?? false;
if (remember) {
await LocalStorage.setToken('otp_email', email);
}
}
/// Load email from local storage if "remember me" is true
Future<void> _loadSavedEmail() async {
final remember = LocalStorage.getBool('remember_me') ?? false;
if (remember) {
final savedEmail = LocalStorage.getToken('otp_email') ?? '';
emailController.text = savedEmail;
email.value = savedEmail;
logSafe("[OTPController] Loaded saved email from local storage: $savedEmail");
}
}
}

View File

@ -7,7 +7,7 @@ import 'package:marco/helpers/theme/theme_customizer.dart';
import 'package:marco/helpers/theme/app_theme.dart';
import 'package:url_strategy/url_strategy.dart';
import 'package:marco/helpers/services/app_logger.dart';
import 'package:marco/helpers/services/auth_service.dart';
Future<void> initializeApp() async {
try {
logSafe("💡 Starting app initialization...");
@ -24,6 +24,20 @@ Future<void> initializeApp() async {
await LocalStorage.init();
logSafe("💡 Local storage initialized.");
// If a refresh token is found, try to refresh the JWT token
final refreshToken = await LocalStorage.getRefreshToken();
if (refreshToken != null && refreshToken.isNotEmpty) {
logSafe("🔁 Refresh token found. Attempting to refresh JWT...");
final success = await AuthService.refreshToken();
if (!success) {
logSafe("⚠️ Refresh token invalid or expired. Skipping controller injection.");
// Optionally, clear tokens and force logout here if needed
}
} else {
logSafe("❌ No refresh token found. Skipping refresh.");
}
await ThemeCustomizer.init();
logSafe("💡 Theme customizer initialized.");
@ -38,8 +52,12 @@ Future<void> initializeApp() async {
Get.put(ProjectController(), permanent: true);
logSafe("💡 ProjectController injected as permanent.");
}
// Load data into controllers if required
await Get.find<PermissionController>().loadData(token);
await Get.find<ProjectController>().fetchProjects();
} else {
logSafe("⚠️ No token found. Skipping PermissionController and ProjectController injection.");
logSafe("⚠️ No valid JWT token found. Skipping controller initialization.");
}
AppStyle.init();