How to authenticate a user password from a given request in Laravel? How is the password checked against the password hash stored in the database?
相关问题
- Views base64 encoded blob in HTML with PHP
- Laravel Option Select - Default Issue
- PHP Recursively File Folder Scan Sorted by Modific
- Can php detect if javascript is on or not?
- Using similar_text and strpos together
Laravel Login Authentication:
From Laravel 5 onward, you can use the
bcrypt()
function to hash a plaintext. So, you can save that hashed password in DB and then, compare the hashed password again to match.And then, compare these two. You're good to go.
Or, if you want to go with the Laravel way:
As per Laravel documentation, and I quote: "The attempt method accepts an array of key / value pairs as its first argument. The values in the array will be used to find the user in your database table. So, in the example above, the user will be retrieved by the value of the email column. If the user is found, the hashed password stored in the database will be compared with the password value passed to the method via the array. You should not hash the password specified as the password value, since the framework will automatically hash the value before comparing it to the hashed password in the database. If the two hashed passwords match an authenticated session will be started for the user.
The attempt method will return true if authentication was successful. Otherwise, false will be returned."
First, you'll need to find the User who is logging in based on email address or username or however you identify them, for example:
Then, you'll need to CHECK the hashed password, like so:
This will return true or false based on whether or not the password matches.
You can create the below method to find the user authentication as explained on the laravel website for authentication:
Please check the link below for more details regarding authentication on laravel website: https://laravel.com/docs/5.6/authentication#authenticating-users
Step 1: first get user data from DB
Step 2: Get user password as
Step 3: Validate it as
woo hoo!!!!! you have done :)