使用Guard的Laravel Passport多重身份验证 [英] Laravel Passport Multiple Authentication using Guards

查看:287
本文介绍了使用Guard的Laravel Passport多重身份验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们可以使用具有不同防护措施的laravel通行证来为两种不同类型的用户认证API吗? 例如,对于驱动程序用户,我们有驱动程序应用程序;对于卖方用户,我们有供应商应用程序.两者都有不同的模型驱动程序和供应商. 我们如何使用Laravel Passport使用不同的防护措施来验证两种类型的用户?

Can we use laravel passport with different guards to authenticate APIs for two different types of users. For example we have driver app for driver user and vendor app for vendor user. Both have their different models Driver and Vendor. How can we use different guards to authenticate both types of users using Laravel Passport?

推荐答案

我设法使用一个简单的中间件创建了多个auth(使用laravel/passport).

I managed to create multiple auths (with laravel/passport) by using a simple middlware.

步骤1:config/auth.php

将您的用户类别添加到提供商

Add your user classes to providers

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

    'api' => [
        'driver' => 'passport',
        'provider' => 'basic_users', // default
    ],        
],

...

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

通过CLI清理缓存

php artisan config:cache

第2步:创建中间件

php artisan make:middleware AdminUserProvider

在app/Http/Middleware中打开新创建的中间件,并更新如下所示的hand方法

Open the newly created middleware in app/Http/Middleware and update the hand method like below

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

第3步:注册您的中间件

将新创建的中间件添加到$ routeMiddleware

Add the newly created middleware to $routeMiddleware

protected $routeMiddleware = [
    ...
    'auth.admin' => \App\Http\Middleware\AdminUserProvider::class,
];

并确保它位于$ middlewarePriority的顶部

and make sure it's at the top of $middlewarePriority

protected $middlewarePriority = [
    \App\Http\Middleware\AdminUserProvider::class,
    ...
];

第4步:添加要路由的中间件

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

步骤5:LoginControllers(AdminUserController和BasicUserController)

public function login()
{
    $validatedData = request()->validate([
        'email' => 'required',
        'password' => 'required|min:6'
    ]);
    // get user object
    $user = AdminUser::where('email', request()->email)->first();
    // do the passwords match?
    if (!Hash::check(request()->password, $user->password)) {
        // no they don't
        return response()->json(['error' => 'Unauthorized'], 401);
    }
    // log the user in (needed for future requests)
    Auth::login($user);
    // get new token
    $tokenResult = $user->createToken($this->tokenName);
    // return token in json response
    return response()->json(['success' => ['token' => $tokenResult->accessToken]], 200);
}

总结:

登录控制器使用Eloquent模型获取用户对象,然后通过Auth :: login($ user)登录用户

The login controllers use Eloquent models to get the user object and then log the user in through Auth::login($user)

然后,对于将来需要身份验证的请求,新的中间件会将api auth Guard提供程序更改为正确的类.

Then for future requests that need authentication, the new middleware will change the api auth guard provider to the correct class.

这篇关于使用Guard的Laravel Passport多重身份验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆