P粉3739908572023-08-29 00:51:27
like this
$encrypted = Crypt::encrypt('password_name_variable');
P粉0715596092023-08-29 00:50:53
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.
public function login(Request $request): Response
{
$user = User::where('username', $request->get('username'));
if (!$user || !Hash::check($request->get('password'), $user->password)) {
return back()->with([
'message' => '用户名和/或密码不正确。',
'alert-type' => 'error'
]);
}
$request->session()->put('user', $user);
return redirect('dashboard');
}
However, there are built-in functions in Laravel to achieve this, and depending on your needs, it may be simpler to do this:
public function login(Request $request): Response { if (!Auth::attempt(['username' => $request->get('username'), 'password' => $request->get('password')]) { return back()->with([ 'message' => '用户名和/或密码不正确。', 'alert-type' => 'error' ]); } return redirect('dashboard'); }
https://laravel.com/api/8.x/Illuminate/Support/Facades/Auth.html#method_attempt