Redirect after login to page you logged in from (or signed up from)

1,595 views
Skip to first unread message

Alan McCann

unread,
Aug 12, 2010, 12:32:00 AM8/12/10
to Devise
I can see how to set a specific page for redirect after login/
registration but is there a way to keep track of the page a user
logged in from (or registered from) so that we can return them there
after.

For example: I am reading a forum and I see a discussion I want to
take part in. I click on the login button and after I log in, I am
back at that discussion and don't have to navigate my way back.

Thanks!

Alan McCann

unread,
Aug 14, 2010, 11:06:28 AM8/14/10
to Devise
OK - I've found the method to set the redirect path for a given model
(e.g.) after login.

I've tried to search for a way of saving the URL you are at before you
come to the signup page and save that with the user (session
variables).

Is that information captured by Devise somewhere. Looking at the
classes and methods, I can't find it (or can't understand that I did
find it!)

Very cool on adding Oauth2 into the core!

Alan

millisami

unread,
Aug 24, 2010, 2:02:19 AM8/24/10
to Devise
Hey Alan!
Did you find out the way to deal with this?
Coz I'm too in this same problem.
Or any help from Devise users on how to remember the last protected
page and redirect the user to that specific page??

Carlos Antonio da Silva

unread,
Aug 24, 2010, 7:11:35 AM8/24/10
to plataforma...@googlegroups.com
When you try to access a page in Devise that needs authentication, Devise saves this path and redirects you to the sign in page. After you sign in, Devise will redirect you back to that page.
If no page was saved, it will redirect you to what after_sign_in_path_for says (usually root_path).
If you want to save the page the user is navigating on, even if this page is not required access, you can put sth like this in your app controller:

def store_location
  if request.get? && request.format.html? && !request.xhr? && !devise_controller?
    session[:"scope.return_to"] = request.request_uri
  end
end

where "scope" in the session key, is your own scope (user for instance = user.return_to).
This key is for Rails 2.x only, if you're using Devise with Rails 3, use "scope_return_to" (underline instead of dot).

If that works for you, I'd ask you to create a page on the wiki, explaining this.
Thanks.
--
At.
Carlos A. da Silva

kshil

unread,
Sep 20, 2010, 11:35:11 PM9/20/10
to Devise
Try this.
redirect_to stored_location_for(:user) || root_path

Craig Demyanovich

unread,
Feb 28, 2011, 10:46:30 AM2/28/11
to plataforma...@googlegroups.com
While this has worked for me on Rails 2.3.10, I'm finding all future logins redirect to this page no matter from which page I'm logging in. I've tried to solve this by creating an initializer that instructs Warden to delete this value from the session, but I don't yet have it working, since there is no cookie with this name set on login.

In config/initializers/warden.rb:

# Stop tracking return URL when user logs out.
#
# Devise <= 1.0.x for Rails 2.3.x uses user.return_to
# Devise >= 1.1.x for Rails 3.x uses user_return_to
Warden::Manager.before_logout do |user,auth,opts|
  auth.response.delete_cookie "user.return_to"
end

Any other ideas?

Thanks,
Craig
Reply all
Reply to author
Forward
0 new messages