简体   繁体   English

流明护照使用用户名或电子邮件登录

[英]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. 目前,我正在工作或流明(Laravel的微框架),我已在该应用程序中集成了护照以供用户登录。

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 vendor / laravel / passport / src / Bridge / UserRepository.php在此我更新了getUserEntityByUserCredentials函数代码

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. 任何帮助表示感谢提前。

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. 我只是将这个方法添加到App \\ User.php中 ,它可以完美地运行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM