使用 Passport 结合 guard 时报 attempt 不存在

想使用 admin 表作为后台认证,并结合 passport 使用,使用 Auth::guard('admin')->attempt 验证登录是否可行?
之前使用的是 jwt。以下是代码和报错

config/auth.php

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

        'admin' => [
            'driver' => 'passport',
            'provider' => 'admins',
        ]
    ],

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

        'admins' => [
            'driver' => 'eloquent',
            'model' => App\Admin::class
        ]
    ],

Admin.php

use Illuminate\Foundation\Auth\User as Authenticatable;
use Laravel\Passport\HasApiTokens;

class Admin extends Authenticatable
{
    use HasApiTokens;

public function findForPassport($username)
    {
        return self::orWhere('email', $username)->orWhere('username', $username)->first();
    }
}

中间件修改认证

  public function handle($request, Closure $next)
    {
        try {
            if ($request->is('admin/*')) {
                config(['auth.guards.api.provider' => 'admins']);
            } elseif ($request->is('api/*')) {
                config(['auth.guards.api.provider' => 'api']);
            }

        } catch (\Exception $exception) {
            throw new $exception;
        }
        return $next($request);
    }

AuthController.php

 public function login(Request $request)
    {
        $credentials = [
            'username' => $request->username,
            'password' => $request->password
        ];

        dd(Auth::guard('admin')->attempt($credentials), config('auth.guards.api.provider'));
        if (!Auth::attempt($credentials)) {
            return response()->json([
                'message' => 'Username Or Password Error'
            ], 401);
        }
    }

postman 报错

BadMethodCallException: Method Illuminate\Auth\RequestGuard::attempt does not exist. in file /home/vagrant/Code/Laravel/demo-v2/laravel/vendor/laravel/framework/src/Illuminate/Support/Traits/Macroable.php on line 104

#0 /home/vagrant/Code/Laravel/demo-v2/laravel/app/Http/Controllers/Admin/AuthController.php(22): Illuminate\Auth\RequestGuard->__call('attempt', Array)
#1 [internal function]: App\Http\Controllers\Admin\AuthController->login(Object(Illuminate\Http\Request))
#2 /home/vagrant/Code/Laravel/demo-v2/laravel/vendor/laravel/framework/src/Illuminate/Routing/Controller.php(54): call_user_func_array(Array, Array)
#3 /home/vagrant/Code/Laravel/demo-v2/laravel/vendor/laravel/framework/src/Illuminate/Routing/ControllerDispatcher.php(45): Illuminate\Routing\Controller->callAction('login', Array)
#4 /home/vagrant/Code/Laravel/demo-v2/laravel/vendor/laravel/framework/src/Illuminate/Routing/Route.php(219): Illuminate\Routing\ControllerDispatcher->dispatch(Object(Illuminate\Routing\Route), Object(App\Http\Controllers\Admin\AuthController), 'login')
#5 /home/vagrant/Code/Laravel/demo-v2/laravel/vendor/laravel/framework/src/Illuminate/Routing/Route.php(176): Illuminate\Routing\Route->runController()
#6 /home/vagrant/Code/Laravel/demo-v2/laravel/vendor/laravel/framework/src/Illuminate/Routing/Router.php(680): Illuminate\Routing\Route->run()
#7 /home/vagrant/Code/Laravel/demo-v2/laravel/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(130): Illuminate\Routing\Router->Illuminate\Routing\{closure}(Object(Illuminate\Http\Request))
#8 /home/vagrant/Code/Laravel/demo-v2/laravel/app/Http/Middleware/PassportGuard.php(28): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#9 /home/vagrant/Code/Laravel/demo-v2/laravel/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(171): App\Http\Middleware\PassportGuard->handle(Object(Illuminate\Http\Request), Object(Closure))
#10 /home/vagrant/Code/Laravel/demo-v2/laravel/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(105): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#11 /home/vagrant/Code/Laravel/demo-v2/laravel/vendor/laravel/framework/src/Illuminate/Routing/Router.php(682): Illuminate\Pipeline\Pipeline->then(Object(Closure))
#12 /home/vagrant/Code/Laravel/demo-v2/laravel/vendor/laravel/framework/src/Illuminate/Routing/Router.php(657): Illuminate\Routing\Router->runRouteWithinStack(Object(Illuminate\Routing\Route), Object(Illuminate\Http\Request))
#13 /home/vagrant/Code/Laravel/demo-v2/laravel/vendor/laravel/framework/src/Illuminate/Routing/Router.php(623): Illuminate\Routing\Router->runRoute(Object(Illuminate\Http\Request), Object(Illuminate\Routing\Route))
#14 /home/vagrant/Code/Laravel/demo-v2/laravel/vendor/laravel/framework/src/Illuminate/Routing/Router.php(612): Illuminate\Routing\Router->dispatchToRoute(Object(Illuminate\Http\Request))
#15 /home/vagrant/Code/Laravel/demo-v2/laravel/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php(176): Illuminate\Routing\Router->dispatch(Object(Illuminate\Http\Request))
#16 /home/vagrant/Code/Laravel/demo-v2/laravel/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(130): Illuminate\Foundation\Http\Kernel->Illuminate\Foundation\Http\{closure}(Object(Illuminate\Http\Request))
#17 /home/vagrant/Code/Laravel/demo-v2/laravel/vendor/barryvdh/laravel-debugbar/src/Middleware/InjectDebugbar.php(65): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#18 /home/vagrant/Code/Laravel/demo-v2/laravel/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(171): Barryvdh\Debugbar\Middleware\InjectDebugbar->handle(Object(Illuminate\Http\Request), Object(Closure))
#19 /home/vagrant/Code/Laravel/demo-v2/laravel/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/TransformsRequest.php(21): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#20 /home/vagrant/Code/Laravel/demo-v2/laravel/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(171): Illuminate\Foundation\Http\Middleware\TransformsRequest->handle(Object(Illuminate\Http\Request), Object(Closure))
#21 /home/vagrant/Code/Laravel/demo-v2/laravel/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/TransformsRequest.php(21): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#22 /home/vagrant/Code/Laravel/demo-v2/laravel/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(171): Illuminate\Foundation\Http\Middleware\TransformsRequest->handle(Object(Illuminate\Http\Request), Object(Closure))
#23 /home/vagrant/Code/Laravel/demo-v2/laravel/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/ValidatePostSize.php(27): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#24 /home/vagrant/Code/Laravel/demo-v2/laravel/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(171): Illuminate\Foundation\Http\Middleware\ValidatePostSize->handle(Object(Illuminate\Http\Request), Object(Closure))
#25 /home/vagrant/Code/Laravel/demo-v2/laravel/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/CheckForMaintenanceMode.php(62): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#26 /home/vagrant/Code/Laravel/demo-v2/laravel/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(171): Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode->handle(Object(Illuminate\Http\Request), Object(Closure))
#27 /home/vagrant/Code/Laravel/demo-v2/laravel/vendor/fideloper/proxy/src/TrustProxies.php(57): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#28 /home/vagrant/Code/Laravel/demo-v2/laravel/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(171): Fideloper\Proxy\TrustProxies->handle(Object(Illuminate\Http\Request), Object(Closure))
#29 /home/vagrant/Code/Laravel/demo-v2/laravel/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(105): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#30 /home/vagrant/Code/Laravel/demo-v2/laravel/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php(151): Illuminate\Pipeline\Pipeline->then(Object(Closure))
#31 /home/vagrant/Code/Laravel/demo-v2/laravel/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php(116): Illuminate\Foundation\Http\Kernel->sendRequestThroughRouter(Object(Illuminate\Http\Request))
#32 /home/vagrant/Code/Laravel/demo-v2/laravel/public/index.php(55): Illuminate\Foundation\Http\Kernel->handle(Object(Illuminate\Http\Request))
#33 {main}
《L01 基础入门》
我们将带你从零开发一个项目并部署到线上,本课程教授 Web 开发中专业、实用的技能,如 Git 工作流、Laravel Mix 前端工作流等。
《L05 电商实战》
从零开发一个电商项目,功能包括电商后台、商品 & SKU 管理、购物车、订单管理、支付宝支付、微信支付、订单退款流程、优惠券等
讨论数量: 5

passport内部用的也是JWT,换JWT吧,passport研究了好几天愣是没弄明白,
我只知道RequestGuard确实没有attemp方法

1年前 评论
____ (楼主) 1年前

@Big__ passport 确实不支持 attempt 的,这一篇评论里有写
博客:Laravel5.5+passport 放弃 dingo 开发 API 实战,让 API 开发更省心

做法是 use AuthenticatesUsers; 使用这个 Trait, 然后通过 $this->guard('api')->attempt([]) 的方式,这个会获取 SessionGuard 的实例,调用里面的 attempt 方法,passport 中的 RequestGuard 默认没有实现 attempt 方法,或者你重写一个 Guard 都可以

所以这也解释了为什么其他人的例子里都是采用先找到用户
然后 通过 HashCheck 对比密码检验密码是否正确

 $admin_user = AdminUser::where('email', $request->email)
            ->firstOrFail();

        if (!Hash::check($request->password, $admin_user->password)) {
            return $this->failed('密码不正确');
        }

博客:passport API 认证 -- 多表登录

1年前 评论

换下守卫的位置

1年前 评论
____ (楼主) 1年前
jenkincei

Passport的guard并未实现attempt方法

1年前 评论

我也遇到这样的问题了!唉,没办法啦!

1个月前 评论

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!