Allow login using username or email in Laravel 5.4

后端 未结 10 2232
南旧
南旧 2021-02-04 00:48

Now I\'ve followed the Laravel documentation on how to allow usernames during authentication, but it takes away the ability to use the email. I want to allow users to use their

10条回答
  •  佛祖请我去吃肉
    2021-02-04 01:22

    Add this code to your login controller - Hope it will works. Reference login with email or username in one field

     public function __construct()
    {
        $this->middleware('guest')->except('logout');
    
        $this->username = $this->findUsername();
    }
    
    public function findUsername()
    {
        $login = request()->input('login');
    
        $fieldType = filter_var($login, FILTER_VALIDATE_EMAIL) ? 'email' : 'username';
    
        request()->merge([$fieldType => $login]);
    
        return $fieldType;
    }
    
    public function username()
    {
        return $this->username;
    }
    

提交回复
热议问题