Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel 5.2 Authentication via API

I am developing a RESTful API with Laravel 5.2. In the token guard located at \Illuminate\Auth\TokenGuard\TokenGuard.php on line 46 the column name for the token is defined as api_token:

$this->storageKey = 'api_token';

I want to change this column name to something else, for example api_key.

How can I do this? I do not want to modify the core TokenGuard.php file.

like image 249
Vijai Pandey Avatar asked Jan 18 '16 05:01

Vijai Pandey


People also ask

How can I get my Auth ID in Laravel API?

You can use auth()->user()->id or Auth::user()->id to get current user id. Save this answer.

How do I use authentication token in REST API?

Users of the REST API can authenticate by providing a user ID and password to the REST API login resource with the HTTP POST method. An LTPA token is generated that enables the user to authenticate future requests. This LTPA token has the prefix LtpaToken2 .

How can we implement authentication in Laravel?

Just run php artisan make:auth and php artisan migrate in a fresh Laravel application. Then, navigate your browser to http://your-app.test/register or any other URL that is assigned to your application. These two commands will take care of scaffolding your entire authentication system!

What is Laravel Auth API?

Laravel includes an authentication guard that will automatically validate API tokens on incoming requests. You only need to specify the auth:api middleware on any route that requires a valid access token: use Illuminate\Http\Request; Route::middleware('auth:api')->get('/user', function(Request $request) {


2 Answers

The built in TokenGuard does not have a way to modify the storageKey field. Therefore, you will need to create your own Guard class that sets the field, and tell Auth to use your Guard class.

First, start by creating a new Guard class that extends the base TokenGuard class. In this example, it is created at app/Services/Auth/MyTokenGuard.php:

namespace App\Services\Auth;

use Illuminate\Http\Request;
use Illuminate\Auth\TokenGuard;
use Illuminate\Contracts\Auth\UserProvider;

class MyTokenGuard extends TokenGuard
{
    public function __construct(UserProvider $provider, Request $request)
    {
        parent::__construct($provider, $request);
        $this->inputKey = 'api_key'; // if you want to rename this, as well
        $this->storageKey = 'api_key';
    }
}

Once you've created your class, you need to let Auth know about it. You can do this in the boot() method on your AuthServiceProvider service provider:

public function boot(GateContract $gate)
{
    $this->registerPolicies($gate);

    Auth::extend('mytoken', function($app, $name, array $config) {
        return new \App\Services\Auth\MyTokenGuard(Auth::createUserProvider($config['provider']), $app['request']);
    });
}

And finally, you need to tell Auth to use your new mytoken guard. This is done in the config/auth.php config file.

'guards' => [
    'api' => [
        'driver' => 'mytoken',
        'provider' => 'users',
    ],
],
like image 174
patricus Avatar answered Sep 24 '22 15:09

patricus


Unfortunately there is no way to configure that.

The only way to use an other key is to create your own "Guard": Adding Custom Guards.

You can extend the TokenGuard class and override the __constructor with your own column names.

like image 36
Fiete Avatar answered Sep 20 '22 15:09

Fiete