Search code examples
phpmysqllaravelauthenticationlaravel-5.3

username and email altogether login in laravel 5.3


I am currently doing a project. In this I want that the user login either his email or username but stuck at this As I cannot do this. I want that If a user provide his email or his username and password he can logged into his panel. I am using laravel 5.3 default function. I am doing this in laravel 5.1 but cannot do this in 5.3. Please help.


Solution

  • You can do that by overriding login() method in Auth\LoginController.php. Just check if credentials are valid and do this:

    return $this->sendLoginResponse($request);
    

    For example:

    if (Hash::check($request->password, $passwordFromDB) && ($request->username == $usernameFromDB || $request->email == $emailFromDB)) {
        return $this->sendLoginResponse($request);
    }
    
    return $this->sendFailedLoginResponse($request);