Skip to content

All topics  /  Laravel

How to Override Auth Login Method in Laravel 9?

Laravel ·

Teams applying “How to Override Auth Login Method in Laravel 9?” in a production project can use reduction force rif to record implementation, review and debugging time, then compare that effort with tests and shipped functionality instead of treating activity as performance by itself.

Before copying the example into a live application, confirm version-specific behaviour with the official Laravel website and test it against the project’s actual database and runtime.

How to Override Auth Login Method in Laravel 9?

Hello Friends,

In this tutorial we will demonstrate how to override auth login method in laravel 9. we will help you to give an example of laravel attempt login custom. I would like to show you laravel fortify custom login. This article will give you a simple example of auth login function in laravel. Let's get started with laravel custom login function.

Sometimes we need to overwrite or custom code for the login method, so here I will give you a very simple example of how to overwrite auth default login function in laravel app.

You can use this example with laravel 6, laravel 7, laravel 8, and laravel 9 versions.

You can see bellow default route for login post method:

web.php


<?php
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\LoginController;
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/
Route::post('login', 'Auth\LoginController@login');

so, Basically you can create new method login into your LoginController and override auth method. let's add code like below:

app/Http/Controllers/LoginController.php

<?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 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');
    }

  
     /**
     * Write code on Method
     *
     * @return response()
     */
    public function login(Request $request)
    {
        $request->validate([
            'email' => 'required',
            'password' => 'required',
        ]);

     
        $credentials = $request->only('email', 'password');
        if (Auth::attempt($credentials)) {

  
            return redirect()->route('home');
        }

    
        return redirect("login")->withSuccess('Oppes! You have entered invalid credentials');
    }
}

i hope it can help you....