Login with Mobile Number using Laravel Custom Auth

Websolutionstuff | Jun-18-2021 | Categories : Laravel

In this tutorial i will show you how to login with mobile number in laravel using Laravel Custom Auth , laravel provide inbuilt laravel authentication for their user but if you want to create your laravel authentication customization then you need to remove and add some piece of code in laravel 8 for custom authentication.

Here,we are creating laravel login with mobile number using laravel custom auth.

Step 1 : Create New Project for Login with Mobile Number using Laravel Custom Auth Example.

Step 2 : Create Laravel Authentication using Command.

Step 3 : Edit User Migration and Run Migration.

Step 4 : Make Changes in RedirectIfAuthenticated.php File.

Step 5 : Make Changes in Controller.

Step 6 : Make Changes in Blade File.

 

Step 1 : Create New Project for Login with Mobile Number using Laravel Custom Auth Example
composer create-project --prefer-dist laravel/laravel custom-auth

 

Step 2 : Create Laravel Authentication using Command

Now, run below command to run default auth in laravel.

php artisan make:auth

 

Step 3 : Edit User Migration and Run Migration

After that add one mobileno filed to your user migration for mobile number authentication.

Schema::create('users', function (Blueprint $table) {
    $table->increments('id');
    $table->string('name');
    $table->string('email')->unique();
    $table->string('mobileno')->unique();
    $table->string('password');
    $table->rememberToken();
    $table->timestamps();
});

and run below command in your terminal.

php artisan migrate

 

Step 4 : Make Changes in RedirectIfAuthenticated.php File

In this step we need to make some change in RedirectIfAuthenticated middleware.So, open app/Http/Middleware/RedirectIfAuthenticated.php file and copy 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);
    }
}

 

 

Step 5 : Make Changes in Controller

Now, we will make changes in 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, [
            'mobileno' => 'required|regex:/[0-9]{10}/|digits:10',   
                      
        ]);

        // Get user record
        $user = User::where('mobileno', $request->get('mobileno'))->first();

        // Check Condition Mobile No. Found or Not
        if($request->get('mobileno') != $user->mobileno) {
            \Session::put('errors', 'Please Register First mobile number.!!');
            return back();
        }        
        
        // Set Auth Details
        \Auth::login($user);
        
        // Redirect home page
        return redirect()->route('home');
    }
}

 

Step 6 : Make Changes in Blade File

Now,open your 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('mobileno') ? ' has-error' : '' }}">
                            <label for="mobileno" class="col-md-4 control-label">Enter Mobile No.</label>
                            <div class="col-md-6">
                                <input id="mobileno" type="text" class="form-control" name="mobileno" value="{{ old('mobileno') }}" required autofocus>
                                @if ($errors->has('mobileno'))
                                    <span class="help-block">
                                        <strong>{{ $errors->first('mobileno') }}</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

 

Now, it is ready to run.

 

Recommended Post
Featured Post
How To Use OpenAI In Laravel 8/9
How To Use OpenAI In Laravel 8...

In this article, we will explore the integration of OpenAI into Laravel versions 8, 9, and 10. Our focus will be on unde...

Read More

Feb-06-2023

Laravel 8 User Roles and Permissions Without Package
Laravel 8 User Roles and Permi...

In this tutorial we will see laravel 8 user roles and permissions without package.Roles and permissions are an impo...

Read More

Sep-13-2021

How to Create Apexcharts Line Chart in Laravel 11
How to Create Apexcharts Line...

Hello developers! In this article, we'll see how to create apexcharts line chart in laravel 11. ApexCharts is a...

Read More

Apr-22-2024

How To Push Array Element In Node.js
How To Push Array Element In N...

Hello Dev, In this example we will see how to push array element in node.js example. I will share some example about&...

Read More

Oct-11-2021