Override Auth Login Method in Laravel 8 Step by Step

    By: Manu
    2 years ago

    Hi Devs, Lets see tutorial of How to override auth login function in laravel 8. I will create an example of laravel override login method. Article will give you simple example of laravel fortify custom login. About auth login function in laravel. Let's see bellow example laravel custom login function.

    Sometime we need to add some custom code or override login method in laravel, so here i will give you very simple example how to overwrite auth default login function in laravel application.

    Bellow is the given default route for login post method:

    Route::post('login', 'Auth\LoginController@loginUser');
    

    so, You can create new method loginUser into your LoginController and override auth method. let's add code as like bellow:


    <?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 LoginUser
    
         */
    
        public function loginUser(Request $request)
    
        {
    
           // Here inside function you can add custom logic.
    
            $request->validate([
    
                'email' => 'required',
    
                'password' => 'required',
    
            ]);
    
    
            $credentials = $request->only('email', 'password');
    
            if (Auth::attempt($credentials)) {
    
    
                return redirect()->route('home');
    
            }
    
            return redirect("login")->withSuccess('You have entered invalid creds');
    
        }
    }
    

    As route login pointing towards "loginUser" you use your function instead default provided.