K Hsueh
K Hsueh

Reputation: 620

Laravel Passport column \"api_token\" does not exist

I am setting up laravel passport on a api project. I try to follow the steps on this site but couldn't get the authenication to work.

The requesting a token part seems to work fine. When making a call to http://127.0.0.1:8000/oauth/token, it return a valid token.

When I send a request to the api with the token in the Authorization Header, it gives a column "api_token" does not exit error

Authorization Header: Authorization:Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1Ni........

Error:

"SQLSTATE[42703]: Undefined column: 7 ERROR:  column "api_token" does not exist↵LINE 1: select * from "users" where "api_token" = $1 limit 1↵  

Do I need to create the api_token column myself? I used the default migration file to create the table. This is the migration file for the users table

/**
 * Run the migrations.
 *
 * @return void
 */
public function up()
{
    Schema::create('users', function (Blueprint $table) {
        $table->increments('id');
        $table->string('name');
        $table->string('email')->unique();
        $table->string('password');
        $table->rememberToken();
        $table->timestamps();
    });
}

Upvotes: 0

Views: 5347

Answers (5)

Nikita Makovey
Nikita Makovey

Reputation: 39

The solution is php artisan config:cache (don't forget do this after any changes in config directory).

Upvotes: 2

ruchita
ruchita

Reputation: 41

Change guards of config/auth.php file with this.

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

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

after changing auth.php, clear all cache.

Upvotes: 4

Octavio Santelices
Octavio Santelices

Reputation: 11

your problem is in config/auth.php

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

im atach complete code for auth.php here

<?php

return [

    /*
    |--------------------------------------------------------------------------
    | Authentication Defaults
    |--------------------------------------------------------------------------
    |
    | This option controls the default authentication "guard" and password
    | reset options for your application. You may change these defaults
    | as required, but they're a perfect start for most applications.
    |
    */

    'defaults' => [
        'guard' => 'web',
        'passwords' => 'users',
    ],

    /*
    |--------------------------------------------------------------------------
    | Authentication Guards
    |--------------------------------------------------------------------------
    |
    | Next, you may define every authentication guard for your application.
    | Of course, a great default configuration has been defined for you
    | here which uses session storage and the Eloquent user provider.
    |
    | All authentication drivers have a user provider. This defines how the
    | users are actually retrieved out of your database or other storage
    | mechanisms used by this application to persist your user's data.
    |
    | Supported: "session", "token"
    |
    */

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

    /*
    |--------------------------------------------------------------------------
    | User Providers
    |--------------------------------------------------------------------------
    |
    | All authentication drivers have a user provider. This defines how the
    | users are actually retrieved out of your database or other storage
    | mechanisms used by this application to persist your user's data.
    |
    | If you have multiple user tables or models you may configure multiple
    | sources which represent each model / table. These sources may then
    | be assigned to any extra authentication guards you have defined.
    |
    | Supported: "database", "eloquent"
    |
    */

    'providers' => [
        'users' => [
            'driver' => 'eloquent',
            'model' => App\Models\User::class,
        ],


    ],

    /*
    |--------------------------------------------------------------------------
    | Resetting Passwords
    |--------------------------------------------------------------------------
    |
    | You may specify multiple password reset configurations if you have more
    | than one user table or model in the application and you want to have
    | separate password reset settings based on the specific user types.
    |
    | The expire time is the number of minutes that the reset token should be
    | considered valid. This security feature keeps tokens short-lived so
    | they have less time to be guessed. You may change this as needed.
    |
    */

    'passwords' => [
        'users' => [
            'provider' => 'users',
            'table' => 'password_resets',
            'expire' => 60,
        ],
    ],

];

Upvotes: 0

Ruan Nawe
Ruan Nawe

Reputation: 470

The solution is simple!

go to file: config/auth.php

identify this code block:

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

and change to:

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

Upvotes: 1

Demonyowh
Demonyowh

Reputation: 1672

in your users migration you must add api_token

public function up()
{
    Schema::create('users', function (Blueprint $table) {
        $table->increments('id');
        $table->string('name');
        $table->string('email')->unique();
        $table->string('password');
        $tabke->string('api_token'); // specify the length also
        $table->rememberToken();
        $table->timestamps();
    });
}

Upvotes: -1

Related Questions