MAB MAB
MAB MAB

Reputation: 1

Flutter Reset Password Function - Validation and Update Issue with SharedPreferences

Problem Description:

I am developing a Flutter app and am facing an issue with the reset password feature. The functionality involves validating the current password stored in SharedPreferences and updating it with a new one. However, the reset process doesn’t work as expected. Here's what happens:

When I enter the correct current password, I still get the "Incorrect current password" error message. After resetting, the new password doesn't replace the old password properly for login validation, even though the logs indicate it was updated. Debug logs show that the password stored in SharedPreferences and the password used for validation don’t always align.

** Environment:**

Flutter version: Flutter 3.24.5 Dart version: 3.5.4 (stable) SharedPreferences version: 2.3.3

Code Snippets:

1) PasswordManager Class:

import 'package:shared_preferences/shared_preferences.dart';

class PasswordManager {
  static const String _passwordKey = 'password';

  static Future<String> getPassword() async {
    final prefs = await SharedPreferences.getInstance();
    return prefs.getString(_passwordKey) ?? 'admin'; // Default password
  }

  static Future<bool> updatePassword(String newPassword) async {
    final prefs = await SharedPreferences.getInstance();
    return prefs.setString(_passwordKey, newPassword);
  }

  static Future<bool> validatePassword(String enteredPassword) async {
    final storedPassword = await getPassword();
    return storedPassword == enteredPassword.trim();
  }
}

2) Reset Password Dialog in Dashboard Screen:

void _showResetPasswordDialog(BuildContext context) {
  final TextEditingController currentPasswordController = TextEditingController();
  final TextEditingController newPasswordController = TextEditingController();

  showDialog(
    context: context,
    builder: (BuildContext context) {
      return AlertDialog(
        title: Text('Reset Password'),
        content: Column(
          mainAxisSize: MainAxisSize.min,
          children: [
            TextField(
              controller: currentPasswordController,
              decoration: InputDecoration(labelText: 'Current Password'),
              obscureText: true,
            ),
            TextField(
              controller: newPasswordController,
              decoration: InputDecoration(labelText: 'New Password'),
              obscureText: true,
            ),
          ],
        ),
        actions: [
          TextButton(
            onPressed: () => Navigator.pop(context),
            child: Text('Cancel'),
          ),
          ElevatedButton(
            onPressed: () async {
              final currentPassword = currentPasswordController.text.trim();
              final newPassword = newPasswordController.text.trim();

              if (await PasswordManager.validatePassword(currentPassword)) {
                await PasswordManager.updatePassword(newPassword);
                Navigator.pop(context);
              } else {
                ScaffoldMessenger.of(context).showSnackBar(
                  SnackBar(content: Text('Incorrect current password.')),
                );
              }
            },
            child: Text('Save'),
          ),
        ],
      );
    },
  );
}

Debug Logs:

DEBUG: PasswordManager.getPassword() -> Stored Password: "admin"
DEBUG: Entered Current Password: "admin"
DEBUG: PasswordManager.validatePassword() -> Entered: "admin", Stored: "admin"
DEBUG: PasswordManager.updatePassword() -> Password updated to: "1234"
DEBUG: PasswordManager.getPassword() -> Stored Password: "admin"

Expected Behavior:

The reset password feature should: Validate the current password against the stored password. Update the stored password when the validation succeeds. Allow the user to log in with the new password after reset.

Actual Behavior:

Validation often fails even with the correct current password. New password updates are inconsistent during subsequent validation.

Verified that SharedPreferences is working by logging stored values. Ensured that the validation checks trim and match correctly. Reloaded the password after every update to avoid stale data.

Upvotes: 0

Views: 16

Answers (0)

Related Questions