Changing route after sign out with devise - ruby-on-rails

I am creating a website using rails and I have started using the devise gem. I have added a sign out link to my homepage which works, but I want to route the user back to the login page after they sign out. At this moment after the user signs in they are offered a sign out link which signs them out but they remain at the page. How do I make it so that they are sent back to the login page after they sign out? Thanks.

You can change the redirect path in ApplicationController using Devise's after_sign_out_path_for method...
class ApplicationController < ActionController::Base
private
def after_sign_out_path_for(resource_or_scope)
new_user_session_path
end
end

Related

ActiveAdmin Gem: Redirect user to login page if not logged in

I am working with active admin gem and it is working great.
But a minor that I'm facing is whenever I manually enter a url such as:
https://www.example.com/admin/users/5
and if the admin is not logged in then the above url page shows:
{"errors":["You need to sign in or sign up before continuing."]}
which is fine, but I want a redirection to the login page and if admin log in from the page, it should show up the desired page pointing to https://www.example.com/admin/users/5
I'm not able to figure out the redirection procedure for ActiveAdmin.
In config/initializers/active_admin.rb you have such config
config.authentication_method = :authenticate_admin_user!
then create a method named authenticate_admin_user! in the ApplicationController
def authenticate_admin_user!
#check for current_user if not then redirect to login
end

Get target path before authentication

I'm using Devise for user authentication in a rails application, and so certain routes are wrapped in a authenticate tag to allow access only to users who are logged in.
authenticate :user do
post '/user/orders/new', to: 'user/orders#new', as: :new_user_order
end
For example, when a user tries to create a new order when they're not logged in, they're redirected to a sign in page.
What I want is for the user to be directed to the new order page after logging in.
Currently, we're using request.referrer which redirects the user back to the page they were on before trying to create a new order. I can't seem to figure out a way of getting the path they were targeting before being sent to sign in.
Anybody know how I could do this?
Thanks
You need to provide a before filter for user authentication in the controller and not mention it in routes . Just put the following code in the controller and it should redirect to sign-up if the user isn't logged in -
before_filter :authenticate_user
You need to override Devise's after_sign_in_path_for(resource_or_scope) method, you can do it in application controller
def after_sign_in_path_for(resource_or_scope)
# new_order_path
end
Hope that helped!
You can persist the last url - in session[:bookmark] and by doing something like this -
prepend_before_filter :set_bookmark
def set_bookmark
session[:bookmark] = request.url
end
And within SessionsController, you can use value of session[:bookmark] if it exists or after_sign_in_path_for

Redirect after signing in after being redirected from users/edit in devise

If unauthenticated users attempt to access users/edit , devise redirects them to users/sign_in. This is good. However, after successful signing in, I wish the user to be redirected back to users/edit. How is this possible.
Have you overriden the after_sign_in_path_for method? Check out the wiki about How To: redirect to a specific page on successful sign in
Anyway, AFAIK, if you haven't overriden this method, the default behaviour is redirect user to the last stored location, 'users/edit' in that case. Check out the content of stored_location_for on your application before user is signed in.
I interpret the question as how do I redirect back after sign in, where the answer would be thus:
https://github.com/plataformatec/devise/wiki/How-To:-redirect-to-a-specific-page-on-successful-sign-in
class ApplicationController < ActionController::Base
def after_sign_in_path_for(resource)
request.env['omniauth.origin'] || stored_location_for(resource) || root_path
end
end

Disable auto sign in for users right after a registration

I use Devise gem. It's not configured to be :confirmable, however I want an user not to log in automatically right after a registration process. For some reason, now they are logged in automatically which exactly the opposite of what I want.
So how do I do that?
https://github.com/plataformatec/devise/wiki/How-To:-Redirect-to-a-specific-page-on-successful-sign-up-(registration)
Basically, follow the above steps. In your after_sign_up_path_for method, you can call
sign_out resource
then redirect them to whatever page you want them to go to (perhaps root, or the login page).
The user is logged in via the sign_up method called by devise; it's standard procedure -- many websites log in users immediately after they sign up. Most that don't require confirmation first, so your use case it somewhat atypical. Still, doing the method I described above should do what you want, without having to implement confirmable or doing any membership approvals.
Signing in the user after sign up is achieved by the sign_up method in Devise::RegistrationsController that gets called by the [create][1] action. You can simply override this method with an empty one to prevent signing in. That worked for me!
In app/controllers/users/registrations_controller.rb:
class Users::RegistrationsController < Devise::RegistrationsController
def sign_up(resource_name, resource)
end
end
Note: replace users with your table name.
Extra:
If you allow only admins to add new users , or perhaps other admins, and use the create action to add them, this previous method will also give you the benefit of not having to sign out. You can customize what page to be redirected to after signup by overriding the after_sign_up_path_for method in app/controllers/application_controller.rb:
class ApplicationController < ActionController::Base
def after_sign_up_path_for(user)
'/users/show' # replace with the path you want
end

Disable devise sign_up after logging in

I'm using devise as registration engine in my rails 3.1 app. How can i prevent users from accessing some pages when they are logged in? I need to disable devise registration and some custom pages? Is there any way to implement this?
Devise automatically handles redirecting logged in users away from the sign in and sign up actions. If you would like to do this for other pages you would need to use controller before filters or an authorization solution such as CanCan.
You could quickly do a controller filter to redirect logged in users like so in a controller:
def SomeController < ApplicationController
before_filter :redirect_logged_in_user, :only => :action_to_prevent
private
def redirect_logged_in_user
redirect_to your_redirect_path if current_user
end
end
Devise is authentication system. To control users access to some pages you need authorization. For example, https://github.com/ryanb/cancan

Resources