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】axiosでResponceHeaderに任意の値を取得できるように設定する【devise-token-auth】

Last updated at Posted at 2021-06-30

概要

ReactとRailsAPIを使ったアプリで、devise-token-authをaxiosでアクセスしたときに取得できるheader情報を取得する方法について書きます。

devise-token-authでSignOutしたときに、SignInしたときの'access-token', 'uid','client'が必要になります。

しかし、CORSを使う場合defaultのままだと、上記値が取得できません。

なので、ResponceHeaderに'access-token', 'uid','client'の値が乗っかるように設定していきます。

実装方法

CORSに以下のコードを追加します。
expose: ['access-token', 'uid','client'],

実際に入れてみたのが以下

CORS.rb
Rails.application.config.middleware.insert_before 0, Rack::Cors do
  allow do
    origins 'http://localhost:3001'

    resource '*',
      headers: :any,
      expose: ['access-token', 'uid','client'],
      methods: [:get, :post, :put, :patch, :delete, :options, :head],
      credentials: true
  end
end

参考

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?