Lravel 5.4: JWT API with multi-auth on two tables one works the other not

后端 未结 3 607
闹比i
闹比i 2020-12-16 08:31

I am using...

  • Laravel 5.4
  • tymon/jwt-auth : 1.0.0-rc.2

I have application with two authentications API one is customers and

3条回答
  •  误落风尘
    2020-12-16 08:44

    First let me thank you @AmrAbdelRahman for you efforts and your time.

    My problem was the application always using my default authentication "guard" as my default looks like that

    'defaults' => [
            'guard'     => 'api',
            'passwords' => 'users',
        ],
    

    so every time I try to authenticate the other user which was the driver it fails during the default authenticate is api and the it should using driver in this case.

    what I did in my case was making a switcher in my App\Providers\AppServiceProvider under the boot here is how it looks like

    $this->app['router']->matched(function (\Illuminate\Routing\Events\RouteMatched $e) {
                $route = $e->route;
                if (!array_has($route->getAction(), 'guard')) {
                    return;
                }
                $routeGuard = array_get($route->getAction(), 'guard');
                $this->app['auth']->resolveUsersUsing(function ($guard = null) use ($routeGuard) {
                    return $this->app['auth']->guard($routeGuard)->user();
                });
                $this->app['auth']->setDefaultDriver($routeGuard);
            });
    

    Now in my case if the $guard =api it will read that guard and act correctly and if it's driver it will use the new guard and act as expected. Hope this will help some one in future.

提交回复
热议问题