gpt4 book ai didi

Laravel Passport createToken 错误

转载 作者:行者123 更新时间:2023-12-04 01:55:44 27 4
gpt4 key购买 nike

嗨,我正在尝试从用户对象生成 token ,如下所示:

<?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 路由生成 token 没有问题。

这是堆栈跟踪的第一部分:
    [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()

最佳答案

可能是护照安装的问题。
试试这个: php 工匠护照:安装 --force

关于Laravel Passport createToken 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50809629/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com