2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

【Rails】APIモードで開発を始めたけど、やっぱりcookie使いたい

Last updated at Posted at 2020-07-02

はじめに

自分用なので手順しか書かない

手順

controllerに追記

controllerでcookiesを呼び出す時の話
APIモードだと cookies がそもそも呼び出せない

ApplicationController か、cookiesを呼びだすcontrollerに以下を追加

include ActionController::Cookies

middleware追加

middlewareを追加しないとcookieの読み出しも書き込みも実際してくれない

application.rbに以下の2行を追加

config.middleware.use ActionDispatch::Cookies
config.middleware.use ActionDispatch::Session::CookieStore

おわり

ここまでやるとDeviseも自然にset-cookie返してくれるようになります
基本はDevise + jwtでやってたんですけど、API経由ではないルート追加することになり、header渡せず無事死亡しました

2
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?