Documentation

CSRF

Browse documentation

Read this page as HTML, Markdown, or structured JSON—or open the published Markdown with an AI assistant. Gemini receives the prompt through your clipboard because its signed-out page does not reliably prefill URL text; paste when the new tab opens. External assistants need the public site URL.

CSRF

To use CSRF, enable the pipe in your routes.cr by adding the following pipe to a pipeline.

Crystal
plug Amber::Pipe::CSRF.new

Then, insert the csrf_tag helper in your forms.

How to use CSRF with AJAX

Simply call the csrf_tag helper inside your controller and return it as part of a JSON object:

Crystal
def my_action
    {csrf: csrf_tag}.to_json
end

In your Javascript, after getting the JSON object back, refresh your CSRF tag with the one from the server.

JavaScript
$("input[name*=_csrf]").replaceWith(e['csrf']);