Laravel 5.4 login with email or phone

Jagadesha NH
Aug 20, 2017 · 2 min read

In this post, I would like to show you how easy it is to let user login with either registered email or phone number.

Make sure you add phone filed to create_users_table migration. In India phone numbers are of 10 digits long, and also make sure phone field is unique.

Schema::create('users', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->string('email')->unique();
$table->string('phone',10)->unique();
$table->string('password');
$table->boolean('is_active')->default(1);
$table->rememberToken();
$table->timestamps();
});

To migrate type

php artisan migrate

Once the migration is done, edit RegisterController to validate phone and insert it into database.

<?php

namespace App\Http\Controllers\Auth;

use App\User;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Validator;
use Illuminate\Foundation\Auth\RegistersUsers;

class RegisterController extends Controller
{
use RegistersUsers;
protected $redirectTo = '/home';

public function __construct()
{
$this->middleware('guest');
}

protected function validator(array $data)
{
return Validator::make($data, [
'name' => 'required|string|max:255',
'email' => 'required|string|email|max:255|unique:users',
'phone' => 'required|digits:10|unique:users',
'password' => 'required|string|min:6|confirmed',
]);
}

protected function create(array $data)
{
return User::create([
'name' => $data['name'],
'email' => $data['email'],
'phone' => $data['phone'],
'password' => bcrypt($data['password']),
]);
}
}

In User model make phone as fillable

protected $fillable = [
'name', 'email', 'phone', 'password',
];

Now generate authentication scaffolding

php artisan make:auth

In LoginController, you see that it uses a trait called AuthenticatesUsers, Go to AuthenticatesUsers and copy the credentials function and check if the input is numeric or email, if it is numeric then return credentials array. Copy and override the method in LoginController

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');
}

After overriding your LoginController should look like

<?phpnamespace App\Http\Controllers\Auth;use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\AuthenticatesUsers;
class LoginController extends Controller
{
/*
|--------------------------------------------------------------------------
| Login Controller
|--------------------------------------------------------------------------
|
| This controller handles authenticating users for the application and
| redirecting them to your home screen. The controller uses a trait
| to conveniently provide its functionality to your applications.
|
*/
use AuthenticatesUsers;/**
* Where to redirect users after login.
*
* @var string
*/
protected $redirectTo = '/home';
/**
* Create a new controller instance.
*
* @return void
*/
public function __construct()
{
$this->middleware('guest')->except('logout');
}
/**
* Get the needed authorization credentials from the request.
*
* @param \Illuminate\Http\Request $request
* @return array
*/
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');
}
}

One last step don’t forget change input type from email to text in login.blade.php

<input id="email" type="text" class="form-control" name="email" value="{{ old('email') }}" required autofocus>

Now you can login with either email or phone :-)

Jagadesha NH

Written by

HI THERE! Welcome to my blog. I'm 'Jagadesha NH', passionate programmer, fun-loving web developer living in the 'Silicon Valley of India'.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade