What is the correct way to set the WWW-Authenticate header in response to rails?

What is the correct way to set the WWW-Authenticate header in response to rails?

I am currently doing this:

headers["WWW-Authenticate"] = %(Basic realm="My Realm") render :json => {:message =>I18n.t("errors.messages.authorization_error")}, :status => :unauthorized 

I think there is an easier way to set this header using rails api. Without direct access to the header.

+4
source share
1 answer

There is nothing wrong with what you do, but rails has HTTP authentication baked inside. See this link: http://apidock.com/rails/ActionController/HttpAuthentication/Basic/ControllerMethods/authenticate_or_request_with_http_basic

+2
source

Source: https://habr.com/ru/post/1386906/


All Articles