{"title":"CSRF","description":"","section":"guides/controllers","version":"v1.5","path":"guides/controllers/csrf","canonical_url":"https://amberframework.org/docs/v1.5/guides/controllers/csrf","markdown_url":"https://amberframework.org/docs/v1.5/guides/controllers/csrf.md","inherited":true,"content_markdown":"# CSRF\n\nTo use CSRF, enable the pipe in your `routes.cr` by adding the following pipe to a pipeline.\n\n```\nplug Amber::Pipe::CSRF.new\n```\n\nThen, insert the `csrf_tag` helper in your forms.\n\n## How to use CSRF with AJAX\n\nSimply call the `csrf_tag` helper inside your controller and return it as part of a JSON object:\n\n```crystal\ndef my_action\n    {csrf: csrf_tag}.to_json\nend\n```\n\nIn your Javascript, after getting the JSON object back, refresh your CSRF tag with the one from the server.\n\n```javascript\n$(\"input[name*=_csrf]\").replaceWith(e['csrf']);\n```"}