Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Handeling hasTooManyLoginAttempts in laravel?

Tags:

php

laravel

The user has surpassed their alloed maximum of login attempts will key this by the username and the IP address of the client making,I use trait AuthenticatesUsers pulled in. you look inside of mentioned trait, you will see another trait ThrottlesLogins pulled in.

Auth congfig:

'guards' => [
        'web' => [
            'driver' => 'session',
            'provider' => 'users',
        ],
        'api' => [
            'driver' => 'passport',
            'provider' => 'users',
        ],
        'admin-web' => [
            'driver' => 'session',
            'provider' => 'admins',
        ],
        'admin-api' => [
            'driver' => 'passport',
            'provider' => 'admins',
        ],
    ],

Authcontroller:

 class AuthController extends Controller
    {

     use ThrottlesLogins;

  public function login(Request $request)
        {
            $method = __FUNCTION__;

            //set validations
            $validator = Validator::make($request->all(), [
                'email' => 'required|string|email',
                'password' => 'required|string|min:6',
            ]);
            if ($validator->fails()) {
                return (new FailedServerResponse($this->controller, $method, $this->errorType['validation'], $validator->errors()))->show();
            }

            $admin = Admin::where('email', $request->email)->first();

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

            if (Auth::guard('admin-web')->attempt(['email' => $request->email, 'password' => $request->password, 'is_active' => 1], true)) {
                try {
                    $token = $admin->createToken('register admin')->accessToken;
                } catch (\Exception $e) {
                    return (new FailedServerResponse($this->controller, $method, $this->errorType['token']))->show();
                }

                return $token;
                //success and everything is ok
                $extra = ['token' => $token, 'is_register' => true];
                return (new UserResponse($admin->load('userActivities', 'addresses.city.province', 'wallets', 'userGalleries'), $actionName, $extra))->withPrimaryLayout();

            } else {
                return (new FailedServerResponse($this->controller, $method, $this->errorType['notFound']))->show();
            }
        }

     protected function hasTooManyLoginAttempts(Request $request)
        {
            $attempts = 2;
            $lockoutMinites = 10;
            return $this->limiter()->tooManyAttempts(
                $this->throttleKey($request), $attempts, $lockoutMinites
            );
        }

hasTooManyLoginAttempts not working. can you help me?

like image 262
Maryam Avatar asked Feb 04 '26 14:02

Maryam


1 Answers

Maybe the problem is that

$this->incrementLoginAttempts($request);

If the login attempt was unsuccessful we will increment the number of attempts to login and redirect the user back to the login form.

like image 73
hamed Avatar answered Feb 06 '26 02:02

hamed