I want to use the after_sign_in_path_for
and after_inactive_sign_up_path_for
methods to redirect user to some specific page, I would put these two methods and before_action :authenticate_user!
all in the application controller, however, when the before_action method run it will be called on all actions, which will redirect my app to the wrong routes. Should I use before_action :authenticate_user!, except: [:after_sign_in_path_for, :after_inactive_sign_up_path_for]
to skip authentication on these two methods?
class ApplicationController < ActionController::Base
# Prevent CSRF attacks by raising an exception.
# For APIs, you may want to use :null_session instead.
protect_from_forgery with: :exception
before_action :authenticate!
def after_sign_in_path_for(user)
if user && user.is_a?(Vendor)
return edit_vendor_registration_path
elsif user && user.is_a?(Customer)
return dashboard_path
end
end
def after_sign_out_path_for(user)
if user && user.is_a?(Vendor)
return root_path
elsif user && user.is_a?(Customer)
return root_path
end
end
def authenticate!
if @current_user == current_customer
:authenticate_customer!
elsif @current_user == current_vendor
:authenticate_vendor!
end
end
end
I ran into this error Filter chain halted as :require_no_authentication rendered or redirected
, I believe the program somehow created an endless loop that keep redirecting to the dashboard_path.
I think you are mixing up a few things.
before_action :authenticate_user!
is used for each controller action where you want the user to be authenticated so he can continue his request.
e.g. after_sign_in_path_for
is a method of a devise controller which can be overriden like this: https://github.com/plataformatec/devise/wiki/How-To:-Redirect-to-a-specific-page-on-successful-sign-up-%28registration%29