Implement Google ReCaptcha v2 in Laravel
What is Google’s ReCaptcha?
Google ReCaptcha is a Turing test system to protect a website or app from fraud and abuse without creating friction with the program. ReCaptcha uses advanced risk analysis and adaptive challenges to keep malicious software from engaging in abusive activities on your websites and applications
By implementing ReCaptcha, websites are protected from unwanted robot scripting. Users can continue to use the website or application such as making purchases, viewing pages or creating accounts and users who are unable to complete challenges will not be able to continue and will be blocked.
Implement in Laravel
Well, in this experiment we will try to add Google ReCaptcha to a login form without using any package. I will show how to implement reCaptcha V2 with checkbox challenge.
If you notice that Google Recaptcha also have V3. The different between this version is just the human-interaction involvement. V2 requires the user to click the “I’m not a robot” checkbox and can serve the user an image recognition challenge while V3 runs in the background and generates a score based on a user’s behaviour. The higher the score, the more likely a user is human.
Here are the steps for V2:
- Create a reCaptcha V2 account
- Add keys to config file
- Implement reCaptcha scripting at Login page
- Create a Laravel Rule
- Add Validation rule to Login logic
- Testing and validation
Lets get started
Create a reCaptcha account
In this step we need to set google site key and secret key. If you don’t have it, we must first register a new site at this link before we can use Google ReCaptcha.
Skip this step if you already create one.