Laravel 5.3 E-Mail Confirmation

548 Views Asked by At

I need to implement the functionality of confirmation of the email address of a user before making him able to login to my platform.

I have successfully implemented the sending of a confirmation code at registration and save a string "confirmation_code" and and a Boolean "confirmed" of whether a user is confirmed or not in the database.

The verification itself is also already implemented.

Now the only problem I have is the checking at login if the user is confirmed or not. I read into the API of Laravel 5.3 but I could not figure out how to edit the login process in the way I like. I don't like changing too much of the original functionality of laravel, I just want to add a simple parameter to the authentication process during login. Cant be that hard can it?

3

There are 3 best solutions below

1
Blacktiger On BEST ANSWER

Okay, I have found a solution for my problem.

I have now overriden the attemptLogin(Request $request) function with my personal preferences. Not a great deal if you know what you have to look for. I think it should be somehow clarified in the laravel docs because authenticate itself doesnt work at all.

However, here is my code if anyone is interested:

protected function attemptLogin(Request $request){
    return $this->guard()->attempt([
        'email' => $request->email,
        'password' => $request->password,
        'confirmed' => 1
    ], $request->has('remember'));
}

The code above is written in my LoginController.php file.

The only thing I now have to find out is how to detect why a login has failed so I can tell the user whether his/her email/password combination was wrong or their account is not activated yet.

Thanks for the help!

6
msonowal On

Assuming you are using default LoginController Override the authenticate method in the controller

public function authenticate()
{
    if (Auth::attempt(['email' => $email, 'password' => $password, 'confirmed'=>1])) {
        // Authentication passed...
        return redirect()->intended('dashboard');
    }
}

add this to your controller

0
Victor Lundgren On

Laravel 5.3

I know this question is a little old but anyone else looking for a answer that let you keep all of the funcionalities of Laravel's Auth easily. I believe I have one.

If you take a look at Auth\AuthenticateUser you'll notice that the method login uses the method attemptLogin, which gets the array of credentials from the method credentials.

Instead of overriding the whole login flow, just override the credentials method, that way you can simply set the email confirmation without losing error messages and such.

For a E-mail confirmation validation, in your case, you can do sommething like:

protected function credentials(Request $request)
    {
        $credentials = $request->only($this->username(), 'password');
        $credentials['confirmed'] = 1;
        return $credentials;
    }