简体   繁体   English

我想使用laravel中的用户名或电子邮件更新登录名

[英]I want to update login with username or email in laravel

I have updated laravel default make:Auth to Login with both username and email. 我已经更新了laravel default make:Auth以使用用户名和电子邮件登录。 I just have override username function in 我只是在其中有覆盖用户名功能

loginController.php to: loginController.php可以:

public function username()
    {
        $loginType = request()->input('username');
        $this->username = filter_var($loginType , FILTER_VALIDATE_EMAIL) ? 'email' : 'username';
        request()->merge([$this->username => $loginType]);
        $this->property_exists($this, 'username') ? $this->username : 'email';
    }

but got an error: 但出现错误:

Method [property_exists] does not exist on [App\Http\Controllers\Auth\LoginController].

property_exists is a php function, not a method from some class. property_exists是php函数,而不是某些类中的方法。 So, to improve this error, just rewrite your code: 因此,要改善此错误,只需重写代码:

public function username()
{
    $loginType = request()->input('username');
    $this->username = filter_var($loginType , FILTER_VALIDATE_EMAIL) ? 'email' : 'username';
    request()->merge([$this->username => $loginType]);
    property_exists($this, 'username') ? $this->username : 'email';
}

property_exists — Checks if the object or class has a property property_exists —检查对象或类是否具有属性

bool property_exists ( mixed $class , string $property ) bool property_exists(混合$ class,字符串$ property)

public function username()
{
    $loginType = request()->input('username');
    $this->username = filter_var($loginType , FILTER_VALIDATE_EMAIL) ? 'email' : 'username';
    request()->merge([$this->username => $loginType]);
    property_exists($this, 'username') ? $this->username : 'email';
}

The username() method in the LoginController refers to the username field name sent by the request. LoginController中的username()方法引用请求发送的用户名字段名称 In your case I would imagine the field name is the same but what changes is whether you use that field to find an email or username. 在您的情况下,我可以想象该字段名称是相同的,但是发生的变化是您是否使用该字段查找电子邮件或用户名。 In that case you should be able to "trick" the user provider to do this by overriding the credentials method in your LoginController (and don't override the username function): 在这种情况下,您应该能够通过覆盖LoginController中的credentials方法来“欺骗”用户提供者(而不是覆盖username功能):

protected function credentials(Request $request)
{
    $v = \Validator::make($request->only($this->username()), [
         $this->username() => "email"
    ]);
    if ($v->passes()) {
        return [
           "email" => $request->input($this->username()),
           "password" => $request->input("password") 
       ]; 
    } else {
       return [
           "username" => $request->input($this->username()),
           "password" => $request->input("password") 
       ]; 
    }
}

This will be passed to the user provider which will select the user either by the email column or by the username column depending on what is passed. 这将被传递给用户提供其要么被选择用户email列或由username取决于什么传递列。

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

相关问题 我想在 wordpress 中使用用户名、电子邮件 ID 和电话号码登录 - I want to login with username,email id and phone number in wordpress Laravel 5 使用用户名而不是电子邮件登录 - Laravel 5 login with username instead of email Laravel - 使用用户名、电子邮件或电话登录 - Laravel - Login with username, email or phone 如何使用预定义的电子邮件/用户名和密码登录Laravel? - How Can I Login In Laravel With Predefined Email/Username and Password? 允许在 Laravel 5.4 中使用用户名或电子邮件登录 - Allow login using username or email in Laravel 5.4 用户名和电子邮件完全登录laravel 5.3 - username and email altogether login in laravel 5.3 使用电子邮件或用户名登录 Fortify Laravel 8 - Login with either email or username with Fortify Laravel 8 当用户在 laravel 中分别输入用户名和密码失败时,我想显示一条特定的消息 5 - I want to show a specific message when the user fail to login for username and password separately in laravel 5 Laravel - 使用电话和用户名登录,FILTER_VALIDATE_EMAIL 问题 - Laravel - login with phone and username, FILTER_VALIDATE_EMAIL ISSUE Laravel 5使用Attempt方法使用用户名或电子邮件自定义登录 - Laravel 5 custom login with username OR email using the Attempt method
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM