Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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"=>"[email protected]", "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", "[email protected]"], ["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", "[email protected]"], ["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
like image 948
CodingSomething Avatar asked Jun 21 '20 13:06

CodingSomething


1 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
like image 142
Marcelo Soto Montes Avatar answered Oct 20 '22 00:10

Marcelo Soto Montes