I have used this how-to to redirect back to current page after sign in, sign out using Devise.
It is almost always working but one case is not.
I also have redirected to login page after logout. Without this redirection everything is working.
The case that is not working is: when I logout and login again. In the login page I print session[:previous url] and it is empty and after login, it redirects to the root path·
This behavior seems pretty strange to me... Any ideas?
UPDATE
This is the redirection that I mentioned. This way, session[:previous url] is empty in the login page
def after_sign_out_path_for(resource)
login_path
end
This is how it is working without the redirection. This way, session[:previous url] is working properly
def after_sign_out_path_for(resource)
session[:previous_url] || root_path
end
You need to override the Devise's method after_sign_out_path_for to achieve this. In your application controller add this method
def after_sign_out_path_for(resource_or_scope)
request.referrer
end
I know it's a very old question but many people (including me) still have this problem.
Devise resets the session during logout so as RSB wrote, you need to use request.referrer to redirect user back to where they came from.
In your specific case you may want to change the method to following
def after_sign_out_path_for(resource_or_scope)
session[:previous_url] = request.referrer
redirect_to login_path
end
However expecting the app to remember where user was when they log out and log back in is... quite exotic.
Related
I am working on a Ruby on Rails application and using Devise for user authentication. However, whenever I login or logout (from any page) I am always redirected to the home page "/". While what I'm trying to do is that when I login from any page (e.g. "/events") I am redirected back to this exact page ("/events" in this case) after login. And the same goes for logout.
I tried following this link. But the output of store_user_location is always "/" and also, the functions after_sign_out_path_for and after_sign_out_path_for are not overridden and my code for them was never reached.
I added after_sign_out_path_for also in application_controller as follows:
def after_sign_in_path(resource_or_scope)
puts stored_location_for(resource_or_scope) || super
end
How can I fix this problem?
Try using request.referrer to the sessions_controller.rb or application_controller.rb
You can access referer path using request.referer
Like below
protected
def after_sign_in_path_for(resource_or_scope)
stored_location_for(resource_or_scope) || request.referrer
end
def after_sign_out_path_for(resource_or_scope)
stored_location_for(resource_or_scope) || request.referrer
end
See the devise wiki
If I access http://example.com, I am first taken to the login page and after successful login, to the home page.
If I access http://example.com/myusers, after successful login, I want to be redirected to the current page and not the home page.
After reading a few online posts, I understood that in my application_controller.rb I can manipulate the after_sign_in_path_for method. I tested this by adding a specific url to this method and I always get redirected to that url after a successful login.
def after_sign_in_path_for(resource)
"http://example.com/users"
end
How can I make this method capture the URL that the user requested before signing in and redirect the user accordingly?
Devise comes with something like this out-of-the-box. As part of the failure scenario it stores the last request. This "How-to" should help you with the solution. Note that you need to call this method in your before_filter of the controller(s):
before_filter: authenticate_user!
https://github.com/plataformatec/devise/wiki/How-To:-Redirect-back-to-current-page-after-sign-in,-sign-out,-sign-up,-update#a-simpler-solution
You can store the requested URL in a session variable.
session[:return_to] = request.original_url
You can set the session variable inside of an before_action :authorize method in your application controller, this may make sense somewhere else depending on your authorization strategy.
def authorize
if current_user_authorized?
# Authorized...
else
# Record the requested page URL and redirect to the login page.
session[:return_to] = request.original_url
redirect_to login_url, error: "Not authorized."
end
end
To use it you can write:
def after_sign_in_path_for(resource)
session[:return_to] || root_url
end
If "root_path" takes you to the root page, what's the equivalent path to redirect you to the same page?
Here's the context: I'm using Devise for logging in users on a Rails 4.0 app, and my "custom_failure.rb" file currently redirects the user to the root page if login is unsuccessful.
Right now, a failed login will take the user to the root path. But the login form is on every page on the site, and if someone makes a failed login on page X, I want that user to stay on page X (or at least be redirected back to it).
Here's my "custom_failure.rb" file right now:
class CustomFailure < Devise::FailureApp
def redirect_url
root_path
end
.
.
end
What should I type in to replace "root_path"?
you can use request.referrer to redirect to the same page
you can use redirect_to :back
Method. After executing the controller, it will reload the same page.
Have you tried redirect_back_or_to signin_path ? It might help.
I'm trying to figure out how to get the user to be returned back to the original page after they sign up or sign in to the site through Devise.
For instance: User is viewing Listing A. User clicks "Sign Up", signs up successfully (no confirmation). But instead of being redirected back to the root, they're returned back to Listing A.
I used to do this with my homegrown authentication by storing the original URL in the session, then on successful login, redirect to the stored URL, or continue onto the default page if it is null.
Can I do the same thing with Devise, or is there a preferred way of achieving the same thing?
THE ANSWER
After wracking my brain for hours, then posting a SO question, it hits me almost immediately after submitting the question. Here's how I did it:
In my application controller, I sniff to see if there is a return_to params in the URL. If there is, I set that in the session.
Then, I overwrite after_sign_in_path_for in the application controller to redirect to the stored session value or root_path.
Here's (the pertinent parts of) my application_controller.rb:
class ApplicationController < ActionController::Base
before_filter :check_for_manual_return
def check_for_manual_return
session[:return_to] = params[:return_to] if !params[:return_to].nil?
end
def after_sign_in_path_for(resource)
return session[:return_to] || root_path
end
end
What's the best practice for redirecting the user, using Devise, back to the page she is currently on after she logs out?
The devise docs say to override the following (in your application controller):
def after_sign_out_path_for(resource_or_scope)
# logic here
end
Which is easy enough. However, I'm setting the previous page to be a session variable, like this:
session[:return_to] = request.fullpath
The problem is that when you sign out, the session is destroyed, and the top method occurs AFTER the session is destroyed, meaning you no longer have access to it. I'm thinking of putting it in a class variable or something similar, but wanted to see what SO thought.
If you are always using the page where the logout link was clicked you could use the referrer on the request.
def after_sign_out_path_for(resource_or_scope)
request.referrer
end
if you want to redirect user to sign in page after sign out write the below function in you application controller
def after_sign_out_path_for(resource_or_scope)
new_user_session_path
end