Laravel 7 Login With Email Or Username In One Field

Hey Artisan

In this tutorial i wll teach you how we can login using username or email with one field in laravel 7. It is a common problem of many web application to login with email or username using one field.

Laravel 7 login with username or email is the today's topic which i am going to discuss briefly. Hope you guys will enjoy it. So if you don't know how to laravel 7 login with username or email, don't worry, i am here to help you.

So let's start our today's tutorial laravel 7 login with username or email eample tutorial.

 

laravel-auth-login-with-email-or-username-in-one-field

First we need to make some changes to the login view particularly in E-Mail Address input field row. Go to login.blade.php file present in resources/views/auth directory. Now replace the first row in the form which the E-Mail Address input field with the contents below.

Read more : Laravel 6 : Activate Account after Email Verification using Laravel mail

resources/views/auth/login.blade.php

Don’t worry, let me explain you everything above. Basically we will send the input of the login with the name login. Laravel will accept it and it will find either the input is email or username. It will be merged in the request with the key email or username.

So, in the value attribute on input tag, we are setting it to the old('username') if it evaluates to true otherwise old('email').Let’s setup back-end logic for our login. Go to LoginController.php file present in app/Http/Controllers/Auth directory. Here’s the updated code for the Login Controller file.

app/Http/Auth/LoginController.php

 
namespace App\Http\Controllers\Auth;
 
use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\AuthenticatesUsers;
 
class LoginController extends Controller
{
   
    use AuthenticatesUsers;
 
    protected $redirectTo = '/home';
 
    protected $username;
 
    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;
    }
}

Let me explain every change made in the Login Controller. We have added a new username property which we will use to store whether the login field is email or username. This field will only have two values email or username. In the constructor, we are calling the findUsername method.

In this method we get the value of the login input. We check weather the field is a valid email, if yes we set $fieldType to email otherwise username.

Then we add the $fieldType with the input login in the request by using the merge method. Merge method basically merge a new input into the current request’s array. Then we return the $fieldType which could either be email or username.You can also use this code to do same task. 

app/Http/Auth/LoginController.php and override login() method:

public function login(Request $request)
{
    $this->validate($request, [
        'login'    => 'required',
        'password' => 'required',
    ]);

    $login_type = filter_var($request->input('login'), FILTER_VALIDATE_EMAIL ) 
        ? 'email' 
        : 'username';

    $request->merge([
        $login_type => $request->input('login')
    ]);

    if (Auth::attempt($request->only($login_type, 'password'))) {
        return redirect()->intended($this->redirectPath());
    }

    return redirect()->back()
        ->withInput()
        ->withErrors([
            'login' => 'These credentials do not match our records.',
        ]);
    } 

}

Hope it will help you. 

A web enthusiastic, self-motivated Full-Stack Web Developer from Dhaka, Bangladesh with experience in developing applications using JavaScript, Laravel & Wordpress specifically. Facebook Github Website