Login with either email or username with Fortify Laravel 8


I’m trying to allow users to login using either email or username along with their password.

I’ve added the following code inside boot() in FortifyServiceProvider.php :

Fortify::authenticateUsing(function (Request $request) {
        $user = User::where('email', $request->email)
            ->orWhere('username', $request->username)

        if ($user &&
            Hash::check($request->password, $user->password)) {
            return $user;

But now when users try to login by entering their username instead of email they get the following error message:

These credentials do not match our records.


I’ve forgotten to change the following line:

->orWhere('username', $request->username)


->orWhere('username', $request->email)

Because simply there is only one field in my login form that can hold either email or username. The name of this field is ’email’.

Answered By – Bu Saeed

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published