lat4732's avatar
Level 12

Problem with adding google reCAPTCHA to the login and register pages

Hey!

I'm trying to add Google reCAPTCHA to the login and register pages. I'm using Fortify. I've read the Fortify documentation and ended up with this code inside the FortifyServiceProvider:

Fortify::authenticateUsing(function (Request $request) {
    return $request->validate(
        [
            'g-recaptcha-response' => 'required|recaptcha'
        ],
        [
            'g-recaptcha-response.required' => 'Please, pass the captcha.',
            'g-recaptcha-response.recaptcha' => 'Captcha verification failed!'
        ]
    );
});

but when I try to log in with correct credentials I get this error:

lluminate\Auth\SessionGuard::login(): Argument #1 ($user) must be of type Illuminate\Contracts\Auth\Authenticatable, array given ....

and when I remove the code from the FortifyServiceProvider the login works perfect. Any idea what is causing this issue? Also how can I define on which pages this code to be executed? Is Fortify::authenticateUsing() called only on the login and register pages?

0 likes
7 replies
lat4732's avatar
Level 12

@Sergiu17 Okay so I need to modify my code like this I guess:

Fortify::authenticateUsing(function (Request $request) {
            $validate = $request->validate(
                [
                    'g-recaptcha-response' => 'required|recaptcha'
                ],
                [
                    'g-recaptcha-response.required' => 'Please, pass the captcha.',
                    'g-recaptcha-response.recaptcha' => 'Captcha verification failed!'
                ]
            );

			if(!$validate) {
				return $validate;
			} else {
				return user instance
			}
});

But how do I actually return the user instance?

Sergiu17's avatar

@Laralex open the documentation

    Fortify::authenticateUsing(function (Request $request) {
        $user = User::where('email', $request->email)->first();

        if ($user &&
            Hash::check($request->password, $user->password)) {
            return $user;
        }

        return false;
    });
lat4732's avatar
Level 12

@Sergiu17 Didn't expected that. Nevermind, Final code:

            $validate = $request->validate(
                [
                    'g-recaptcha-response' => 'required|recaptcha'
                ],
                [
                    'g-recaptcha-response.required' => 'Please, pass the captcha.',
                    'g-recaptcha-response.recaptcha' => 'Captcha verification failed!'
                ]
            );

			if(!$validate) {
				return $validate;
			} else {
				$user = User::where('email', $request->email)->first();

                if ($user &&
                    Hash::check($request->password, $user->password)) {
                    return $user;
                }

                return false;
			}

But it is actually working only for the login page, it does not affect the register. How to I also execute this on the register page?

lat4732's avatar
Level 12

Still can't find how to add this on the register page. Any ideas?

Please or to participate in this conversation.