Laravel bcrypt 密码在 lumen 中不匹配
Laravel bcrypt password not matching in lumen
我在数据库中使用 bcrypt() 密码创建了许多 laravel 用户,现在我正在构建 JWT 基础身份验证的 lumen 中编写 API
我的代码看起来像
$credentials = [
'email' => $this->request->input('email'),
'password' => $this->request->input('password'),
];
if (Auth::check($credentials)) {
dd('success');
} else {
dd('failed');
}
失败,转到条件的其他部分
我也试过 Hash::check,它也给了我 false
if (Hash::check($this->request->input('password'), $user->password)) {
return response()->json([
'token' => $this->jwt($user)
], 200);
}
注意:我在请求中传递的密码是正确的。
我这里做错了什么,缺少什么
你试过用attempt()
代替check
public function login(Request $request)
{
$credentials = $request->only('email', 'password');
if ($token = $this->guard()->attempt($credentials)) {
return $this->respondWithToken($token);
}
return response()->json(['error' => 'Unauthorized'], 401);
}
您可能需要使用
编辑您的 .env 文件
AUTH_DRIVER=eloquent
AUTH_MODEL=\App\Models\User
AUTH_TABLE=users
我在数据库中使用 bcrypt() 密码创建了许多 laravel 用户,现在我正在构建 JWT 基础身份验证的 lumen 中编写 API
我的代码看起来像
$credentials = [
'email' => $this->request->input('email'),
'password' => $this->request->input('password'),
];
if (Auth::check($credentials)) {
dd('success');
} else {
dd('failed');
}
失败,转到条件的其他部分
我也试过 Hash::check,它也给了我 false
if (Hash::check($this->request->input('password'), $user->password)) {
return response()->json([
'token' => $this->jwt($user)
], 200);
}
注意:我在请求中传递的密码是正确的。
我这里做错了什么,缺少什么
你试过用attempt()
代替check
public function login(Request $request)
{
$credentials = $request->only('email', 'password');
if ($token = $this->guard()->attempt($credentials)) {
return $this->respondWithToken($token);
}
return response()->json(['error' => 'Unauthorized'], 401);
}
您可能需要使用
编辑您的 .env 文件AUTH_DRIVER=eloquent
AUTH_MODEL=\App\Models\User
AUTH_TABLE=users