Ad

Trait Method HasTooManyLoginAttempts Has Not Been Applied

- 1 answer

I have upgraded my project from 5.2 to 5.3 in laravel. after that, I have the following error:-

Trait method hasTooManyLoginAttempts has not been applied, because there are collisions with other trait methods on App\Http\Controllers\Auth\AuthController in D:\xampp1\htdocs\clubmart_frontend\ app\Http\Controllers\Auth\AuthController.php on line 19

Following is the code of my AuthController:-

<?php

namespace App\Http\Controllers\Auth;

use App\Contracts\Repositories\UserRepositoryInterface;
use App\Http\Controllers\Controller;
use App\Models\User;
use App\Models\Voucher;
use App\Services\CartManager;
use Illuminate\Foundation\Auth\RegistersUsers;
use Illuminate\Foundation\Auth\AuthenticatesUsers;
use Validator;
use Illuminate\Http\Request;
use App\Events\UserWasRegistered;
use Event;
use Auth;

class AuthController extends Controller
{
    /*
    |--------------------------------------------------------------------------
    | Registration & Login Controller
    |--------------------------------------------------------------------------
    |
    | This controller handles the registration of new users, as well as the
    | authentication of existing users. By default, this controller uses
    | a simple trait to add these behaviours. Why don't you explore it?
    |
    */

    use AuthenticatesUsers, RegistersUsers;

    public $guard = 'web';

    /**
     * Where to redirect users after login / registration.
     *
     * @var string
     */
    protected $redirectTo = '/';

    /** @var UserRepositoryInterface */
    protected $userRepository;

    /**
     * Create a new authentication controller instance.
     *
     * @param UserRepositoryInterface $userRepository
     */
    public function __construct(UserRepositoryInterface $userRepository)
    {
        $this->userRepository = $userRepository;
        $this->middleware($this->guestMiddleware(), ['except' => 'logout']);
    }

    /**
     * Get a validator for an incoming registration request.
     *
     * @param  array $data
     *
     * @return Validator
     */
    protected function validator(array $data)
    {
        return Validator::make(
            $data,
            [
                'first_name' => 'required|max:255',
                'last_name'  => 'required|max:255',
                'email'      => 'required|email|max:255|unique:users',
                'password'   => 'required|min:6|confirmed',
            ]
        );
    }

    /**
     * Create a new user instance after a valid registration.
     *
     * @param  array $data
     *
     * @return User
     */
    protected function create(array $data)
    {
        return $this->userRepository->create(
            [
                'name'       => $data['email'],
                'first_name' => $data['first_name'],
                'last_name'  => $data['last_name'],
                'email'      => $data['email'],
                'password'   => $data['password'],
            ]
        );
    }
    protected function authenticated(Request $request, User $user)
    {
        if($user = Auth::user()) {
            if(!empty(app(CartManager::class)->getItems())) {
                return redirect()->intended('/cart');
            }
            else {
                return redirect()->intended('/');
            }
        }
        else {
            return redirect()->intended('/');
        }
    }
    //overwrite for add flash message to session
    public function postRegister(Request $request, User $user)
    {

        $validator = $this->validator($request->all());
        if ($validator->fails()) {
            $this->throwValidationException(
                $request, $validator
            );
        }

        //login the newly created user
        \Auth::login($this->create($request->all()));

        //fire up the send user email event
        $user_id =  $user->find(\Auth::user()->id);
        Event::fire(new UserWasRegistered($user_id));

        $request->session()->flash('alert-success', 'Registration successful!');
        if(!empty(app(CartManager::class)->getItems())) {
            return redirect()->intended('/cart');
        }
        else {
            return redirect($this->redirectPath());
        }
    }

    /**
     * Log the user out of the application.
     * overwrite for clear user from session
     * @return \Illuminate\Http\Response
     */
    public function logout(Request $request)
    {
        if($request->session()->has('user_id'))
            $request->session()->forget('user_id');

        \Auth::guard($this->getGuard())->logout();

        return redirect(property_exists($this, 'redirectAfterLogout') ? $this->redirectAfterLogout : '/');
    }


}

This is the code of Controller.php:-

<?php

namespace App\Http\Controllers;

use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Routing\Controller as BaseController;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
// use Illuminate\Foundation\Auth\Access\AuthorizesResources;

class Controller extends BaseController
{
    use AuthorizesRequests, DispatchesJobs, ValidatesRequests;
}

Any help will be appreciated. thanks in advance.

Edit

I have removed the following things as suggested:-

use Illuminate\Foundation\Auth\ThrottlesLogins;
use ThrottlesLogins;

But after that I have the following error:-

Trait method guard has not been applied, because there are collisions with other trait methods on App\Http\Controllers\Auth\AuthController in D:\xampp1\htdocs\clubmart_frontend\app\Http\Controllers\Auth\AuthController.php on line 19

Ad

Answer

This was the solution:-

I simply added the following method to the AuthController.php.

public function getLogin(){
        if (view()->exists('auth.authenticate')) {
            return view('auth.authenticate');
        }

        return view('auth.login');
    }

I have changed $this->guestMiddleware() to 'guest' in AuthController.php

public function __construct(UserRepositoryInterface $userRepository)
    {
        $this->userRepository = $userRepository;
        // $this->middleware($this->guestMiddleware(), ['except' => 'logout']);
        $this->middleware('guest', ['except' => 'logout']);
    }

I also have removed the following:-

use Illuminate\Foundation\Auth\RegistersUsers;
use RegistersUsers;

This solved the problem and me successfully able to log in and the project was updated from 5.2 to 5.3. thanks to all for the help.

Ad
source: stackoverflow.com
Ad