How to verify that input password matches database hashed password in Laravel 8
P粉129731808
2023-08-28 14:25:49
<p>How to verify user password from given request in Laravel? How is the password compared to the password hash stored in the database?
**
This is my controller
**</p>
<pre class="brush:php;toolbar:false;"><?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
class MainController extends Controller
{
function login1(Request $request){
$username = $request->input('username');
$password = $request->input('password');
$data = DB::table('users')->where(['username'=>$username, 'password'=>$password])->first();
if($data == null){
echo "error";
$notification = array(
'message' => 'User does not exist! ',
'alert-type' => 'error'
);
return back()->with($notification);
}
else{
$request->session()->put('user',$data);
return redirect('dashboard');
}
}}</pre></p>
like this
$encrypted = Crypt::encrypt('password_name_variable');
Basically, what you want to do is:
users
Users with a given username in the table.So, you want to first query for users with a given username. Then, after retrieving the user and verifying its existence, you can check if the provided password matches the hashed password on the retrieved model.
However, there are built-in functions in Laravel to achieve this, and depending on your needs, it may be simpler to do this:
https://laravel.com/api/8.x/Illuminate/Support/Facades/Auth.html#method_attempt