amber logo
  • Blog
  • Media
  • Guides
  • Github
  • Discord
  • Star

CSRF

CSRF

Amber Framework Documentation 1.4.1 (Stable)

1.4.1 (Stable)
Documentation
  • Introduction
  • Getting Started
  • Guides
    • Overview
    • Installation
    • Create New App
    • Directory Structure
    • Configuration
    • Docker
    • Controllers
      • Sessions
      • Request & Response Objects
      • Halt!
      • Respond With
      • Params
      • Cookies
      • Filters
      • Flash
      • Redirection
      • CSRF
    • SSL
    • Views
      • Basic View Helpers
      • Form Builder
    • Models
      • Granite
      • Jennifer
    • Routing
      • Pipelines
      • Routes
    • Websockets
      • Channels
      • Sockets
      • JavaScript Client
    • Mailers
    • Testing
      • System Tests
  • Deployment
    • Manual Deploy
    • Digital Ocean
    • Heroku
    • Dokku
  • CLI
    • New
    • Recipes
    • Plugins
    • Generate
    • Database
    • Watch
    • Routes
    • Exec
    • Encrypt
  • Examples
    • Amber Auth
    • Crystal Debug
    • Minimal Configuration
    • JSON API Full CRUD
  • Cookbook
    • From Scratch
    • Hello World
    • CORS
    • File Download
    • File Upload
    • Cookies
    • Authenticate
    • JSON API
    • JSON Mapping
    • WebSocket Chat
  • Troubleshooting
  • In Production
  • Contributing
  • Code of Conduct
Docs 1.4.1 (Stable) Guides Controllers CSRF CSRF
Copy as Markdown View as Markdown
Open in Claude

CSRF

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

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:

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.

$("input[name*=_csrf]").replaceWith(e['csrf']);
Previous
Redirection
Next
Granite
amber logo

Amber aims to deliver developer happiness, productivity and bare metal performance. It offers a familiar set of tools like generators, database management, repl, deployment tools, docker support and production tools like encrypted secrets.


Copyright © 2025 Amber Framework . All rights reserved.

Amber Framework
Twitter Documentation Source Code Discord
Crystal Language
Crystal Documentation Crystal Standard Library Crystal Community Source Code Try Crystal Online