[RUBY] [Rails] Use cookies in API mode

environment

Premise

--I'm using Rails in API mode ([English version] of the ← document (https://guides.rubyonrails.org/api_app.html)).

Or

--ʻActionController :: API` is inherited and the controller for API is used.

Problem / what you want to do

As it is, cookies cannot be accessed, so we will make it accessible.

manner

Here, the latter is assumed (the controller for API is used by inheriting ʻActionController :: API`).

Include ActionController :: Cookies

ʻInclude ActionController :: Cookies on the base controller that inherits ʻActionController :: API, or on the controller that you actually want to access cookies.

In other words

  class YourApiBaseController < ActionController::API
    include ActionController::Cookies

Or

  class YourApiController < YourApiBaseController
    include ActionController::Cookies

Enable ActionDispatch :: Cookies in your application

config/application.rb


module YourApi
  class Application < Rails::Application
    config.middleware.use ActionDispatch::Cookies

Other

It is necessary to set session_store and credentials.yml.enc (secrets.yml * in the case of the old version) separately.

config/initializers/session_store.rb


Rails.application.config.session_store :cookie_store, key: 'your-cookie-key-comes-here'

Recommended Posts

[Rails] Use cookies in API mode
Use images in Rails
Nuxt.js × Create an application in Rails API mode
Use multiple checkboxes in Rails6!
Rails API
Launch the Rails app locally in production mode (API Server)
How to return Rails API mode to Rails
How to use custom helpers in rails
How to use MySQL in Rails tutorial
How to use credentials.yml.enc introduced in Rails 5.2
Group_by in Rails
[Rails] Use jQuery
[Rails] How to use select boxes in Ransack
Rails6 [API mode] + MySQL5.7 environment construction with Docker
How to use JQuery in js.erb of Rails6
Make Rails API mode support cookie (session) authentication
[Rails] How to use PostgreSQL in Vagrant environment
Use pagy for pagination in your Rails app.
Use JavaFX Clipboard API
Model association in Rails
Use Extend (Concerns) in Rails to standardize Controller processing.
Adding columns in Rails
Use java.time in Jackson
Disable turbolinks in Rails
Rails (API mode) x React x TypeScript simple Todo app
I want to use a little icon in Rails
Implement user follow function in Rails (I use Ajax) ②
CSRF measures in Rails
Use Interceptor in Spring
Use OpenCV in Java
^, $ in Rails regular expression
Zabbix API in Java
Implement user follow function in Rails (I use Ajax) ①
Use MouseListener in Processing
Use PostgreSQL in Scala
Understand migration in rails
Get cookies in Spring
Use PreparedStatement in Java
Split routes.rb in Rails6
Beginners use ubuntu in windows to prepare rails environment
Implement markdown in Rails
How to call and use API in Java (Spring Boot)
Display API definition in Swagger UI using Docker + Rails6 + apipie
Use your own classes in the lib directory with Rails6
Get UserAgent in [Rails] controller
How to use Chain API
Implement follow function in Rails
[Rails] How to use enum
[RSpec] Let's use FactoryBot [Rails]
[Rails] How to use enum
Use Bulk API with RestHighLevelClient
Implement LTI authentication in Rails
Use Face API from Ruby
API creation with Rails + GraphQL
Use ruby variables in javascript.
Java Stream API in 5 minutes
Error in rails db: migrate
How to use rails join
Gem often used in Rails
Display Flash messages in Rails
Use multiple databases with Rails 6.0