Laravel Passport令牌不返回一系列代码 [英] Laravel Passport Token doesn't return series of code

查看:28
本文介绍了Laravel Passport令牌不返回一系列代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我当前感到困惑的是,为什么我的令牌响应JSON没有提供令牌代码。

"token": {
    "token": {
        "name": "appToken",
        "abilities": [
            "*"
        ],
        "tokenable_id": 1,
        "tokenable_type": "App\Models\User",
        "updated_at": "2021-08-29T17:35:00.000000Z",
        "created_at": "2021-08-29T17:35:00.000000Z",
        "id": 7
    }
},

以下是响应的代码。

public function login()
{
    if (Auth::attempt(['email' => request('email'), 'password' => request('password')])) {
        $user = Auth::user();
        $success['token'] = $user->createToken('appToken')->accessToken;
       //After successfull authentication, notice how I return json parameters
        return response()->json([
          'success' => true,
          'token' => $success,
          'user' => $user
      ]);
    } else {
   //if authentication is unsuccessfull, notice how I return json parameters
      return response()->json([
        'success' => false,
        'message' => 'Invalid Email or Password',
    ], 401);
    }
}

我需要用于身份验证的代码:持有者

推荐答案

您是否阅读了密码授予令牌的Laravel Passport Documentation

Once you have created a password grant client, you may request an access token by issuing a POST request to the /oauth/token route with the user's email address and password.

您必须在控制器的登录函数中执行以下操作:

$response = Http::asForm()->post('http://passport-app.com/oauth/token', [
    'grant_type' => 'password',
    'client_id' => 'client-id',
    'client_secret' => 'client-secret',
    'username' => $request->email,
    'password' => $request->password,
    'scope' => '',
]);

然后,您将能够从您的$response

获得access_tokenrefresh_token
$response->access_token;
$response->refresh_token;

这篇关于Laravel Passport令牌不返回一系列代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆