tymondesigns / jwt-auth

🔐 JSON Web Token Authentication for Laravel & Lumen
https://jwt-auth.com
MIT License
11.23k stars 1.55k forks source link

When config('auth.defaults.guard') is web, the authenticate() method in middleware returns false #1859

Open parvinshi opened 4 years ago

parvinshi commented 4 years ago

When config('auth.defaults.guard') is 'web', the authenticate() method in middleware returns false, But when changing 'auth.defaults.guard ' to 'api', $this->auth->parseToken()->authenticate() in middleware returns user model

Your environment

Q A
Framework Laravel
Framework version 5.8.31
Package version 1.0.0-rc.4.1
PHP version 7.2.20
parvinshi commented 4 years ago
public function guard($name = null)
{
   $name = $name ?: $this->getDefaultDriver();

   return $this->guards[$name] ?? $this->guards[$name] = $this->resolve($name);
}
kilvn commented 4 years ago

我也遇到了同样的问题,尝试设置 config('auth.defaults.guard', 'api'); 或者 auth(‘api’) 都没用。

parvinshi commented 4 years ago

我也遇到了同样的问题,尝试设置 config('auth.defaults.guard', 'api'); 或者 auth(‘api’) 都没用。

$this->auth->parseToken()->authenticate()的$this->auth就是auth(),也可以写成auth()->parseToken()->authenticate(),默认解析的是auth.php配置下的defaults数组的guard键对应的Authentication Guards,laravel默认是'web',然后通过guards数组找到身份认证使用的provider model。你把defaults配置数组的guard值改为'api',provider对应providers配置数组的model对应好,然后再试试

parvinshi commented 4 years ago

我也遇到了同样的问题,尝试设置 config('auth.defaults.guard', 'api'); 或者 auth(‘api’) 都没用。

这个包里面对laravel的Auth进行了扩展,具体代码如下:

protected function extendAuthGuard()
{
        $this->app['auth']->extend('jwt', function ($app, $name, array $config) {
            $guard = new JWTGuard(
                $app['tymon.jwt'],
                $app['auth']->createUserProvider($config['provider']),
                $app['request']
            );

            $app->refresh('request', $guard, 'setRequest');

            return $guard;
        });
}

这里$this->app['auth']就是上面说的$this->auth、auth()或者Auth::guard(),它进行的拓展是在laravel的auth.php中defaults配置数组guard值基础上,如果默认为web,自己定义的jwt如果和默认web定义的provider model不一样就会找不到认证的用户

stale[bot] commented 3 years ago

Is this still relevant? If so, what is blocking it? Is there anything you can do to help move it forward?

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs.