I want to redirect to the rooth path after a users confirms his email by clicking on the activation link. The devise wiki says to implement the following method in the registrations controller:
def after_inactive_sign_up_path_for(resource_or_scope)
session["user_return_to"] || root_path
end
But it doesnt get picked up and keeps directing my to the following url:
http://localhost:3000/users/sign_in
How can i override this behaviour of devise?
def after_inactive_sign_up_path_for(resource_or_scope)
root_path
end
Anonymousxxx posted the answer in a comment. The method is supposed to be in the confirmationscontroller.
module Users
class ConfirmationsController < Devise::ConfirmationsController
protected
def after_confirmation_path_for(_, _)
root_path
end
end
end
Related
I have a Rails 5 app with Devise. Each user has a role_id where they are assigned a role upon creation. I'm trying to use the after_sign_in_path_for method that Devise gives to redirect to a specific page on login based on the role.
Below is what I have so far, but it doesn't work when trying to sign out a disabled user.
class ApplicationController < ActionController::Base
def after_sign_in_path_for(resource)
case resource.role_id
when Role.find_by(name: "admin").id
root_path
when Role.find_by(name: "disabled").id
destroy_user_session_path
else
super
end
end
end
I'm able to sign in when I'm an admin user and it redirects. But if I try to sign in as a user whose role is disabled, it tries to tear down the session then raises an exception of No route matches [GET] "/users/sign_out". I know the method destroy_user_session_path expects a delete method but how can I pass this in the application controller?
What am I doing wrong here?
Update
I tried the sign_out(resource) as suggested in the first answer, and it raises an exception undefined methodto_model' for true:TrueClassin mymy_sessions_controller.rb` which I use to override the create method to set a login token and limit concurrent sessions. Here is the controller.
class MySessionsController < Devise::SessionsController
skip_before_action :check_concurrent_session
def create
super
set_login_token
end
private
def set_login_token
token = Devise.friendly_token
session[:token] = token
current_user.login_token = token
current_user.save(validate: false)
end
end
You can check roles inside MySessionsController#create and prevent logging if the role not valid instead of allowing user to login then logout
def create
unless current_user.role_id == Role.find_by(name: "disabled").id
super set_login_token
else
redirect_to new_user_session_path, alert: "You can't log in"
end
end
You can also use active_for_authentication? and inactive_message methods in user model to prevent him from login. in /app/models/user.rb:
def active_for_authentication?
super and self.role_id != Role.find_by(name: "disabled").id
end
def inactive_message
"You can't log in"
end
destroy_user_session_path is making [GET] "/users/sign_out" request.
You can use sign_out or reset_session function to delete session directly.
Hope this answer works for you.
Use devise's sign_out(resource) method instead of destroy_user_session_path. This method will destroy the user session.
Spree::UserSessionsController.class_eval do
include Spree::AuthenticationHelpers
def new
end
def create
authenticate_spree_user!
if spree_user_signed_in?
redirect_to main_app.userregistration_new_path
else
render :new
end
end
end
I got above method defination from spree_auth_devise sessionscontroller implementation. Hopefully I have got access to spree_current_user.
Next, I am trying to implement the signout, infact i am able to signout with simple call to
spree_logout_path
But I want to change the redirection after signout. unable to find any specific action declaration for signout..
How to perform this signout along with a different routes redirection in rails + Spree ?
app/controllers/application_controller.rb
class ApplicationController < ActionController::Base
protected
def after_sign_out_path_for(resource)
root_path # change this to where you want to redirect after sign out
end
end
I want after login user redirects to page creating new table(website), so I have function in my ApplicationController:
def after_sign_in_path_for(resource)
new_website_path
end
and after registration I want user redirects to his edit page:
def after_sign_up_path_for(resource)
edit_user_registration_path
end
So the question is - why it doesn't work ?
This is because after_sign_up_path_for(resource) is a protected method of the Devise registrations controller as you can see in the controller. The only way you will be able to get a custom redirect after sign up, is to override the Devise registrations controller on your own. There are instructions on doing so on the Devise Wiki.
So I changed access level like this:
class RegistrationsController < Devise::RegistrationsController
protected
def after_sign_up_path_for(resource)
edit_user_registration_path
end
protected
def after_sign_in_path_for(resource)
new_website_path
end
end
Before this at it was working for after_sign_in and after_sign_out and now it doesn't wor at all. Why ?
I have a customer devise controller and i wan to set my sign up action to update a user status to online when the user signs in and off line when the users logout. I have
def signin
super
end
I want to update the users status attribute to online on login and offline on logout. Pls any help here
You may be missing attr_accesible declaration for the "status" attribute of User table?
You can use the after_sign_in_path_for and after_sign_out_path_for hooks provided in devise. Just override those methods in your ApplicationController. Eg.
class ApplicationController < ActionController::Base
private
def after_sign_in_path_for(resource_or_scope)
#update user status to online
root_path
end
def after_sign_out_path_for(resource_or_scope)
#update user status to offline
root_path
end
end
More info here: devise wiki
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
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
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
Suppose you want to show user's dashboard after logging in.
class HomesController < ApplicationController
def index
if current_user //returns nil if not logged in
#users = User.all
render :dashboard
end
end
def dashboard
#users = User.all
end
end
in routes.rb:
root :to => 'homes#index'
If logged in, if-block is entered in index action and dashboard.erb is rendered.
(make sure to initialize all variables, required by dashboard.erb, in your if-block)
Otherwise rails renders index.erb