简体   繁体   English

如何阻止 devise 尝试重定向到 /users/sign_in 以获取 API 请求?

[英]How do I stop devise from trying to redirect to /users/sign_in for API requests?

On my react frontend, I have an API request which is being sent to my ruby on rails backend, via the axios library:在我的反应前端,我有一个 API 请求,该请求通过 axios 库发送到我在 rails 后端的 ruby:

        Axios({
            url: "/terms",
            headers: {
                'Authorization': token
            }
        }).then((response) => {
            console.log(response.request)
            setActiveSet(response.data)
            setActiveSetIndex(0)
        }).catch((error) => {
            console.log(error)
        })

When the above request is performed, I see the following chain of responses in my backend logs (ruby on rails):执行上述请求时,我在后端日志(ruby on rails)中看到以下响应链:

Started GET "/terms" for 192.168.1.119 at 2021-01-05 13:54:02 -0700
Cannot render console from 192.168.1.119! Allowed networks: 127.0.0.0/127.255.255.255, ::1
Processing by TermsController#index as HTML
Completed 401 Unauthorized in 0ms (ActiveRecord: 0.0ms | Allocations: 491)


Started GET "/users/sign_in" for 192.168.1.119 at 2021-01-05 13:54:02 -0700
Cannot render console from 192.168.1.119! Allowed networks: 127.0.0.0/127.255.255.255, ::1
Processing by Devise::SessionsController#new as JSON
Completed 200 OK in 1ms (Views: 0.2ms | ActiveRecord: 0.0ms | Allocations: 907)


Started GET "/terms" for 192.168.1.119 at 2021-01-05 13:54:02 -0700
Cannot render console from 192.168.1.119! Allowed networks: 127.0.0.0/127.255.255.255, ::1
Processing by TermsController#index as HTML
Completed 401 Unauthorized in 1ms (ActiveRecord: 0.0ms | Allocations: 491)


Started GET "/users/sign_in" for 192.168.1.119 at 2021-01-05 13:54:02 -0700
Cannot render console from 192.168.1.119! Allowed networks: 127.0.0.0/127.255.255.255, ::1
Processing by Devise::SessionsController#new as JSON
Completed 200 OK in 1ms (Views: 0.2ms | ActiveRecord: 0.0ms | Allocations: 905)

The .catch block is never called. .catch块永远不会被调用。 When I look at the responses, I see 2 successful 200 responses.当我查看响应时,我看到 2 个成功的 200 个响应。 The 401 responses don't show up on the frontend. 401 响应不会显示在前端。

I'm trying to capture the 401 status code, and then display a login form to the user via my react frontend.我正在尝试捕获 401 状态代码,然后通过我的反应前端向用户显示登录表单。 The backend (devise gem) is trying to redirect me to /users/sign_in, which is not what I want to happen.后端(设计 gem)试图将我重定向到 /users/sign_in,这不是我想要发生的。 I just want a 401 so that my react app can display its own login form, but for reasons unknown to me, axios is only showing the response having 200 statuses.我只想要一个 401,以便我的 react 应用程序可以显示自己的登录表单,但由于我不知道的原因,axios 仅显示具有 200 个状态的响应。

You can modify the responses when authentication fails by changing the failure app :您可以通过更改失败应用程序来修改身份验证失败时的响应:

module MyNamespace
  class FailureApp < Devise::FailureApp
    def respond
      request.format.json? ? api_response : super
    end

    private
    def api_response
      self.status = 401
      self.content_type = 'application/json'
      # optional - send a message in the response body
      # response.body = { error: i18n_message }.to_json
    end
  end
end
# add to config/initializers/devise.rb
config.warden do |manager|
  manager.failure_app = MyNamespace::FailureApp
end

The failure app is just a basic Rack application built onActionController::Metal that gets called when Warden throws (when user authentication fails).失败应用程序只是一个基于ActionController::Metal构建的基本 Rack 应用程序,当 Warden 抛出时(当用户身份验证失败时)被调用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将devise sign_in路径重定向到其他控制器和视图? 注意:这是在身份验证发生之前 - How do I redirect devise sign_in path to a different controller and view? Note:This is before the authentication happens Rails rspec尝试重定向到/ users / sign_in - Rails rspec trying to redirect to /users/sign_in 如何调用完全合格的Devise sign_in方法? - How do I call the fully qualified Devise sign_in method? 触发模式而不是重定向到/ users / sign_in路径,通过dev的Rails 4 - Trigger modal instead redirect to /users/sign_in path, Rails 4 with devise 限制访问或重定向设计路径用户/登录 - Restrict access or redirect devise path users/sign_in Rails 3 - 使用OmniAuth设计 - 登录后重定向转到/ users / sign_in - Rails 3 - Devise With OmniAuth - Redirect after signing in goes to /users/sign_in 如何将默认的Devise sign_in URL重定向到404页面? - How to redirect default Devise sign_in url to 404 page? 使用Devise,如何将根页面设置为如果用户注销则不重定向到“ sign_in”页面? - Using Devise, how can I set the root page to not redirect to the “sign_in” page if the user is logged out? 设计Rails应用程序重定向到用户/登录 - devise rails application redirecting to users/sign_in 设计+子域 - 重定向用户sign_in - Devise + Subdomain - Redirect user for sign_in
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM