Turnstile (phoenix_turnstile v1.2.0)

View Source

Use Cloudflare Turnstile in Phoenix. Check out the README to get started.

Summary

Functions

Refreshes the Turnstile widget in a LiveView.

Removes the Turnstile widget from a LiveView.

Renders the Turnstile script tag.

Returns the configured secret key.

Returns the configured site key.

Calls the Turnstile verify endpoint with a response token.

Renders the Turnstile widget.

Functions

refresh(socket, id \\ nil)

Refreshes the Turnstile widget in a LiveView.

Since the widget uses phx-update="ignore", this function can be used if the widget needs to be re-mounted in the DOM, such as when the verification fails. If there are multiple Turnstile widgets on the page and you only want to refresh one of them, pass a DOM ID as the second argument. Otherwise they will all be refreshed.

remove(socket, id \\ nil)

Removes the Turnstile widget from a LiveView.

Since the widget uses phx-update="ignore", this function can be used if the widget needs to be removed from the DOM. If there are multiple Turnstile widgets on the page and you only want to refresh one of them, pass a DOM ID as the second argument. Otherwise they will all be removed.

script(assigns)

Renders the Turnstile script tag.

Uses explicit rendering so it works with hooks. Additional attributes will be passed through to the script tag.

secret_key()

Returns the configured secret key.

site_key()

Returns the configured site key.

verify(map, remoteip \\ nil)

Calls the Turnstile verify endpoint with a response token.

Expects a map with string keys that contains a value for "cf-response-token" (see verification for more info). The remote IP can be passed for extra security when running the verification, but is optional. Returns {:ok, response} if the verification succeeded, or {:error, reason} if the verification failed.

widget(assigns)

Renders the Turnstile widget.

Attributes

  • :id - The ID of the element. Defaults to "cf-turnstile".
  • :class - The class name passed to the element. Defaults to nil.
  • :hook - The phx-hook used. Defaults to "Turnstile".
  • :sitekey - The Turnstile site key. Defaults to the :site_key config value.
  • :events - An atom list of the callback events to listen for for in the Live View. See events.

All other attributes will be passed through to the element as data-* attributes so the widget can be customized. See the Turnstile docs for a list of available attributes.