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

查看:26
本文介绍了使用 Guards 的 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?

推荐答案

我使用一个简单的中间件成功地创建了多个身份验证(使用 laravel/passport).

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

第一步: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' => AppUser::class,
    ],
    'admin_users' => [
        'driver' => 'eloquent',
        'model' => AppAdminUser::class,
    ],
    'basic_users' => [
        'driver' => 'eloquent',
        'model' => AppBasicUser::class,
    ],
],

通过 CLI 清理缓存

Clean the cache via 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' => AppHttpMiddlewareAdminUserProvider::class,
];

并确保它位于 $middlewarePriority 的顶部

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

protected $middlewarePriority = [
    AppHttpMiddlewareAdminUserProvider::class,
    ...
];

第 4 步:向路由添加中间件

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

第 5 步:登录控制器(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 防护提供程序更改为正确的类.

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

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

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