View Source Turnstile (phoenix_turnstile v1.0.0)

Use Cloudflare Turnstile in Phoenix apps

Link to this section 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.

Link to this section Functions

Link to this function

refresh(socket, id \\ nil)

View Source

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.

Link to this function

remove(socket, id \\ nil)

View Source

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.

Renders the Turnstile script tag.

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

Returns the configured secret key.

Returns the configured site key.

Link to this function

verify(map, remoteip \\ nil)

View Source

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.

Renders the Turnstile widget.

attributes

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.

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.