In this article, we will see how to login with a mobile number in laravel, Laravel provides inbuilt laravel authentication for their user but if you want to create custom authentication like login using a phone number, login using social accounts, login using email, login using OTP.
So, let's see laravel 8 custom authentication, laravel login with mobile number, laravel 8 login with phone number, laravel custom login example using mobile number, custom authentication laravel 8, custom auth laravel, custom login and registration in laravel 8, how to create custom login and registration page in laravel 8, login and register in laravel 8.
Step 1: Create New Project
Step 2: Create Laravel Authentication Using Laravel Auth Command
Step 3: Edit User Migration And Run Migration
Step 4: Make Changes in RedirectIfAuthenticated.php File
Step 5: Make Changes In the Controller
Step 6: Make Changes In Blade File
In this step, we will create a new application using the below command.
composer create-project --prefer-dist laravel/laravel custom-auth-laravel-8
Now, run the below command to run the default auth in laravel.
php artisan make:auth
After that add, the mobile_no field to your user's migration for mobile number authentication.
Schema::create('users', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->string('email')->unique();
$table->string('mobile_no')->unique();
$table->string('password');
$table->rememberToken();
$table->timestamps();
});
Migrate the table using the below command.
php artisan migrate
In this step, we need to make some changes in the RedirectIfAuthenticated middleware. So, open the app/Http/Middleware/RedirectIfAuthenticated.php file and the below code.
namespace App\Http\Middleware;
use Closure;
use Illuminate\Support\Facades\Auth;
class RedirectIfAuthenticated
{
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @param string|null $guard
* @return mixed
*/
public function handle($request, Closure $next, $guard = null)
{
if (Auth::check()) {
return redirect('/home');
}
return $next($request);
}
}
Now, we will make changes in the app/Http/Controllers/Auth/LoginController.php file.
namespace App\Http\Controllers\Auth;
use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\AuthenticatesUsers;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\User;
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');
$this->user = new User;
}
public function login(Request $request)
{
// Check validation - Note : you can change validation as per your requirements
$this->validate($request, [
'mobile_no' => 'required|regex:/[0-9]{10}/|digits:10',
]);
// Get user record
$user = User::where('mobile_no', $request->get('mobile_no'))->first();
// Check Condition Mobile No. Found or Not
if($request->get('mobile_no') != $user->mobile_no) {
\Session::put('errors', 'Please Register First mobile number.!!');
return back();
}
// Set Auth Details
\Auth::login($user);
// Redirect home page
return redirect()->route('home');
}
}
Now, open the resources/views/auth/login.blade.php file and make changes as below.
@extends('layouts.app')
@section('content')
<div class="container">
<div class="row">
<div class="col-md-8 col-md-offset-2">
<div class="panel panel-default">
<div class="panel-heading">Login</div>
<div class="panel-body">
<form class="form-horizontal" method="POST" action="{{ route('login') }}">
{{ csrf_field() }}
<div class="form-group{{ $errors->has('mobile_no') ? ' has-error' : '' }}">
<label for="mobile_no" class="col-md-4 control-label">Enter Mobile No.</label>
<div class="col-md-6">
<input id="mobile_no" type="text" class="form-control" name="mobile_no" value="{{ old('mobile_no') }}" required autofocus>
@if ($errors->has('mobile_no'))
<span class="help-block">
<strong>{{ $errors->first('mobile_no') }}</strong>
</span>
@endif
</div>
</div>
<div class="form-group">
<div class="col-md-6 col-md-offset-4">
<div class="checkbox">
<label>
<input type="checkbox" name="remember" {{ old('remember') ? 'checked' : '' }}> Remember Me
</label>
</div>
</div>
</div>
<div class="form-group">
<div class="col-md-8 col-md-offset-4">
<button type="submit" class="btn btn-primary">
Login
</button>
<a class="btn btn-link" href="{{ route('password.request') }}">
Forgot Your Password?
</a>
</div>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
@endsection
You might also like: