Laravel Passport createToken错误

时间:2018-06-12 05:11:14

标签: laravel laravel-passport

您好我正在尝试从用户对象生成令牌,如下所示:

<?php
namespace App\Http\Controllers\API\Auth;

use App\Models\User;
use App\Http\Controllers\Controller;
use Auth;
use Response;

class LoginController extends Controller
{
    public function login() { 
        $content = [];
        if(Auth::attempt(['email' => request('username'), 'password' => request('password')])){
            $user = Auth::user();
            $role = $user->roles()->first()->name;
            $content['access_token'] = $user->createToken('Token Name')->accessToken;
            $status = 200;
        }
        else{
            $content['error'] = "Unauthorised";
            $content['message'] = "Your email or password are incorrect";
            $status = 401;
        }
        return response()->json($content, $status); 
    }
}

我收到500服务器错误:

Trying to get property of non-object

我在User模型上有HasApiTokens,并且使用内置的Passport路由生成令牌没有问题。

这是Stack Trace的第一部分:

    [2018-06-12 18:20:59] local.ERROR: Trying to get property of non-object {"userId":1,"email":"***","exception":"[object] (ErrorException(code: 0): Trying to get property of non-object at ...vendor/laravel/passport/src/ClientRepository.php:81)
[stacktrace]
#0 ...vendor/laravel/passport/src/ClientRepository.php(81): Illuminate\\Foundation\\Bootstrap\\HandleExceptions->handleError(8, 'Trying to get p...', '/Users/peterste...', 81, Array)
#1 ...vendor/laravel/passport/src/PersonalAccessTokenFactory.php(71): Laravel\\Passport\\ClientRepository->personalAccessClient()
#2 ...vendor/laravel/passport/src/HasApiTokens.php(67): Laravel\\Passport\\PersonalAccessTokenFactory->make(1, 'Token Name', Array)
#3 ...app/Http/Controllers/API/Auth/LoginController.php(16): App\\Models\\User->createToken('Token Name')
#4 [internal function]: App\\Http\\Controllers\\API\\Auth\\LoginController->login()
#5 ...vendor/laravel/framework/src/Illuminate/Routing/Controller.php(54): call_user_func_array(Array, Array)
#6 ...vendor/laravel/framework/src/Illuminate/Routing/ControllerDispatcher.php(45): Illuminate\\Routing\\Controller->callAction('login', Array)
#7 ...vendor/laravel/framework/src/Illuminate/Routing/Route.php(212): Illuminate\\Routing\\ControllerDispatcher->dispatch(Object(Illuminate\\Routing\\Route), Object(App\\Http\\Controllers\\API\\Auth\\LoginController), 'login')
#8 ...vendor/laravel/framework/src/Illuminate/Routing/Route.php(169): Illuminate\\Routing\\Route->runController()
#9 ...vendor/laravel/framework/src/Illuminate/Routing/Router.php(659): Illuminate\\Routing\\Route->run()

3 个答案:

答案 0 :(得分:0)

尝试使用以下命令创建客户端ID和密码: php artisan护照:客户

了解更多详情: https://laravel.com/docs/5.6/passport#client-credentials-grant-tokens

答案 1 :(得分:0)

护照安装可能有问题。

尝试一下: php artisan护照:安装--force

答案 2 :(得分:0)

这可能已经晚了,但是为了那些遇到类似问题的人,

运行后,将“ client-id”替换为Client ID的值

php artisan passport:client --personal.

如此

Passport::personalAccessClientId('client-id');

应替换为Passport::personalAccessClientId('4'); //其中4是生成的客户端ID //此代码位于AuthServiceProvider的boot()函数中