登录后设计不会重定向到根目录

Posted

技术标签:

【中文标题】登录后设计不会重定向到根目录【英文标题】:Devise does not redirect to root after sign in 【发布时间】:2021-12-24 18:17:48 【问题描述】:

我有一个使用 devise 的 rails 应用程序,rails 版本为 6.0.3.6。直到昨天,当用户登录时,他们被重定向到我在 routes.rb 文件中声明为 root 的用户仪表板(根据需要)。

然而,当我使用命名空间为 API 添加新路由时,情况发生了变化。现在用户在登录后被重定向到 API 的页面(/api/v1/circles),我希望他们再次被重定向到用户仪表板。

我尝试了一些我在网上找到的东西,比如

更改 routes.rb 中的路线顺序 以不同方式指定根路由或 在application_controller.rb 中定义一个redirect_after_sign_in 方法

我无法让它工作。为什么设计不再重定向到根目录?

routes.rb

Rails.application.routes.draw do
  devise_for :users
...
  root to: 'pages#user_dashboard'
...
resources :circles, only: [:create, :index, :show, :edit, :update, :destroy]
...
  namespace :api do
    namespace :v1 do
      get 'circles', to: 'circles#index'
    end
  end
end

铁路路线:

...
          root GET    /                            pages#user_dashboard
...
api_v1_circles GET    /api/v1/circles(.:format)    api/v1/circles#index
...

日志(使用 cmets)

# starting server
  => Booting Puma
     => Rails 6.0.3.6 application starting in development
     => Run `rails server --help` for more startup options
     Puma starting in single mode...
     * Version 4.3.7 (ruby 2.6.6-p146), codename: Mysterious Traveller
     * Min threads: 5, max threads: 5
     * Environment: development
     * Listening on tcp://127.0.0.1:3000
     * Listening on tcp://[::1]:3000
     Use Ctrl-C to stop

  # go to root
     Started GET "/" for ::1 at 2021-11-12 11:24:14 +0100
        (0.5ms)  SELECT "schema_migrations"."version" FROM "schema_migrations" ORDER BY "schema_migrations"."version" ASC
     Processing by PagesController#user_dashboard as html
     Completed 401 Unauthorized in 4ms (ActiveRecord: 0.0ms | Allocations: 1327)

  # need to sign in before showing root page, rendering login page
     Started GET "/users/sign_in" for ::1 at 2021-11-12 11:24:14 +0100
     Processing by Devise::SessionsController#new as HTML
       Rendering devise/sessions/new.html.erb within layouts/application
       Rendered devise/shared/_links.html.erb (Duration: 0.7ms | Allocations: 530)
       Rendered devise/sessions/new.html.erb within layouts/application (Duration: 30.2ms | Allocations: 15827)
      [Webpacker] Everything's up-to-date. Nothing to do
        Rendered shared/_navbar.html.erb (Duration: 0.8ms | Allocations: 737)
        Rendered shared/_flashes.html.erb (Duration: 0.3ms | Allocations: 192)
        Rendered shared/_footer.html.erb (Duration: 0.7ms | Allocations: 585)
      Completed 200 OK in 85ms (Views: 72.8ms | ActiveRecord: 3.3ms | Allocations: 76460)

  # wants to redirect to API page (why?)
      Started GET "/api/v1/circles" for ::1 at 2021-11-12 11:24:14 +0100
      Processing by Api::V1::CirclesController#index as */*
  5524 Completed 401 Unauthorized in 1ms (ActiveRecord: 0.0ms | Allocations: 388)

  # need to sign in before showing API page, renderin login page again
   Started GET "/users/sign_in" for ::1 at 2021-11-12 11:24:14 +0100
     Processing by Devise::SessionsController#new as */*
       Rendering devise/sessions/new.html.erb within layouts/application
       Rendered devise/shared/_links.html.erb (Duration: 0.2ms | Allocations: 77)
       Rendered devise/sessions/new.html.erb within layouts/application (Duration: 19.7ms | Allocations: 8254)
     [Webpacker] Everything's up-to-date. Nothing to do
       Rendered shared/_navbar.html.erb (Duration: 0.3ms | Allocations: 136)
       Rendered shared/_flashes.html.erb (Duration: 0.1ms | Allocations: 28)
       Rendered shared/_footer.html.erb (Duration: 0.5ms | Allocations: 456)
     Completed 200 OK in 32ms (Views: 30.4ms | ActiveRecord: 0.0ms | Allocations: 22541)

# user enters login data, sending the request
     Started POST "/users/sign_in" for ::1 at 2021-11-12 11:25:09 +0100
     Processing by Devise::SessionsController#create as HTML
       Parameters: "authenticity_token"=>"Ih9dc0OVE4HwzZcnaAUzmGvOmTyqP5GkYxr7HeebfFMAwYry355dfDvtaHiISZ3R5rC7+jrApL8W2Rjr+T2ADg==", "user"=>"email"=>"philipp@soulbottles.com", "password"=>"[FILTERED]", "remem
     ber_me"=>"0", "commit"=>"Log in"
       User Load (0.6ms)  SELECT "users".* FROM "users" WHERE "users"."email" = $1 ORDER BY "users"."id" ASC LIMIT $2  [["email", "philipp@soulbottles.com"], ["LIMIT", 1]]
     Redirected to http://localhost:3000/api/v1/circles
     Completed 302 Found in 254ms (ActiveRecord: 0.8ms | Allocations: 5381)

  # redirecting to API page after successful login (again, why?)
      Started GET "/api/v1/circles" for ::1 at 2021-11-12 11:25:10 +0100
      Processing by Api::V1::CirclesController#index as HTML
        User Load (0.2ms)  SELECT "users".* FROM "users" WHERE "users"."id" = $1 ORDER BY "users"."id" ASC LIMIT $2  [["id", 1], ["LIMIT", 1]]
        Circle Load (0.5ms)  SELECT "circles".* FROM "circles" ORDER BY "circles"."title" ASC
        ↳ app/controllers/api/v1/circles_controller.rb:7:in `index'
     Completed 200 OK in 16ms (Views: 4.9ms | ActiveRecord: 4.0ms | Allocations: 12664)

【问题讨论】:

【参考方案1】:

您是否尝试在 routes.rb 文件中添加以下内容?

authenticated :user do
  root to: "pages#user_dashboard", as: :authenticated_root
end

【讨论】:

我之前做过类似的事情,只是再次尝试了你的建议,没有任何改变。登录后我仍然会被重定向到 API 页面。【参考方案2】:

其他人向我指出了这个解决方案:

application_controller.rb 我添加了

def after_sign_in_path_for(resource)
  root_path
end

【讨论】:

以上是关于登录后设计不会重定向到根目录的主要内容,如果未能解决你的问题,请参考以下文章

注册设计后如何将用户重定向到登录表单?

用户登录后 Facebook 不会重定向到回调 URL

通过 JSF 表单成功登录后 Spring Security 不会重定向到登录页面

会话超时后,Spring 安全性不会重定向到上次请求的页面登录

用户登录后为什么gem设计重定向注册路径?

Spring登录后如何重定向到请求的页面?