Laravel Passport - /oauth/token generates invalid token

天涯浪子 提交于 2019-12-11 15:17:50

问题


So I created a client using php artisan passport:client, and when I do a POST request to oauth/token I thankfully get back a token.

However, when I try to use that token by putting it in my headers and going to an auth:api protected route I get 401 unauthorized.

Using a different route to login, however, seems to work. When I use this route in my api controller:

Route::post('/login', 'Auth\LoginController@login')->name('login');

which has the following code:

public function login(Request $request) {
    $input = $request->all();

    if (Auth::attempt(['email' => $input['email'], 'password' => $input['password'] ])) {
        $user = Auth::user();

        return [
            'success' => true,
            'token' => $user->createToken('test')->accessToken
        ];
    }

    return [
        'success' => false,
        'message' => 'unable to authenticate'
    ];
}

I get back a token that works on protected routes.

I'm using Postman to test, I have headers Authorization: Bearer <token> and Accept: application/json. Not sure why it works one way but not another.

edit: Here is the protected route:

Route::middleware('auth:api')->get('/user', function (Request $request) 
{
    return $request->user();
});

which uses:

'api' => [
    'driver' => 'passport',
    'provider' => 'users',
],

来源:https://stackoverflow.com/questions/55006058/laravel-passport-oauth-token-generates-invalid-token

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!