Golang reCAPTCHA middleware for Fiber. This package takes care of getting the g-recaptcha-response
field from the sent form (or other request) data and validating the reCAPTCHA using Google's API.
This package should support at least these types of reCAPTCHA:
- reCAPTCHA V2, "I'm not a robot" Checkbox
- reCAPTCHA V2, Invisible reCAPTCHA badge
You can add this package to your code by two simple steps, which are:
- get the package by
go get github.com/jansvabik/fiber-recaptcha
- import the package manually by importing
github.com/jansvabik/fiber-recaptcha
or by calling the exportedMiddleware(c *fiber.Ctx) error
function from inside of the package
To use the middleware within your web server, you should add the Middleware
function to the queue of functions in your Fiber router, like in this example:
// pass your secret key to the package
recaptcha.SecretKey = "place-your-recaptcha-secret-key-here"
// create new fiber router
router := fiber.New()
// create an endpoint with the recaptcha middleware
router.Post("/endpoint", recaptcha.Middleware, endpoint.YourHandler)
When there is a request to this endpoint, for first it will go through this reCAPTCHA middleware which sends a request to the Google API and validates the whole request to be sent by human or by robot (depending to the Google response). Then, it sets up a local variable recaptchaSuccess
with boolean value which you can access and use in an if
statement.
// test the recaptcha success in your endpoint handler
if c.Locals("recaptchaSuccess") == false {
// code to do if the validation fails
}
// do other job if the validation succeeds
There are only two possible values which can appear in the local variable:
- boolean
true
if the validation succeeds - boolean
false
if the validation fails