philicevic's avatar

Changing the authenticate-method in Laravel 5.5

Hey guys,

i wanted to add an E-Mail-Verification to my application, and for that i wanted to change the authentication. In the docs it says, that i can change it through adding a authenticate method in my LoginController.php:

https://laravel.com/docs/5.5/authentication#authenticating-users

This is not working for me, am i getting it wrong? Or did I miss something?

0 likes
10 replies
philicevic's avatar

It is basicly the default LoginController, i just added the authenticate method like this:

/**
     * Handle an authentication attempt.
     *
     * @return Response
     */
    public function authenticate()
    {
    dd(request()->user());
        if (Auth::attempt(['email' => $email, 'password' => $password, 'verified' => 1])) {
            // Authentication passed...
            return redirect()->intended('dashboard');
        }
    }
AlbertMulaki's avatar

php artisan cache:clear php artisan config:clear composer dump-autoload

vivekdhumal's avatar

@philicevic if you are using default login scaffold controller which comes by default with laravel, and if you see the trait is AuthenticatesUsers then you need to override public function login(Request $request) method to write your own authentication logic.

The authenticate method will not work.

1 like
philicevic's avatar

@vivekdhumal Thanks! But is there a way to see the default login method? Because i only want to add something, not change it completely. Otherwise i have to write the method on my own ^^

philicevic's avatar

@vivekdhumal in the trait there is an authenticate method, but it is empty.

I could take the content of the "login"-method and use it for my overwriting, but i don't know where to add the part which should be the authenticate method. By default it is not even used in any of this logic. The problem with the provided documentation is (as i said) that overwriting the authenticate method won't have any effect if it is not even used. ^^

vivekdhumal's avatar
Level 2

@philicevic if you saw the method is not authenticate it is authenticated


 /**
     * The user has been authenticated.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  mixed  $user
     * @return mixed
     */
    protected function authenticated(Request $request, $user)
    {
        //
    }

and if you read the comment, it clearly indicates that it is callback after your successful login

BUT if you want to add email verification then you need to override login method something like this in your LoginController.php.


 /**
     * Handle a login request to the application.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\RedirectResponse|\Illuminate\Http\Response|\Illuminate\Http\JsonResponse
     */
    public function login(Request $request)
    {
        if (Auth::attempt(['email' => request('email'), 'password' => request('password'), 'verified' => 1])) {
            // Authentication passed...
            return redirect()->intended('dashboard');
        }
    }

Note: as per the documentation the authenticate method is a manual method which you can use when you are not using AuthenticateUsers trait

2 likes

Please or to participate in this conversation.