如何让JWTAuth服务于不同的用户类型?

时间:2018-04-24 03:50:22

标签: php authentication laravel-5 jwt token

我有这个工作方法来记录用户:

// Authenticate Business User
public function authBU() {

    $this->validate( request(), [
        'provider' => 'required',
        'token'    => 'required',
    ] );    
    $user = JWTAuth::authenticate(request( 'token' ));

    return $this->buildItemResponse( $user, new UserLoginTransformer() );
}

它从User表返回,但是,我需要它来查看BusinessUser表。

我已在bsuiness_users

中设置了auth.php
'guards' => [
        'web' => [
            'driver'   => 'session',
            'provider' => 'users',
        ],

        'api' => [
            'driver'   => 'token',
            'provider' => 'users',
        ],

        'business_api' => [
            'driver'   => 'passport',
            'provider' => 'business_users',
        ],
    ],


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

        'business_users' => [
            'driver' => 'eloquent',
            'model' => \App\Services\Users\BusinessUser::class,
        ],
    ],

如何在不编辑供应商代码的情况下让JWTAuth::authenticate(request( 'token' ));返回BusinessUser

这是JWTAuth.php中的方法

/**
 * Authenticate a user via a token.
 *
 * @param mixed $token
 *
 * @return mixed
 */
public function authenticate($token = false)
{
    $id = $this->getPayload($token)->get('sub');

    if (! $this->auth->byId($id)) {
        return false;
    }

    return $this->auth->user();
}

0 个答案:

没有答案