我正在尝试使用Tymon JWT和
创建一个jwt令牌LoginController
$user = User::find(1);
$token = auth('api')->login($user);
工作正常,我正在获得令牌
MeController
public function get(Request $request){
$token_user = [];
$authorization = '';
try {
$authorization = $request->header('Authorization'); //Works fine. I am getting Bearer `My Token String`
$token_user = auth('api')->user();
} catch (\Tymon\JWTAuth\Exceptions\UserNotDefinedException $e) {
// do something
$token_user = $e;
}
return response()->json(compact('token_user', 'authorization'));
}
总是返回null。
routes / api.php
Route::get('me', 'User\MeController@get');
在我的config / auth php中,我更改了模型,因为我不使用laravel附带的默认UserModel
config / auth.php
'providers' => [
'users' => [
'driver' => 'eloquent',
'model' => App\Models\User::class, //Though changing it does not make any difference
],
// 'users' => [
// 'driver' => 'database',
// 'table' => 'users',
// ],
],
composer.json
"require": {
"php": "^7.1.3",
"barryvdh/laravel-cors": "^0.11.3",
"fideloper/proxy": "^4.0",
"laravel/framework": "5.8.*",
"laravel/tinker": "^1.0",
"tymon/jwt-auth": "^1.0.0-rc.1"
},
如何从令牌中获取用户
更新1 我遵循了https://github.com/angular/angular-cli/issues/13865,它确实有效
所做的更改
app \ Http \ Kernel.php
protected $routeMiddleware = [
'jwt.auth' => \Tymon\JWTAuth\Http\Middleware\Authenticate::class, //added this line
'jwt.refresh' => \Tymon\JWTAuth\Http\Middleware\RefreshToken::class, //added this line
];
config \ app.php
'aliases' => [
'App' => Illuminate\Support\Facades\App::class,
.
.
.
'JWTAuth' => Tymon\JWTAuth\Facades\JWTAuth::class,//added this line
'JWTFactory' => Tymon\JWTAuth\Facades\JWTFactory::class,//added this line
],