Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Redirect to specific URL after logging in

Is there a way in Devise 1.0, the library for Rails 2.3, to redirect to a specific URL and not root_url after logging in?

EDIT: forgot to mention it's Devise 1.0

like image 323
pupeno Avatar asked Sep 03 '10 13:09

pupeno


People also ask

How do I redirect a URL after login?

The most common ways to implement redirection logic after login are: using HTTP Referer header. saving the original request in the session. appending original URL to the redirected login URL.

How do I automatically redirect a URL?

To redirect from an HTML page, use the META Tag. With this, use the http-equiv attribute to provide an HTTP header for the value of the content attribute. The value of the content is the number of seconds; you want the page to redirect after.

How do I redirect back to original URL after successful login in laravel?

You may use Redirect::intended function. It will redirect the user to the URL they were trying to access before being caught by the authenticaton filter.

How do you get to the next page after login in HTML?

Approach: To redirect from an HTML page to another page, you can use the <meta> tag by specifying the particular link in the URL attribute. It is the client-side redirection, the browsers request the server to provide another page.


2 Answers

Chances are that your user is being redirected before after_sign_in_path is called. This happens if the user tries to go to a page that is protected by authentication directly. This will happen all the time if you have your root_path ('/') protected by authentication.

There's a discussion on google groups about this topic:

  • http://groups.google.com/group/plataformatec-devise/browse_thread/thread/454c7ea651601a6c/64f0adc3be8036d0?#64f0adc3be8036d0

The quick and dirty solution is to overwrite stored_location_for to always return nil like so:

class ApplicationController < ActionController::Base  
...

  private 
  def stored_location_for(resource_or_scope)
    nil
  end

  def after_sign_in_path_for(resource_or_scope)
    my_favorite_path
  end
end
like image 59
bowsersenior Avatar answered Oct 25 '22 21:10

bowsersenior


I think the after_sign_in_path_for method in Devise is what you're looking for.

Define that method in your ApplicationController and it will over-ride Devise's default implementation. This is what the documentation specifies to do.

Details here: http://rdoc.info/github/plataformatec/devise/master/Devise/Controllers/Helpers:after_sign_in_path_for

like image 28
Sidane Avatar answered Oct 25 '22 21:10

Sidane