Verify if a password matches a Laravel hash securely and efficiently.
Simple example of password verification using Laravel Hash.
use Illuminate\Support\Facades\Hash;
$password = 'password-attempt';
$hashedPassword = '$2y$10$92IXUNpkjO0rOQ5byMi.Ye4oKoEa3Ro9llC/.og/at2.uheWG/igi';
// Verify if password is correct
if (Hash::check($password, $hashedPassword)) {
// Password correct
echo 'Valid password!';
} else {
// Password incorrect
echo 'Invalid password!';
}
Complete authentication example with hash verification.
use Illuminate\Support\Facades\Hash;
use App\Models\User;
// Login example
$email = $request->email;
$password = $request->password;
$user = User::where('email', $email)->first();
if ($user && Hash::check($password, $user->password)) {
// Authentication successful
auth()->login($user);
return redirect('/dashboard');
}
return back()->withErrors([
'email' => 'Invalid credentials.'
]);
Create secure Laravel hashes for your passwords and sensitive data.