Devise after_sign_in_path with custom sessions con

2019-06-09 04:40发布

问题:

How to define Devise after_sign_in_path to redirect to page user was before clicking to sign in? I've tried with this in the application_controller:

 13   def after_sign_in_path_for(resource_or_scope)
 14     sign_in_url = url_for(:action => 'new', :controller => 'my_sessions', :only_path => false, :protocol => 'http')
 15     if request.referer == sign_in_url
 16       super
 17     else
 18       stored_location_for(resource_or_scope) || request.referer || root_path
 19     end
 20   end

With this I only get redirected to root_path. It is worth mentioning that I have custom controller for Sessions callse MySessionsController. What am I doing wrong?

回答1:

You should not need to touch after_sign_in_path_for(resource_or_scope) since this is the default behaviour.

The return path is stored in session["user_return_to"] (if user is your devise scope), and is set by store_location! in FailureApp

this failure app is called each time warden authentication fails: see warden config calling Delegator.

is it possible you somehow bypass some of this?

this happens if you warden-authenticate without the bang. warden only bails via the failure app if you call warden.authenticate!, see warden wiki which does happen if you call devise's authenticate_user!

make sure you do use after_sign_in_path_for as redirect location in your session controller like here in case you have a custom one.