Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Devise after_sign_in_path_for works, but redirection doesn't happen

Devise doesn't redirect to the url provided by after_sign_in_path_for. It actually calls my custom after_sign_in_path_for instead. It calculates the url, that I expect it to calculate, but then redirection doesn't happen. It remains at the sign_in page, but the actual signing in does not take place.

I've been trying several versions of devise:

gem 'devise'

and

gem 'devise', :git => 'git://github.com/plataformatec/devise.git'

Same result.

My custom after_sign_in_path_for

def after_sign_in_path_for(resource)
    str = stored_location_for(resource) || stored_location || root_path
    debugger
    str
end

def stored_location
    session.delete(:return_to)
end

def store_location
    session[:return_to] = request.fullpath
end

Is being called, it gives proper url, gives str exactly as I expect it to give. Debugger stops at this point...

But after cont the page stays at sign_in, while however the signing in takes place.

I believe it is not my code issue. It might be a Devise issue. Can anyone, who has it working, share with me the exact version of Devise, which works with you.

like image 947
Dahan Avatar asked Nov 04 '22 13:11

Dahan


1 Answers

Hard to say without seeing your server logs. If after_sign_in_path_for is being called and generating the expected URL, then that leaves one possibility:

# app/controllers/devise/sessions_controller.rb
# POST /resource/sign_in
def create
  resource = warden.authenticate!(auth_options)
  set_flash_message(:notice, :signed_in) if is_navigational_format?
  sign_in(resource_name, resource)
  respond_with resource, :location => after_sign_in_path_for(resource)
end

The respond_with method will act sensibly according to the status of the resource. Since it's rendering the new template again, it points to some sort of validation error or other problem signing in.

Take a look at the server logs. Here is a successful sign in and subsequent redirect using the after_sign_in_path_for from my app:

Started POST "/admins/sign_in" for 127.0.0.1 at 2012-10-18 09:59:27 -0700
[INFO] [127.0.0.1] [2012-10-18 09:59:27 -0700] Processing by Devise::SessionsController#create as HTML
[INFO] [127.0.0.1] [2012-10-18 09:59:27 -0700]   Parameters: {"utf8"=>"✓", "authenticity_token"=>"DGjs2b3k8BIi62KWCn3u5kx7YxxyR03xkERcgH/ilr0=", "admin"=>{"email"=>"[email protected]", "password"=>"[FILTERED]", "remember_me"=>"0"}, "commit"=>"Sign in"}
[DEBUG] [127.0.0.1] [2012-10-18 09:59:27 -0700]   Admin Load (2.1ms)  SELECT "admins".* FROM "admins" WHERE "admins"."email" = '[email protected]' LIMIT 1
[DEBUG] [127.0.0.1] [2012-10-18 09:59:27 -0700]    (1.0ms)  BEGIN
[DEBUG] [127.0.0.1] [2012-10-18 09:59:27 -0700]    (0.9ms)  UPDATE "admins" SET "last_sign_in_at" = '2012-10-18 16:53:19.428068', "current_sign_in_at" = '2012-10-18 16:59:28.076180', "last_sign_in_ip" = '127.0.0.1', "sign_in_count" = 3, "updated_at" = '2012-10-18 16:59:28.078677' WHERE "admins"."id" = 1
[DEBUG] [127.0.0.1] [2012-10-18 09:59:27 -0700]    (9.3ms)  COMMIT
[INFO] [127.0.0.1] [2012-10-18 09:59:27 -0700] Redirected to http://localhost:3001/admin/users
[INFO] [127.0.0.1] [2012-10-18 09:59:27 -0700] Completed 302 Found in 97ms (ActiveRecord: 0.0ms)
[INFO] [127.0.0.1] [2012-10-18 09:59:28 -0700] 

Started GET "/admin/users" for 127.0.0.1 at 2012-10-18 09:59:28 -0700
[INFO] [127.0.0.1] [2012-10-18 09:59:28 -0700] Processing by UsersController#index as HTML
[DEBUG] [127.0.0.1] [2012-10-18 09:59:28 -0700]   Admin Load (1.2ms)  SELECT "admins".* FROM "admins" WHERE "admins"."id" = 1 LIMIT 1
[DEBUG] [127.0.0.1] [2012-10-18 09:59:28 -0700]   User Load (1.0ms)  SELECT "users".* FROM "users" 
[INFO] [127.0.0.1] [2012-10-18 09:59:28 -0700]   Rendered users/index.html.erb within layouts/application (1.0ms)
[INFO] [127.0.0.1] [2012-10-18 09:59:28 -0700] Completed 200 OK in 22ms (Views: 16.5ms | ActiveRecord: 2.2ms)

Are you seeing that redirect?

like image 88
jordanpg Avatar answered Nov 13 '22 12:11

jordanpg