Christian
Christian

Reputation: 888

Internal Server Error[500] when using personal-access-token to access laravel api

(This is not a duplicate of Laravel 5.3 Personal access token 500. There is a problem with creating the token, but I have no issue with creating the token, my token is simply not accepted in the authentication)

I have a setup using laravel/passport on apache. I've followed the installation routine in https://laravel.com/docs/5.7/passport#installation and also There I created a mixed application. A part is using standard laravel, and there is a complex component using vue+vuex. My authentication works as follows: I use laravel standard web authentication and on user login I create a fresh personal-access-token.

When the vue component is created it retrieves the token successfully using:

window.axios.get(`/oauth/personal-access-tokens`)

When I query the api I hand over the token:

axios.defaults.headers.common['Authorization'] = 'Bearer '+context.rootState.user.token;
axios.defaults.headers.common['Accept'] = 'application/json';
window.axios.get(`/api/userfavs`)...

The complete header

Accept  application/json
Accept-Encoding gzip, deflate
Accept-Language en-US,en;q=0.5
Authorization   Bearer 43c7fbc2b96d67d36749ad6…cf6692eb7d21dbc15d4196c1212bd
Cache-Control   max-age=0
Connection  keep-alive
Cookie  XSRF-TOKEN=eyJpdiI6IkNrNSswZER…jOTA5MTA3ZjMxOTk4NWEifQ%3D%3D
Host    localhost
Referer http://localhost/vegbed/1/edit
User-Agent  Mozilla/5.0 (X11; Ubuntu; Linu…) Gecko/20100101 Firefox/64.0
X-CSRF-TOKEN    tbVdEArmqXzdEGGwwd2eYFnL0gwKs4dW01aSrKz1
X-Requested-With    XMLHttpRequest
X-XSRF-TOKEN    eyJpdiI6IkNr

The route:

Route::get('userfavs/','Api\UserFavController@index')->middleware('auth:api');

The response is:

SQLSTATE[42S22]: Column not found: 1054 Unknown column 'api_token' in 'where clause' (SQL: select * from users where api_token = 43c7fbc2b96d67d36749ad6df2278e4e69d755909dfc2de3400cf6692eb7d21dbc15d4196c1212bd limit 1)

Thats what my auth.php looks like:

'guards' => [
    'web' => [
        'driver' => 'session',
        'provider' => 'users',
    ],

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

Upvotes: 1

Views: 2556

Answers (1)

Hidayat Riva'i
Hidayat Riva'i

Reputation: 106

laravel use api_token if you write token in guard in your config.php,, you need api_token field in your table if you use token in your api guards.

'guards' => [
    'web' => [
        'driver'   => 'session',
        'provider' => 'users',
    ],

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

so if you want to use passport then change the driver in API to passport like this :

'guards' => [
    'web' => [
        'driver'   => 'session',
        'provider' => 'users',
    ],

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

when it still get same error after you change it to passport, clear your app with artisan first. clear the cache, config, and view..

Upvotes: 1

Related Questions