Devise redirect to on successful login seems to get stuck on first login attempt

Viewed 1048

I'm developing a small application using Ruby on Rails and using Devise for authentication. I've two login systems setup and one is user and the other one is employee

When logging in using correct email and password, devise sends the correct redirect to path back but it gets stuck there. Nothing continues unless I refresh the page. But once I refresh it, it will happily go to the redirect path and even if after logging out in the same browser tab and then logging back in works. But if I close the tab and then load the application in a new tab, it doesn't work.

Following is the rails server output when logging in,

Started POST "/employees/sign_in" for ::1 at 2020-06-21 18:21:50 +0530
Processing by Devise::SessionsController#create as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"Pr0k+3+FfU72BcwTVRhWxQYBTD/zcv5+QBdWuovDRd4+yGxC1OBWmvAscWJbnog2vVTLpbPM2xOwVxGC3UsLRg==", "employee"=>{"email"=>"employee@example.com", "password"=>"[FILTERED]", "remember_me"=>"0"}, "commit"=>"Log in"}
  Employee Load (0.4ms)  SELECT  "employees".* FROM "employees" WHERE "employees"."email" = $1 ORDER BY "employees"."id" ASC LIMIT $2  [["email", "employee@example.com"], ["LIMIT", 1]]
  ↳ app/controllers/application_controller.rb:23
  CACHE Employee Load (0.0ms)  SELECT  "employees".* FROM "employees" WHERE "employees"."email" = $1 ORDER BY "employees"."id" ASC LIMIT $2  [["email", "employee@example.com"], ["LIMIT", 1]]
  ↳ app/controllers/application_controller.rb:23
Redirected to http://localhost:3000/employee/dashboard
Completed 302 Found in 818ms (ActiveRecord: 0.4ms)

I'm not sure why this happens and hitting a dead end when trying to find anything related to this error.

Following is the code for my after_sign_in_path function

  def after_sign_in_path_for(resource)
    set_flash_message! :alert, :warn_pwned if resource.respond_to?(:pwned?) && resource.pwned?
    set_root
    # super
  end

Here set_root function returns a String with the path for the redirect.

Version information

Ruby version - ruby 2.6.0p0 (2018-12-25 revision 66547) [x86_64-darwin19]
Rails version - Rails 5.2.4.2
Devise version - 4.7.1
Turbolinks - Yes
3 Answers

Try replacing your form_for with form_with in your sessions/new.html.erb. I have a similar problem (Rails 6) and it seems to be ok now.

<%= form_with(model: resource, scope: resource_name, url: session_path(resource_name)) do |f| %>

Additionally, I uncommented the Turbolinks section in config/initializers/devise.rb:

ActiveSupport.on_load(:devise_failure_app) do
  include Turbolinks::Controller
end

This sounds like an issue with your frontend (more specifically Turbolinks) rather than the backend. As your log shows, the redirect actually happens from an HTTP standpoint.

Since I haven't run into this issue myself (mainly because I haven't used Turbolinks much in more complex apps), I can only point you to a few resources that describe issues similar to yours, namely:

I hope one of these two helps.

If not, feel free to share some frontend code – like I said, I think the issue is on the frontend, not the backend.

The first thing I would try is to do a return redirect_to :

def after_sign_in_path_for(resource)
  set_flash_message! :alert, :warn_pwned if resource.respond_to?(:pwned?) && resource.pwned?
  return redirect_to set_root
  # super
end
Related