dingo / api

A RESTful API package for the Laravel and Lumen frameworks.
BSD 3-Clause "New" or "Revised" License
9.33k stars 1.25k forks source link

Multiple Auth Guards? #1521

Open murilo-campaner opened 6 years ago

murilo-campaner commented 6 years ago
Q A
Bug? no
New Feature? no
Framework Laravel
Framework version 5.5
PHP version 7.1.11

Actual Behaviour

Hello everyone... I'm having the following problem.. I use web guard with "Collaborator" model, but in api guard, I want to use "Responsible" model for authentication (basic)...

When I set an username/password of "Collaborator" in Authentication header, the request return success. But when I set an username/password of "Responsible" model it returns "Invalid credentials".

Can anyone help-me with this problem? Here's some pieces of code:

config/auth.php

'guards' => [
        'web' => [
            'driver' => 'session',
            'provider' => 'Collaborator',
        ],
        'api' => [
            'driver' => 'token',
            'provider' => 'Responsible',
        ],
],
'providers' => [
        'Collaborator' => [
            'driver' => 'eloquent',
            'model' => App\Models\Collaborator::class,
        ],
         'Responsible' => [
             'driver' => 'eloquent',
             'model' => App\Models\Responsible::class,
         ],
]

App/Providers/ApiServiceProvider.php

public function register()
    {
        $this->app['Dingo\Api\Transformer\Factory']->setAdapter(function ($app) {
            $fractal = new Manager();

            $fractal->setSerializer(new JsonApiSerializer());

            return new Fractal($fractal);
        });

        app('Dingo\Api\Auth\Auth')->extend('basic', function ($app) {
            return new Basic($app['auth'], 'cpf');
        });
    }

routes/api.php

$api->version('v1', function ($api) {
    $api->group(['namespace' => 'App\Api\V1\Controllers'], function ($api) { // Use this route group for v1
        $api->post('/login', 'AuthController@login')->name('users.login');
        $api->get('/teste', 'AuthController@teste')->name('users.test')->middleware('api.auth');
    });
});