ItSolutionStuff.com

How to Override Auth Login Method in Laravel 8?

By Hardik Savani • November 5, 2023
Laravel

Now, let's see tutorial of How to override auth login function in laravel 8. We will look at example of laravel override login method. This article will give you simple example of laravel fortify custom login. I’m going to show you about auth login function in laravel. Let's see bellow example laravel custom login function.

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

You can see bellow default route for login post method:

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 as like bellow:

app/Http/Controllers/Auth/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....

Hardik Savani

Hardik Savani

I'm a full-stack developer, entrepreneur, and founder of ItSolutionStuff.com. Passionate about PHP, Laravel, JavaScript, and helping developers grow.

šŸ“ŗ Subscribe on YouTube

We Are Recommending You

ā˜…

How to Add Two Factor Authentication with SMS in Laravel?

Read Now →
ā˜…

Laravel 8 Firebase Mobile Number (OTP) Authentication Tutorial

Read Now →
ā˜…

Laravel Sanctum SPA API Authentication Example

Read Now →
ā˜…

Laravel 8 Sanctum API Authentication Tutorial

Read Now →
ā˜…

Laravel Authentication with Breeze Tutorial

Read Now →
ā˜…

Laravel 8 Multi Auth (Authentication) Tutorial

Read Now →
ā˜…

Laravel 8 REST API with Passport Authentication Tutorial

Read Now →
ā˜…

Laravel 8 Auth with Inertia JS Jetstream Tutorial

Read Now →
ā˜…

Laravel 8 Auth with Livewire Jetstream Tutorial

Read Now →
ā˜…

Laravel 8 Authentication using Jetstream Example

Read Now →
ā˜…

How to use Laravel 8/7 Authorization using Gates?

Read Now →