Laravel Custom Login Example Using Mobile Number

Hello artisan,

In this tutorial, I will show you a Laravel authentication example using a mobile number.  Sometimes we need a Laravel login system using a mobile number instead of an email. So I am here to show you the Laravel 8 login example using a mobile number.

I will show you from scratch so that you can understand and use this code in your project. We can use Laravel default auth and we can customize that authentication to make it work with a mobile number instead of email.

So let's start our tutorial that how we can create a custom Laravel authentication system using a mobile number instead of an email.

 

Read also :  Email Verification after Registration in Laravel

 

Step 1: Install Laravel 

We need a fresh Laravel project to complete our tutorial. So download it using the below command.

composer create-project --prefer-dist laravel/laravel blog

 

Step 2: Add route

After downloading we have to set up our route.

routes\web.php

Route::namespace('Admin\Auth')->middleware('guest')->group(function () {
    Route::prefix('admin')->group(function () {
       Route::get('/login','LoginController@show_login_form')->name('login');
       Route::post('/login','LoginController@process_login');
    });
});

 

Step 3:  Create LoginController

Now we need a login controller to write our custom login code. So create it in this following path. 

App\Http\Controllers\Admin\Auth\LoginController.php

namespace App\Http\Controllers\Admin\Auth;

use App\Events\RoomCheckInCheckOutStatusRemove;
use App\Http\Controllers\Controller;
use App\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\App;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Crypt;

class LoginController extends Controller
{
    public function show_login_form(Request $request)
    {   
    	return view()->exists('admin.login') ? view('admin.login') : '';
    }

    public function process_login(Request $request)
    {
    	$check = 0;

        $request->validate([
            'mobile_number' => 'required',
            'password' => 'required'
        ]);

        $credentials = $request->except(['_token']);

        $users = User::select('mobile_number')->get();
        
        foreach ($users as $user) {
            if($user->mobile_number == $request->mobile_number)
            {
                $check = 1;
            }
        }
        if($check == 1)
        {
            if (auth()->attempt($credentials)) {

            return redirect()->route('checkroom');

            }else{
                $this->sendFailedLoginResponse('message', 'Your password not matched in our records');
                return redirect()->back();
            }
        }else
        {
           $this->sendFailedLoginResponse('message', 'Your phone number is not matched in our records');

           return redirect()->back();
        }

    }

    public function sendFailedLoginResponse(string $key = null, string $message = null)
    {
        session()->flash( $key, $message );
    }
}

 

Step 4: Create Blade File

Now in this step, we need to create a blade file for the login form. So create it.

resourses\views\admin\login.blade.php

 

Recommended: Create Custom Two Factor Authentication System in Laravel

 

Hope this tutorial will help you.

 

#laravel #laravel-8x #laravel-auth