onlinecode

Laravel 5.5 – Login With Only Mobile Number Using Laravel Custom Auth

Laravel 5.5 – Login With Only Mobile Number Using Laravel Custom Auth

In this post we will give you information about Laravel 5.5 – Login With Only Mobile Number Using Laravel Custom Auth. Hear we will give you detail about Laravel 5.5 – Login With Only Mobile Number Using Laravel Custom AuthAnd how to use it also give you demo for it if it is necessary.

Today, we are share with you how to modify laravel bydefault authentication and make it custom as per our requirement. in this totorials we are modify laravel authentication sysstem and make “how to login with only mobile number in laravel application using laravel custom authentication”. but you also can modify as per your requirement.

Laravel provide bydefault authentication system on users table’s email and password fields and it very helpfull for us. but sometime we want to change laravel authentication and make is as per our requirment. Ex. we want username or password, we want mobile number and password and sometime we want only mobile number for authentication and not extra needed. so, how to built laravel custom authentication? we are share here with very simple example. if you fallow all step then you can change laravel auth and set your custom auth as per your choice. it is very easy.

Create Laravel New Project

First, we are create one new fresh laravel project run by following command. if you already have laravel project and you want implement custom auth your exist laravel application. so, this step skeep and move on next.


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

After installation please setup your database in .env file

Create Laravel Auth

Next, Generate laravel bydefault auth run by following command


php artisan make:auth

Run Laravel Migration

Befor run laravel migration please add one extra mobile_no field in user table migration open it and add like that.


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

Next, then after run laravel migration using following command


php artisan migrate

Change in RedirectIfAuthenticated

Next, now we are need some changes in RedirectIfAuthenticated middleware. so, open your app/Http/Middleware/RedirectIfAuthenticated.php file and make change like that.


namespace AppHttpMiddleware;

use Closure;
use IlluminateSupportFacadesAuth;

class RedirectIfAuthenticated
{
    /**
     * Handle an incoming request.
     *
     * @param  IlluminateHttpRequest  $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);
    }
}

[ADDCODE]

Overwrite login() method

Next, we are need to overwrite login() method. so, open your app/Http/Controllers/Auth/LoginController.php file and make some change following way.


namespace AppHttpControllersAuth;

use AppHttpControllersController;
use IlluminateFoundationAuthAuthenticatesUsers;
use IlluminateHttpRequest;
use AppHttpRequests;
use AppUser;

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
        $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', 'Your mobile number not match in our system..!!');
            return back();
        }        
        
        // Set Auth Details
        Auth::login($user);
        
        // Redirect home page
        return redirect()->route('home');
    }
}

Change In Login Blade

Next, open your resources/views/auth/login.blade.php file and make changes this way.


@extends('layouts.app')

@section('content')
<div >
    <div >
        <div >
            <div >
                <div >Login</div>

                <div >
                    <form  method="POST" action="{{ route('login') }}">
                        {{ csrf_field() }}
                        <div >
                            <label for="mobile_no" >Enter Mobile No.</label>
                            <div >
                                <input id="mobile_no" type="text"  name="mobile_no" value="{{ old('mobile_no') }}" required autofocus>
                                @if ($errors->has('mobile_no'))
                                    <span >
                                        <strong>{{ $errors->first('mobile_no') }}</strong>
                                    </span>
                                @endif
                            </div>
                        </div>

                        <div >
                            <div >
                                <div >
                                    <label>
                                        <input type="checkbox" name="remember" {{ old('remember') ? 'checked' : '' }}> Remember Me
                                    </label>
                                </div>
                            </div>
                        </div>

                        <div >
                            <div >
                                <button type="submit" >
                                    Login
                                </button>

                                <a  href="{{ route('password.request') }}">
                                    Forgot Your Password?
                                </a>
                            </div>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
</div>
@endsection

Now we are ready to run our example so run bellow command ro quick run:


php artisan serve

Now you can open bellow URL on your browser:

	
http://localhost:8000/login

If you want to any problem then please write comment and also suggest for new topic for make tutorials in future. Thanks…

Hope this code and post will helped you for implement Laravel 5.5 – Login With Only Mobile Number Using Laravel Custom Auth. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve us. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs

For More Info See :: laravel And github

Exit mobile version