Ruby, Web Development

Rails 5 – API Only – Enable Cookies and Sessions

So you’ve been trying a lot of trial and errors just to enable cookie based session on a API only rails app? Are you trying to customize the cookie expiration but it simply defaulting to default session only cookies? The solution is quite simply though and here it is.

In your config/application.rb, you set the cookie parameters upon calling the middleware.

# ... require and other codes here

module AppNameHere
  class Application < Rails::Application
    config.api_only = true

    config.middleware.use ActionDispatch::Cookies
    config.middleware.use ActionDispatch::Session::CookieStore, key: '_coookie_name', expire_after: 30.days
  end
end

1 thought on “Rails 5 – API Only – Enable Cookies and Sessions”

Leave a reply

Your email address will not be published. Required fields are marked *