Laravel 9-无法使用护照产生令牌
以下是我在Laravel 9项目中的登录功能。我正在尝试通过laravel-passport
创建身份验证令牌。
public function login(Request $request) {
$user = User::where('email', $request->email)->first();
if ($user) {
if (Hash::check($request->password, $user->password)) {
$token = $user->createToken('Laravel Password Grant Client')->accessToken;
$response['token'] = $token;
return response($response, 200);
} else {
$response = ["message" => "Password mismatch"];
return response($response, 422);
}
}
}
我得到的回应是,
{
"token": {
"name": "Laravel Password Grant Client",
"abilities": [
"*"
],
"tokenable_id": 1,
"tokenable_type": "App\\Models\\User",
"updated_at": "2022-07-04T05:58:36.000000Z",
"created_at": "2022-07-04T05:58:36.000000Z",
"id": 62
}
}
我在这里没有任何令牌。 请帮助我解决这个问题。
The below is my login function in laravel 9 project. I am trying to create an authentication token through laravel-passport
.
public function login(Request $request) {
$user = User::where('email', $request->email)->first();
if ($user) {
if (Hash::check($request->password, $user->password)) {
$token = $user->createToken('Laravel Password Grant Client')->accessToken;
$response['token'] = $token;
return response($response, 200);
} else {
$response = ["message" => "Password mismatch"];
return response($response, 422);
}
}
}
The response I am getting is,
{
"token": {
"name": "Laravel Password Grant Client",
"abilities": [
"*"
],
"tokenable_id": 1,
"tokenable_type": "App\\Models\\User",
"updated_at": "2022-07-04T05:58:36.000000Z",
"created_at": "2022-07-04T05:58:36.000000Z",
"id": 62
}
}
I am not getting any token here.
Please help me out to fix this.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
AccessToken是正确的,只是不要忘记使用:
而不是:
这是正确的:
$ token = $ user-> createToken('laravel密码授予客户端') - > accessToken;
accessToken is correct just don't forget use:
instead of:
This is correct:
$token = $user->createToken('Laravel Password Grant Client')->accessToken;
您是使用条件使用尝试函数的地方生成令牌的 -
它的工作正常工作
you are generate token using where condition use attempt function like this -
its working properly
创建登录令牌后,您必须先登录用户。
$ data ['email'] =请求电子邮件
$数据['密码'] =请求密码
you have to log in user first after the login token is created.
$data['email'] = request email
$data['password'] = request password
只需使用
PlaintextToken
而不是AccessToken
。它将为您提供一个字符串,您可以用作API令牌。
Just use
plainTextToken
instead ofaccessToken
.It will give you a string, that you can use as API token.