LoginSignup
1
5

More than 5 years have passed since last update.

devise_token_authでunauthenticatedなエラー時のレスポンスをカスタマイズする

Last updated at Posted at 2018-05-16

devise_token_authのgemでアクセストークンなどをヘッダーに送る際、それがエラーになった場合のレスポンスは動的に、render_authenticate_errorメソッドによって作られている。

一番手っ取り早いのは該当するControllerでoverrideしてあげればとりあえずカスタマイズできる。

class Api::ApplicationController < ActionController::API
  include DeviseTokenAuth::Concerns::SetUserByToken

  def render_authenticate_error
    render json: {
        errors: [{
          title: I18n.t('devise.failure.unauthenticated')
        }]
    }, status: 401
  end
end
1
5
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
1
5