Multi Auth with Laravel 5.4 and Passport

后端 未结 7 2132
猫巷女王i
猫巷女王i 2020-12-31 20:46

I am trying to setup multi auth with Laravel Passport, but it doesn\'t seem to support it. I am using the Password Grant to issue tokens which requires me to pass username/p

7条回答
  •  爱一瞬间的悲伤
    2020-12-31 21:26

    You have to modify the main library Files.

    1) File: vendor\laravel\passport\src\Bridge\UserRepository.php

    Find getUserEntityByUserCredentials and Copy the complete method and Paste this method below with name getEntityByUserCredentials. Donot modify the main function because it is used somewhere.

    //Add the $provider variable at last or replace this line.
    public function getEntityByUserCredentials($username, $password, $grantType, ClientEntityInterface $clientEntity, $provider)
    

    Then, in the new duplicated function, find the below:

    $provider = config('auth.guards.api.provider');
    

    and Replace it with:

    $provider = config('auth.guards.'.$provider.'.provider');
    

    2) File: vendor\league\oauth2-server\src\Grant\PasswordGrant.php

    In the function validateUser add the below code after line no. 88

    $provider = $this->getRequestParameter('provider', $request);
    
    if (is_null($provider)) {
         throw OAuthServerException::invalidRequest('provider');
    }
    

    After adding replace the following code with

    $user = $this->userRepository->getEntityByUserCredentials(
            $username,
            $password,
            $this->getIdentifier(),
            $client,
            $provider
        );
    

    Now try this using postman

    Add the provider field in your input field like

    provider = api_vendors
    OR
    provider = api_admins
    OR
    provider = api_users
    And so on....
    

    make sure you have added your provider and set the drivers in the config/auth.php

    'guards' => [
     'api_admins' => [
        'driver' => 'passport',
        'provider' => 'admins',
      ],
      'api_vendors' => [
        'driver' => 'passport',
        'provider' => 'vendors',
      ],
    

    I hope this helps.

提交回复
热议问题