I hope this answer can help others.
If you want to add a variable and pass this variable to the findPassport function in the User Authenticate model, you need to update 3 classes in the passport:
- UserRepositoryInterface in vendor \ league \ oauth2-server \ src \ Repositories \ UserRepositoryInterface
- PasswordGrant in vendor \ league \ oauth2-server \ src \ Grant \ PasswordGrant
- UserRepository in the vendor \ laravel \ passport \ src \ Bridge \ UserRepository
in the code example, I will add a parent variable, and the code will look like this
+ in class UserRepositoryInterface
interface UserRepositoryInterface extends RepositoryInterface { public function getUserEntityByUserCredentials( $username, $password, $parent, <------variable example $grantType, ClientEntityInterface $clientEntity ); }
+ in class PasswordGrant
class PasswordGrant extends AbstractGrant{ protected function validateUser(ServerRequestInterface $request, ClientEntityInterface $client) { $username = $this->getRequestParameter('username', $request); if (is_null($username)) { throw OAuthServerException::invalidRequest('username'); } $password = $this->getRequestParameter('password', $request); if (is_null($password)) { throw OAuthServerException::invalidRequest('password'); } $parent = $this->getRequestParameter('parent', $request); if (is_null($parent)) { throw OAuthServerException::invalidRequest('password'); } $user = $this->userRepository->getUserEntityByUserCredentials( $username, $password, $parent, <--- variable example get from request $this->getIdentifier(), $client ); if ($user instanceof UserEntityInterface === false) { $this->getEmitter()->emit(new RequestEvent(RequestEvent::USER_AUTHENTICATION_FAILED, $request)); throw OAuthServerException::invalidCredentials(); } return $user; } }
+ in class UserRepository
class UserRepository implements UserRepositoryInterface { public function getUserEntityByUserCredentials($username, $password, $parent, $grantType, ClientEntityInterface $clientEntity) /*add 1more parameter that implement from UserRepositoryInterface*/ { $provider = config('auth.guards.api.provider'); if (is_null($model = config('auth.providers.'.$provider.'.model'))) { throw new RuntimeException('Unable to determine authentication model from configuration.'); } if (method_exists($model, 'findForPassport')) { $user = (new $model)->findForPassport($username,$parent); <--- finally we pass parent variable to findForPassport here } else { $user = (new $model)->where('email', $username)->first(); } if (! $user) { return; } elseif (method_exists($user, 'validateForPassportPasswordGrant')) { if (! $user->validateForPassportPasswordGrant($password)) { return; } } elseif (! $this->hasher->check($password, $user->getAuthPassword())) { return; } return new User($user->getAuthIdentifier()); } }
then u can get the $ parent value from the parameter in findForPassport. but make sure you return the value as an eloquent user. If you want to join the table, you can see my sample code below
class User extends Authenticatable{ .......... public function findForPassport($identifier,$parent) { $a = $this ->Join('role as r', 'r.user_id', '=', 'users.id') ->get(); return $a->where('name', $identifier)->where('role_id',$parent)->first(); } }