How to override auth login function in laravel?

by aniya.jaskolski , in category: PHP Frameworks , 5 months ago

How to override auth login function in laravel?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

Member

by deron , 5 months ago

@aniya.jaskolski 

To override the auth login function in Laravel, you can follow these steps:

  1. Create a new controller that will extend the default AuthLoginController:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
namespace AppHttpControllersAuth;

use IlluminateFoundationAuthAuthenticatesUsers;

class CustomLoginController extends AppHttpControllersAuthLoginController
{
    use AuthenticatesUsers;

    // Override the login function here
    public function login(Request $request)
    {
        // Your custom login logic here
    }
}


  1. Update your routes/web.php file to point to the new controller:
1
Route::post('login', 'AuthCustomLoginController@login')->name('login');


  1. Replace the default AuthLoginController with the new CustomLoginController in your auth middleware configuration. You can do this in the AuthServiceProvider:
1
$this->app['router']->aliasMiddleware('auth', AppHttpControllersAuthCustomLoginController::class);


Now, you have successfully overridden the auth login function in Laravel and can customize it according to your needs.