Allow login using username or email in Laravel 5.4

后端 未结 10 1752
南旧
南旧 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 00:57

    I think a simpler way is to just override the username method in LoginController:

    public function username()
    {
       $login = request()->input('login');
       $field = filter_var($login, FILTER_VALIDATE_EMAIL) ? 'email' : 'username';
       request()->merge([$field => $login]);
       return $field;
    }
    
    0 讨论(0)
  • 2021-02-04 00:59

    You need to override protected function attemptLogin(Request $request) method from \Illuminate\Foundation\Auth\AuthenticatesUsers Trait in your LoginController i.e. in my LoginController class

    protected function attemptLogin(Request $request) {
    
        $identity = $request->get("usernameOrEmail");
        $password = $request->get("password");
    
        return \Auth::attempt([
            filter_var($identity, FILTER_VALIDATE_EMAIL) ? 'email' : 'username' => $identity,
            'password' => $password
        ]);
    }
    

    Your LoginController class should use Trait \Illuminate\Foundation\Auth\AuthenticatesUsers in order to override attemptLogin method i.e.

    class LoginController extends Controller {
    
         use \Illuminate\Foundation\Auth\AuthenticatesUsers;
         .......
         .......
    }
    
    0 讨论(0)
  • 2021-02-04 01:07

    Follow instructions from this link: https://laravel.com/docs/5.4/authentication#authenticating-users

    Then you can check for the user input like this

    $username = $request->username; //the input field has name='username' in form
    
    if(filter_var($username, FILTER_VALIDATE_EMAIL)) {
        //user sent their email 
        Auth::attempt(['email' => $username, 'password' => $password]);
    } else {
        //they sent their username instead 
        Auth::attempt(['username' => $username, 'password' => $password]);
    }
    
    //was any of those correct ?
    if ( Auth::check() ) {
        //send them where they are going 
        return redirect()->intended('dashboard');
    }
    
    //Nope, something wrong during authentication 
    return redirect()->back()->withErrors([
        'credentials' => 'Please, check your credentials'
    ]);
    

    This is just a sample. THere are countless various approaches you can take to accomplish the same.

    0 讨论(0)
  • 2021-02-04 01:08
    public function username()
    {
        //return ‘identity’;
        $login = request()->input('identity');
        $field = filter_var($login, FILTER_VALIDATE_EMAIL) ? 'email' : 'phone';
        request()->merge([$field => $login]);
    
        return $field;
    }
    
    
    protected function validateLogin(Request $request)
    {
        $messages = [
            'identity.required' => 'Email or username cannot be empty',
            'email.exists' => 'Email or username already registered',
            'phone.exists' => 'Phone No is already registered',
            'password.required' => 'Password cannot be empty',
        ];
    
        $request->validate([
            'identity' => 'required|string',
            'password' => 'required|string',
            'email' => 'string|exists:users',
            'phone' => 'numeric|exists:users',
        ], $messages);
    }
    

    https://dev.to/pramanadiputra/laravel-how-to-let-user-login-with-email-or-username-j2h

    0 讨论(0)
  • 2021-02-04 01:10

    Open your LoginController.php file.

    1. Add this reference

      use Illuminate\Http\Request;
      
    2. And override the credentials method

      protected function credentials(Request $request)
      {
          $field = filter_var($request->get($this->username()), FILTER_VALIDATE_EMAIL)
          ? 'email'
          : 'username';
      
          return [
              $field => $request->get($this->username()),
              'password' => $request->password,
          ];
      }
      

    Successfully tested in Laravel 5.7.11

    0 讨论(0)
  • 2021-02-04 01:16

    I think its even more simple, just override the method from AuthenticatesUsers traits, credentials method in your LoginController. Here I have implemented to login with either email or phone. You can change it to fit your needs.

    LoginController.php

    protected function credentials(Request $request)
    {
        if(is_numeric($request->get('email'))){
            return ['phone'=>$request->get('email'),'password'=>$request->get('password')];
        }
        return $request->only($this->username(), 'password');
    }
    
    0 讨论(0)
提交回复
热议问题