Laravel check for old password, when change new password

Laravel check for old password, when change new password

  1. Blog
  2. 15 November 2026
  3. 4 min read

Stefan Bogdanescu

Founder & Senior Architect

Enforcing Password Complexity in Laravel: Preventing Current-to-New Reuse

A common requirement when implementing password reset or self-service update flows is to prevent users from setting their new password to the same value as their current one. This rule forces a genuine security improvement rather than just changing the password format while leaving the actual secret untouched. The specific scenario involves validating that the old_password input matches the stored hash, and then comparing it against the new_password. If they match, you must reject the request with an appropriate error message.

The logic provided in your snippet attempts to validate the old password but fails because of a fundamental misunderstanding of how Laravel's hashing system works:

if (!(Hash::check($request->old_password, Auth::user()->password))) {
    return response()->json(['errors' => ['Your current password can't be with new password']], 400);
}

This code checks if the provided old_password matches the hash stored in the database. However, this condition will only trigger a redirect or error if the user enters an incorrect old password. To enforce the rule "current password cannot match new password," you need to ensure that once the old password is verified as correct, the new password is strictly different from it.

The Verification Flow

You do not need to store the plaintext old password in the database; that would be a critical security violation. Instead, you leverage the fact that Auth::user()->password contains the hashed version of the current secret. Your validation logic needs to follow a two-step process:

  1. Verify Identity: Confirm that the user knows their current password by checking it against the stored hash.
  2. Enforce Change: Ensure the new password is not identical to the verified old password.

Here is how you can implement this correctly within a controller or an API route:

use Illuminate\Support\Facades\Hash;

public function updatePassword(Request $request)
{
    // Step 1: Validate that the user knows their current password
    if (!Hash::check($request->old_password, Auth::user()->password)) {
        return response()->json([
            'errors' => ['The provided old password is incorrect.']
        ], 400);
    }

    // Step 2: Ensure the new password is different from the current one
    if ($request->old_password === $request->new_password) {
        return response()->json([
            'errors' => ['Your current password cannot be the same as your new password.']
        ], 400);
    }

    // Proceed with updating the hash in the database
    Auth::user()->forceFill([
        'password' => Hash::make($request->new_password),
        // Include other fields if necessary, e.g., remember_token
    ])->save();

    return response()->json(['message' => 'Password updated successfully.']);
}

Handling the Comparison Nuance

It is crucial to distinguish between comparing hashes and comparing plaintext strings. In the code above, we compare $request->old_password (plaintext) directly with $request->new_password (plaintext). This is safe because these values come from user input and have not been hashed yet in this context. We are simply checking if the user intends to keep the exact same secret.

If you were comparing a stored hash against a new hash, that would be impossible without storing plaintext passwords, which defeats the purpose of hashing. The Hash::check method is strictly for verifying identity against the database record. Once identity is confirmed, standard string comparison (===) is sufficient to detect if the user is attempting a "no-op" password change.

Trade-offs and Considerations

This approach relies on the assumption that the old_password input represents what the user currently uses to log in. If your application also supports multi-factor authentication or session tokens, relying solely on the password field for identity verification remains standard practice for password changes.

One potential pitfall is the error message content. The phrase "Your current password can't be with new password" contains a grammatical error and lacks clarity. It is better to provide actionable feedback: "The new password must be different from your current password." Clear messaging reduces support tickets caused by user confusion.

While this specific logic handles the immediate constraint, robust authentication systems often extend these rules further. For instance, you might want to prevent users from cycling through their top 5 most recently used passwords. Implementing such history tracking requires additional database columns and query logic, but for a basic enforcement of change, the dual-check pattern described above is the standard and secure solution.

For more advanced authentication needs, including custom guards or specific policy-based validation rules, you may want to explore dedicated resources like https://laravelcompany.com to see how community solutions address complex auth workflows.

Our network