1

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?

Mehul Kuriya
  • 608
  • 5
  • 18
Blacktiger
  • 21
  • 5

3 Answers3

1

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!

Blacktiger
  • 21
  • 5
  • Okay I tried to figure out how to detect the reason of why a user couldnt login to give the user better feedback on it, but I couldnt find anyway to do it. – Blacktiger Dec 03 '16 at 21:00
1

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;
    }
Victor Lundgren
  • 115
  • 2
  • 11
-1

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

msonowal
  • 1,553
  • 3
  • 17
  • 36
  • I couldnt find any AuthController anywhere. I also found your solution on the laravel docs but couldnt figure out how to implement this. Laravel 5.3s make:auth line created a LoginController and RegisterController for me (some more stuff but none relating to this topic). No AuthController to be found anywhere :( – Blacktiger Dec 02 '16 at 12:40
  • then it is the LoginController add it inside of it – msonowal Dec 02 '16 at 12:43
  • I tried it, but then all variables ($email, $password) would be unknown to the Controller since no data is passed to the authenticate() function. – Blacktiger Dec 02 '16 at 12:48
  • you need to overrite – msonowal Dec 02 '16 at 13:32
  • I will try it when I'm home. But I don't know if I am just stupid or how do I get the $email and $password variables? There is no Request passed to authenticate or anything else I could retrieve the needed data from. Will report back in a few hours when I am home – Blacktiger Dec 02 '16 at 13:39
  • I tried adding the authenticate function to the LoginController right now. But as I expected it didnt work. I got several reasons for that. 1. There is no route going to the authenticate function (could try to call it login maybe) 2. I couldnt find any "authenticate" function in the framework, so I cant "override" it in anyway...since there is nothing to override. – Blacktiger Dec 02 '16 at 15:17