Search code examples
laravellaravel-authentication

Laravel - Custome Auth Throttlelogins


I have created a custom authentication and everything is working fine.
Now I am trying to add the Throttlelogins to prevent multiple incorrect login attempts. But The ThrottleLogins doesn't seem to load.

Q: What am I missing here? or am I doing something wrong?

The exception:

Method App\Http\Controllers\Auth\CustomersLoginController::hasTooManyLoginAttempts does not exist.

    <?php

    namespace App\Http\Controllers\Auth;

    use Illuminate\Http\Request;
    use App\Http\Controllers\Controller;
    use Illuminate\Foundation\Auth\AuthenticatesUsers;
    use Illuminate\Validation\ValidationException;
    use Illuminate\Foundation\Auth\ThrottlesLogins;

    use Auth;

    class CustomersLoginController extends Controller
    {    
        public function __construct()
        {
            $this->middleware('guest:customers');
        }

        public function ShowLoginForm()
        {
            return view('auth.customer-login');
        }

        public function login(Request $request)
        {
            $v = $request->validate([
                'email' => 'required|email',
                'password' => 'required',
            ]);

            if ($this->hasTooManyLoginAttempts($request)) {
                $this->fireLockoutEvent($request);

                return $this->sendLockoutResponse($request);
            }

            if(Auth::guard('customers')->attempt(['email'=>$request->email,'password'=>$request->password],$request->remember)){
                return redirect()->intended(route('customerdashboard'));
            };

            return $this->sendFailedLoginResponse($request);
        }

        protected function sendFailedLoginResponse(Request $request)
        {
            throw ValidationException::withMessages([
                $this->username() => [trans('auth.failed')],
            ]);
        }
        public function username()
        {
            return 'email';
        }


    }

Error Message Can someone please explain what am I mssing?


Solution

  • The error says you are missing a function: hasTooManyLoginAttempts

    In the function login you can see it's trying to call the function but it does not exist in your class. This is where it goes wrong.

    update

    In the AuthenticateUsers class, which you tried to copy, it's using ThrottlesLogins trait, which you are missing in your controller.

    Update your controller like so:

    class CustomersLoginController extends Controller
    {    
    
        use ThrottlesLogins;
    

    Another update

    You tried to import the Trait which Laravel uses in their own Login. However this will not work here's why:

    When you define a class, it can only have access to other classes within its namespaces. Your controller for instance is defined within the following namespace.

    namespace App\Http\Controllers\Auth;
    

    So to use other classes, you need to import them from their own namespaces so you can access them. e.g.:

    use Illuminate\Foundation\Auth\ThrottlesLogins;
    

    Now that you have imported the ThrottlesLogins, which is actually a trait, now inside the class you use it to expose all of the methods inside.