mjpsr11
mjpsr11

Reputation: 615

Laravel and Passport getting SQLSTATE[42S22]: Column not found: 1054 Unknown column 'api_token'

I've seen many posts about this error, but none of the solutions are working for me.

I'm running Laravel with Passport which is working fine on my development server. As expected, when attempting to check if a user is authenticated on my development server, it returns the catch (inside an axios call):

Development Server

"message":"Unauthenticated."

However, when running the same code on my production server, it returns the catch:

Production Server:

"message": "SQLSTATE[42S22]: Column not found: 1054 Unknown column 'api_token'
in 'where clause' (SQL: select * from `users` where `api_token` = ...

I have have run Passport migration and ['guards']['api']['driver'] set to passport in config/auth.php, and updated the configuration cache that apparently solved the problem for others.

It looks like authentication needs to use the oauth tables from passport migration, but the query appears to be looking at the user table.

EDIT:

I was able to determine that my development server uses the RequestGuard class to find user and my production server uses the TokenGuard class to find user.

Upvotes: 8

Views: 19320

Answers (8)

Sabaoon Bedar
Sabaoon Bedar

Reputation: 3689

I know the question got answered, but this answer is explained to those who really want to know the real cause of the error in depth.

Remember, it is an error when your route in the API is a protected route and you need to specify a token as a bearer while sending a request after authentication to the protected routes. The below route is protected, if I want to request it I will need an access token that is generated with authentication through the passport.

Route::middleware('auth:api')->group(function () {
    Route::get('/loginCheck', 'Auth\ApiAuthController@LoginCheck');

});

What you need to do is when you change the "token" to "passport" are as follows:

Location of below file: config\auth.php

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

    'api' => [
        'driver' => 'passport',//instead of token 
        'provider' => 'users',
        'hash' => false,
    ],
],

then you will need to run the following commands.

php artisan cache:clear
php artisan route:clear
php artisan config:cache

now start the laravel server again

 php artisan serve

And you are done!

Upvotes: 2

ulas korpe
ulas korpe

Reputation: 39

there must be a column named api_token in your users table ,
and this column must be filled with unique values for each record , you can add it to your migration like

$table->string(‘api_token’, length)->after( .. ) or create manually

api apps authanticates the users via using the api_token value , instead "id"

Upvotes: 1

Md Saidur Rahman
Md Saidur Rahman

Reputation: 11

I have been solved it by running the following commands in the composer window

  1. php artisan key:generate
  2. php artisan config:cache
  3. php artisan cache:clear
  4. php artisan config:clear

Upvotes: 1

Mujahid Khan
Mujahid Khan

Reputation: 1834

I was facing same problem and i have solve this by

php artisan config:clear
php artisan config:cache
php artisan cache:clear

may be it helpful

Upvotes: 9

Leotrin Elmazi
Leotrin Elmazi

Reputation: 23

Try running:

php artisan config:cache
php artisan cache:clear
php artisan config:clear

Upvotes: 1

user13376003
user13376003

Reputation:

it also may happen when you use wrong auth middleware :

public function __construct()
{
$this->middleware('auth:api');
}

for example for using sanctum :

public function __construct()
{
$this->middleware('auth:sanctum');
}

Upvotes: 4

Ziaur Rahman
Ziaur Rahman

Reputation: 1188

I have faced the same problem. And I am using Laravel Passport. It all about configuration. got to config/auth.php and change driver name of api array to passport within the guards array

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

    'api' => [
        'driver' => 'passport',//instead of token 
        'provider' => 'users',
        'hash' => false,
    ],
],

Upvotes: 29

mjpsr11
mjpsr11

Reputation: 615

Despite the confusing error, I followed the steps in this post and it correctly now routes the auth request through the RequestGuard and authentication is made as expected.

Not sure how cache can get messed up to cause this, but I'm guessing that my config cache was stuck on web guard and perhaps clearing it now routes correctly through the api guard.

Hope this helps others.

Upvotes: 1

Related Questions