Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/laravel/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
向Laravel passport令牌添加用户属性_Laravel_Laravel 5_Jwt_Laravel 5.6_Laravel Passport - Fatal编程技术网

向Laravel passport令牌添加用户属性

向Laravel passport令牌添加用户属性,laravel,laravel-5,jwt,laravel-5.6,laravel-passport,Laravel,Laravel 5,Jwt,Laravel 5.6,Laravel Passport,嗨,我想在使用Laravel passport生成的访问令牌中添加一些额外的属性。我使用的是Laravel 5.6 解码令牌示例: aud:"1" exp:1528505818 iat:1528498618 jti:"10c880c491ecb73e39a6b1d7614b4be4e13ef4adf71fbc6f858a69899461fecc001fdefb60a287c1" nbf:1528498618 scopes:[] sub:"1" user: { name: "test" r

嗨,我想在使用Laravel passport生成的访问令牌中添加一些额外的属性。我使用的是Laravel 5.6

解码令牌示例:

aud:"1"
exp:1528505818
iat:1528498618
jti:"10c880c491ecb73e39a6b1d7614b4be4e13ef4adf71fbc6f858a69899461fecc001fdefb60a287c1"
nbf:1528498618
scopes:[]
sub:"1"
user: {
  name: "test"
  role: "admin
}
我读过其他文章,但它们只将用户对象附加到响应,而不是将其编码到令牌中

或者,如何将角色添加到scopes数组

尝试了以下操作但没有成功:(500错误:尝试获取非对象的属性)


<?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('HiveMan App')->accessToken;
            $status = 200;
        }
        else{
            $content['error'] = "Unauthorised";
            $content['message'] = "Your email or password are incorrect";
            $status = 401;
        }
        return response()->json($content, $status); 
    }
}