0

I using passport and I want to use for multiple auth so I done this by this topic , just I use name as agent instead of admin, what I trying to do now is get admin id.

Here is my code:

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

And:

public function handle($request, Closure $next)
{
    config(['auth.guards.api.provider' => 'agents']);
    return $next($request);
}

And:

Route::group(['middleware' => ['auth.agent']], function() {

So for getting id I tried:

return Auth::guard('Agent')->id;

Also with these names agent agents Agents

It return me a error:

Auth guard [Agents] is not defined

Sorry, I'm new to Laravel, any idea?

Edit: I also tried

php artisan config:clear  
php artisan config:cache
Salman Zafar
  • 3,844
  • 5
  • 20
  • 43
Jack The Baker
  • 1,781
  • 1
  • 20
  • 51

1 Answers1

2

The guard you have setup has the name "agents" so this is how you should access it.

What i think you have done is returned the SessionGuard object rather than your user model.

Try these:

return Auth::guard('agents')->check(); # true
return Auth::guard('agents')->user()->id; # (int)

Ammendment:

In config/auth.php update the guards array

'guards' => [
    'agents' => [
        'driver'   => 'session',
        'provider' => 'agents',
    ],
]

OR: use Auth::guard('api')->user()->id

Spholt
  • 3,724
  • 1
  • 18
  • 29
  • as I said already tried this, this return `Auth guard [agents] is not defined` – Jack The Baker Nov 12 '19 at 11:59
  • What is the output of `dd(config('auth.guards'));` ? – Spholt Nov 12 '19 at 12:09
  • `array:2 [ "web" => array:2 [ "driver" => "session" "provider" => "users" ] "api" => array:3 [ "driver" => "passport" "provider" => "agents" "hash" => false ] ]` – Jack The Baker Nov 12 '19 at 12:11
  • Ah that will be why it's not understanding! – Spholt Nov 12 '19 at 12:18
  • I've updated my answer, it looks like you hadn't configured the guard, just added the provider – Spholt Nov 12 '19 at 12:21
  • If you are using `Passport` for this guard, you will want to switch out the driver to `passport` – Spholt Nov 12 '19 at 12:22
  • `Argument 2 passed to Illuminate\Auth\SessionGuard::__construct() must implement interface Illuminate\Contracts\Auth\UserProvider, null given, called in E:\homerapi\vendor\laravel\framework\src\Illuminate\Auth\AuthManager.php on line 125` – Jack The Baker Nov 12 '19 at 12:35
  • Ok, that's progress. Does your `Agent` model extend `Authenticatable` (a.k.a `Illuminate\Foundation\Auth\User`)? – Spholt Nov 12 '19 at 12:48
  • Yes `use Illuminate\Foundation\Auth\User as Authenticatable;` – Jack The Baker Nov 12 '19 at 12:57
  • 1
    Ok, so something is misconfigured in your `config.auth.providers` array by the sound of it. The error is happening when it tries to register the `SessionGuard` but isn't getting a `driver`. Instead it is getting `null` which is causing your problem. I can't spend much more time on this until this evening so good luck! I hope i've been of some help ... – Spholt Nov 12 '19 at 13:14