Search code examples
phplaravellaravel-passport

Laravel passport api token works for all


Using passport on Laravel 6 and it work fine with users now I want to set passport for admins I called agents here. So I did in this way (based on this):

Route::middleware('auth:agent')->group(function () {

Controller:

return Auth::guard('agent')->user();

And auth.php:

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

        'api' => [
            'driver' => 'passport',
            'provider' => 'users',
            'hash' => false,
        ],
        'agent' => [
            'driver'   => 'passport',
            'provider' => 'agents',
        ],
    ],

...

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

So I logged as user, and set api token and run this:

$user = Auth::user();
return response()->json(['success' => $user], $this-> successStatus);

This return me user details and working fine then I logged as admin with same token, and run:

return Auth::guard('agent')->user();

It return me agent details too! with same api_token that I used for users. I tested all with postman. It should not retrun agent details with users token, also on the contrary.


Solution

  • Well, while I didn't get any answer, tried to solve this issue on my own, I searched a lot and none of available solution work for me. I decided to use token as driver (basic Laravel auth) for admin users (agents)

    So I changed:

        'agent' => [
            'driver'   => 'token', // change passport to token
            'provider' => 'agents',
            'hash' => true,
        ],
    

    And according that article made api_token column and that solved my issue. I think there is no change to solve this issue by passport, you need to set token or session to get this.