Google Recaptcha API Client for Elixir.
- Add
google_recaptcha
to your list of dependencies inmix.exs
:
def deps do
[{:google_recaptcha, "~> 0.2.0"}]
end
- Run
mix deps.get
to install it.
API keys is needed to get the client working, you can generate here
And set the keys in your project configuration file:
config :google_recaptcha,
api_url: "https://www.google.com/recaptcha/api/siteverify",
public_key: "YOUR_PUBLIC_KEY",
secret_key: "YOUR_SECRET_KEY",
enabled: true # You may set false for development
Check if the captcha is valid, returns :ok
when it is valid and {:error, :error_type}
when something goes wrong:
# When captcha is valid
iex> GoogleRecaptcha.verify(captcha_response, client_ip_addres)
...> :ok
iex> GoogleRecaptcha.verify(captcha_response, client_ip_addres)
...> {:error, :invalid_captcha}
Check if the captcha is valid(check if the recaptcha is enabled), in this case returns bolean(any error will be return as false
):
# When captcha is valid
iex> GoogleRecaptcha.valid?(captcha_response, client_ip_addres)
...> true
# When captcha is disabled
iex> GoogleRecaptcha.valid?(captcha_response, client_ip_addres)
...> true
# Wrong captcha response
iex> GoogleRecaptcha.valid?(wrong_captcha_response, client_ip_addres)
...> false