Home > Software design >  Devise does not redirect to root after sign in
Devise does not redirect to root after sign in

Time:11-13

I have a rails app which uses devise, rails version 6.0.3.6. Until yesterday, when a user signed in, they got redirected to a user dashboard (as desired) which I declared as root in my routes.rb file.

This changed however when I added a new route for an API using a namespace. Now the user gets redirected to the page of the API (which is /api/v1/circles) after login and I want them to get redirected to the user dashboard again.

I tried several things I found on the web like

  • changing the order of routes in routes.rb
  • specify the root route in different ways or
  • define a redirect_after_sign_in method in application_controller.rb

I couldn't make it work. Why would devise not redirect to root anymore?

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

rails routes:

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

logs (with comments)

# 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"=>"[email protected]", "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", "[email protected]"], ["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)

CodePudding user response:

Have you tried adding the following in your routes.rb file?

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

CodePudding user response:

Someone else pointed me to this solution:

in application_controller.rb I added

def after_sign_in_path_for(resource)
  root_path
end
  • Related