How to Override Auth Login Method in Laravel 8

Installation of Laravel Application

  • Laravel Installer
  • By using composer

Laravel Installer

$ composer global require laravel/installer
$ laravel
$ laravel new blog

By using composer

$ composer create-project --prefer-dist laravel/laravel blog
$ php artisan serve

Create Database & Connect

CREATE DATABASE laravel_app;
DB_CONNECTION=mysql DB_HOST=127.0.0.1 DB_PORT=3306 DB_DATABASE=laravel_app DB_USERNAME=root DB_PASSWORD=root

Laravel 8 Authentication with Laravel UI

Laravel 8 Authentication Routes

$ php artisan route:list
GET|HEAD | login | login | App\Http\Controllers\Auth\LoginController@showLoginForm
POST | login | | App\Http\Controllers\Auth\LoginController@login

Override Login in Authentication

<?php namespace App\Http\Controllers\Auth; use App\Http\Controllers\Controller; use App\Providers\RouteServiceProvider; use Illuminate\Foundation\Auth\AuthenticatesUsers; use Illuminate\Http\Request; use Illuminate\Support\Facades\Auth; 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 = RouteServiceProvider::HOME; /** * Create a new controller instance. * * @return void */ public function __construct() { $this->middleware('guest')->except('logout'); } // Custom Login form public function showLoginForm() { return view("login"); } // Add mobile number to login public function login(Request $request) { $request->validate([ 'mobile' => 'required', 'password' => 'required', ]); $credentials = $request->only('mobile', 'password'); if (Auth::attempt($credentials)) { return redirect()->route('home'); } return redirect("login")->withSuccess('Oppes! You have entered invalid credentials'); } }

Custom Login Blade Template

@extends('layouts.app') @section('content') <div class="container"> <div class="row justify-content-center"> <div class="col-md-8"> <div class="card"> <div class="card-header">{{ __('Login') }}</div> <div class="card-body"> <form method="POST" action="{{ route('login') }}"> @csrf <div class="row mb-3"> <label for="mobile" class="col-md-4 col-form-label text-md-right">{{ __('Mobile Number') }}</label> <div class="col-md-6"> <input id="mobile" type="text" class="form-control @error('mobile') is-invalid @enderror" name="mobile" value="{{ old('mobile') }}" required autocomplete="mobile" autofocus> @error('mobile') <span class="invalid-feedback" role="alert"> <strong>{{ $message }}</strong> </span> @enderror </div> </div> <div class="row mb-3"> <label for="password" class="col-md-4 col-form-label text-md-right">{{ __('Password') }}</label> <div class="col-md-6"> <input id="password" type="password" class="form-control @error('password') is-invalid @enderror" name="password" required autocomplete="current-password"> @error('password') <span class="invalid-feedback" role="alert"> <strong>{{ $message }}</strong> </span> @enderror </div> </div> <div class="row mb-3"> <div class="col-md-6 offset-md-4"> <div class="form-check"> <input class="form-check-input" type="checkbox" name="remember" id="remember" {{ old('remember') ? 'checked' : '' }}> <label class="form-check-label" for="remember"> {{ __('Remember Me') }} </label> </div> </div> </div> <div class="row mb-0"> <div class="col-md-8 offset-md-4"> <button type="submit" class="btn btn-primary"> {{ __('Login') }} </button> @if (Route::has('password.request')) <a class="btn btn-link" href="{{ route('password.request') }}"> {{ __('Forgot Your Password?') }} </a> @endif </div> </div> </form> </div> </div> </div> </div> </div> @endsection

Application Testing

$ php artisan serve

--

--

--

Online Web Tutor is web development community found by Sanjay Kumar. It provides web development course for free worldwide by professionally trained instructors

Love podcasts or audiobooks? Learn on the go with our new app.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Online Web Tutor

Online Web Tutor

Online Web Tutor is web development community found by Sanjay Kumar. It provides web development course for free worldwide by professionally trained instructors

More from Medium

How To Get Last Record of Database Table in Laravel 8

Laravel Livewire | Click Event Example

Build Web App Mini Sosmed with Laravel 8 [Part 3]

How To Get Path From Laravel 8 Application Root