Google Recaptcha v0.1.1 GoogleRecaptcha View Source

Provides access interface to Google Recaptcha API.

GoogleRecaptcha need the API keys to work, which you can generate here.

After create the API keys, you should set the given env. variables:

export RECAPTCHA_SECRET_KEY="YOUR_SECRET_KEY"
export RECAPTCHA_PUBLIC_KEY="YOUR_PUBLIC_KEY"

Check the Google Recaptcha Docs for more information

Usage

Verifying captcha response:

# the captcha response is generated by the recaptcha widget.
# When the captcha is correct:
GoogleRecaptcha.verify("correct_captcha_response", "127.0.0.1")
:ok

GoogleRecaptcha.verify("wrong_capcha", "127.0.0.1")
{:error, :invalid_captcha}

Link to this section Summary

Functions

Helper function to check if the captcha is enabled

Public key to be used in google recaptcha widget

Check in the Recaptcha API if the given captcha response is correct

Link to this section Functions

Link to this function enabled?() View Source
enabled?() :: boolean

Helper function to check if the captcha is enabled.

Used for development purpouse, captcha is enabled by default. You can change it overriding the Recaptcha configuration:

# config/dev.exs
config :google_recaptcha, enabled: false

Public key to be used in google recaptcha widget.

You can set the public key simply exporting the variabble RECAPTCHA_PUBLIC_KEY:

export RECAPTCHA_PUBLIC_KEY="YOUR_PUBLIC_KEY"

or overriding the recaptcha config:

# config/dev.exs
config :google_recaptcha, public_key: "YOUR_PUBLIC_KEY"

For more information how to generate/display the recaptcha widget, check here.

Link to this function verify(captcha_response, remote_ip \\ nil) View Source
verify(String.t, String.t | nil) :: :ok | {:error, atom}

Check in the Recaptcha API if the given captcha response is correct.

Examples

# Doc how to generate the captcha widget: https://developers.google.com/recaptcha/docs/display#auto_render
captcha_response = params["g-recaptcha-response"]
verify(captcha_response, conn.remote_ip)
:ok

# Wrong captcha:
verify("wrong_capcha", "127.0.0.1")
{:error, :invalid_captcha}

# Invalid secret key:
verify("captcha_response", "127.0.0.1")
{:error, :invalid_secret}

# Invalid secret and public keys, or keys does not match.
verify("catpcha_response", "70.1.1.0")
{:error, :invalid_keys}