phplaravel-5lumenlaravel-passportlumen-5.3

lumen passport login using username or email


Currently, I am working or lumen(Laravel's microframework), I have integrated passport in that application for user login.

I was stuck when the client told me that we need to log in with both, the username or email!

I have done that by changing lumen passports internal code as below

vendor/laravel/passport/src/Bridge/UserRepository.php in this i have updated getUserEntityByUserCredentials function code to

public function getUserEntityByUserCredentials($username, $password, $grantType, ClientEntityInterface $clientEntity)
{
     $provider = config('auth.guards.api.provider');

     if (is_null($model = config('auth.providers.'.$provider.'.model'))) {
         throw new RuntimeException('Unable to determine user model from configuration.');
     }

     if (method_exists($model, 'findForPassport')) {
         $user = (new $model)->findForPassport($username);
     } else {
         $user = (new $model)->where('email', $username)->orWhere('username', $username)->first();
     }


     if (! $user ) {
         return;
     } elseif (method_exists($user, 'validateForPassportPasswordGrant')) {
         if (! $user->validateForPassportPasswordGrant($password)) {
             return;
         }
     } elseif (! $this->hasher->check($password, $user->password)) {
         return;
     }

     return new User($user->getAuthIdentifier());
}

Now when I update vendor directory every time I need to change this code.

Any simple method with which I can tolerate this headache.

Any help appreciated Thanks in advance.


Solution

  • I didn't know it was this simple, I just tried today and I got succeeded,

    public function findForPassport($username){
        return $user = (new User)->where('email', $username)->orWhere('username', $username)->first();
    }
    

    I just add this method to App\User.php and it works perfectly as I wanted.