1
6

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 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
6
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
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?