Implementing a password change feature in Laravel 5.3 requires handling three distinct validation states simultaneously: verifying the current credentials, enforcing strength rules on the new input, and ensuring the two new fields match each other. The source material outlines a common pattern using an old_password field alongside password and verify_password. While this approach works, it introduces specific security considerations regarding where the old password is hashed and how you handle session-based authentication versus direct database manipulation.
Validating Current Credentials
The most critical step before updating any user record is confirming identity. You cannot simply accept a new password; you must first prove the user knows their current one. In Laravel 5.3, the standard Auth::check() method only confirms if a user is logged in via a session cookie. It does not automatically validate that the string submitted as old_password matches the hash stored in the database.
You need to manually verify this hash. Assuming your user model has an $auths or similar relationship, or if you are using the default User model, you can compare the hashed input against the stored hash. A naive approach might involve comparing raw strings, but that is insecure. Instead, use Laravel's Hash::check() method. This function accepts a plain text string and a hashed string, returning a boolean result without exposing whether the hashes match or not to an attacker via timing attacks (though in 5.3, this implementation detail was less robust than in later versions).
if (! Hash::check($request->old_password, $user->password)) {
return Redirect::back()->withErrors(['old_password' => 'Current password is incorrect.']);
}
If the hash check fails, you must halt execution immediately and redirect the user back with an error message. This prevents the application from proceeding to update the record even if the new password format is invalid.
Enforcing Password Strength
Once identity is confirmed, you can proceed to validate the password and verify_password fields. The source code example uses a custom regular expression for password complexity:
'regex:/^(?=\S*[a-z])(?=\S*[!@#$&*])(?=\S*[A-Z])(?=\S*[\d])\S*$/'
This regex ensures the new password contains at least one lowercase letter, one uppercase letter, one digit, and one special character. While effective, embedding complex regex logic directly into validation rules can make them hard to read and maintain. In Laravel 5.3, you might also consider using the StringValidator trait if available, or simply breaking down the requirements into separate rules for clarity.
The validation group should look like this:
$rules = [
'old_password' => 'required', // Must be provided to check identity
'password' => 'required|min:8|regex:/^(?=\S*[a-z])(?=\S*[!@#$&*])(?=\S*[A-Z])(?=\S*[\d])\S*$/',
'verify_password' => 'required|same:password',
];
$messages = [
'old_password.required' => 'Current password is required to verify your identity.',
'password.regex' => 'Password must contain uppercase, lowercase, number, and special character.',
'verify_password.same' => 'Passwords do not match.',
];
Updating the User Record
After passing validation, you create a new User instance (or update the existing one) with the hashed new password. Note that Laravel's Eloquent model will automatically handle hashing if your $fillable array includes password and your model has the default hashable trait or implements the interface correctly. However, since this is an explicit change request, you should ensure the user object exists.
If the user is already authenticated via session, you can update the current authenticated user directly:
$user = Auth::user();
// Ensure we are updating the correct record
if ($user->email !== $request->email) {
// Handle email mismatch if necessary, or reject request
}
$user->password = Hash::make($request->password);
$user->save();
If you are handling a standalone change password page where the user might not be logged in (e.g., a "forgot password" flow), you would need to find the user by email first, verify the old_password against that specific record, and then update.
Security Considerations in Laravel 5.3
One pitfall in older versions of Laravel is relying solely on session tokens for authentication during sensitive operations like password changes. If a user has not logged in recently, an attacker with access to their email could potentially exploit this if the old_password validation bypasses strict identity checks. Always ensure that Auth::check() returns true before allowing a password change via the authenticated session mechanism.
Additionally, remember that in Laravel 5.3, the Hash::make() function uses bcrypt by default. When assigning the hashed value to the model, you do not need to manually prepend $2y$ or handle salt generation; the framework handles this internally. Just ensure you are passing the raw string from the request into the Hash::make() function, never the already hashed string.
For further guidance on securing applications in Laravel 5.x, resources like https://laravelcompany.com offer insights into maintaining secure coding practices even as you migrate to newer versions.
