tryAutoLogin method

Future<bool> tryAutoLogin()

Implementation

Future<bool> tryAutoLogin() async {
  debugPrint('Attempting auto-login...');
  final prefs = await SharedPreferences.getInstance();
  if (!prefs.containsKey('userId') || !prefs.containsKey('lastLoginTime')) {
    debugPrint('Auto-login failed: No stored credentials.');
    return false;
  }

  final extractedUserId = prefs.getString('userId');
  final extractedLastLoginTime = DateTime.parse(prefs.getString('lastLoginTime')!);

  if (extractedLastLoginTime.add(const Duration(days: 45)).isBefore(DateTime.now())) {
    _clearAuthData();
    debugPrint('Auto-login failed: Credentials expired.');
    return false;
  }

  _userId = extractedUserId;
  _lastLoginTime = extractedLastLoginTime;
  notifyListeners();
  debugPrint('Auto-login successful: userId=$_userId');
  return true;
}