Laravel override authentication validation

Viewed 496

I'm trying to override the custom Laravel authentication by checking if the user confirmed his/her email address. At the same time I would like to log authentication attempts. The latter I'm trying to achieve with two listeners which I registered in EventServiceProvider.php

'Illuminate\Auth\Events\Login' => [
    'App\Listeners\LogSuccessfulLogin',
],
'Illuminate\Auth\Events\Failed' => [
    'App\Listeners\LogFailedAuthenticationAttempt',
],

In app/Http/Controllers/Auth/LoginController.php I have the following.

/**
 * Validate the user login request.
 * Override vendor method.
 *
 * @param  \Illuminate\Http\Request  $request
 * @return void
 */
protected function validateLogin(Request $request)
{
    $rules =  [
        'email' => [
            'required',
            'string',
            'exists:users,email,confirmed,1',
        ],
        'password' => [
            'required',
            'string',
        ],
    ];

    $messages = [
        'email.exists' => 'The combination of email address and password is unknown or the email address has not yet been validated.',
    ];

    $validator = Validator::make($request->all(), $rules, $messages);

    // $this->validate($request, $rules, $messages);

    if ($validator->fails()) {
        return redirect('nl')
                    ->withErrors($validator)
                    ->withInput();
    }
}

The problem is that this way it seems to ignore to check if the user confirmed it's email address. And auth:middleware is used. Now change this:

//$validator = Validator::make($request->all(), $rules, $messages);

$this->validate($request, $rules, $messages);

Now it's checked if the address is confirmed, but the Failed event is never called. How can I achieve both calling the Event and the custom rules?

0 Answers
Related