Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel 5.5 api authorization with API Token

Tags:

php

laravel-5

I am trying to authenticate our user using the API token,

Here is my code config/auth.php code

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

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

My Api.php is like this

Route::group(['middleware' => ['subdomain_setup','auth::api'],'prefix'=>'v1'], function () {
    Route::get('getCoupons','Api\CouponAPI@getCoupons');
});

Now I am getting this error while accessing my api URL

Column not found: 1054 Unknown column 'api_token' in 'where clause' (SQL: select * from users where api_token =

like image 769
mohit Avatar asked Sep 26 '17 11:09

mohit


2 Answers

Make sure you have run Passport migration and ['guards']['api']['driver'] set to passport in config/auth.php, and updated the configuration cache

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

    'api' => [ 
        'driver' => 'passport', 
        'provider' => 'users', 
    ], 
],
like image 62
Sahak Hakobyan Avatar answered Oct 02 '22 08:10

Sahak Hakobyan


You must alter table to add a 'api_token' field.

Schema::create('users', function (Blueprint $table) {
    $table->increments('id');
    $table->string('name');
    $table->string('email')->unique();
    $table->string('password');

    //Add api_token field
    $table->string('api_token', 60)->unique();

    $table->rememberToken();
    $table->timestamps();
});
like image 38
huangfu Avatar answered Oct 02 '22 06:10

huangfu