How to Override Auth Login Method in Laravel 10

1. Create a new LoginController

Create a new LoginController in your app/Http/Controllers directory. This controller should extend the default Laravel Auth LoginController.

2. Override the login method

In your new LoginController, override the login method. This is where you can add your custom logic for authenticating users.

3. Update the routes

Update your routes file to point to your new LoginController.

4. Test it out

Test out your new login method by visiting the login page. If everything is working correctly, you should be able to log in with your custom logic.
[ad_1]

If you do not want to use the default login auth methods of Laravel web application. And if you want to create your own login method instead then this tutorial is for you. In this tutorial, you will learn how to override auth login method or function in Laravel 10 applications.

The default login method is Laravel Auth. You can easily override to default auth login method and create your own login auth method instead of this. For this, you have to make some changes. which are mentioned below.

How to Override Auth Login Method in Laravel 10

Use the following example for overriding auth login function in Laravel 10 applications:

First of all, open your web.php file and see the default login routes; as shown below:

Route::post('login', 'Auth\[email protected]');

Then open your LoginController and find the login method into it, And change it according to your requirement; as shown below:

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

Conclusion

That’s it. In this tutorial, you have learned how to override auth login method or function in Laravel 10 applications.

Recommended Laravel Tutorials

[ad_2]

Jaspreet Singh Ghuman

Jaspreet Singh Ghuman

Jassweb.com/

Passionate Professional Blogger, Freelancer, WordPress Enthusiast, Digital Marketer, Web Developer, Server Operator, Networking Expert. Empowering online presence with diverse skills.

jassweb logo

Jassweb always keeps its services up-to-date with the latest trends in the market, providing its customers all over the world with high-end and easily extensible internet, intranet, and extranet products.

GSTIN is 03EGRPS4248R1ZD.

Contact
Jassweb, Rai Chak, Punjab, India. 143518
Item added to cart.
0 items - 0.00