How would one redirect a non confirmed user on login to a certain page instead of showing a :notice ( " you have to confirm your account " ). Using the Devise gem (latest)
This question: ( Devise - Redirected to page if account isn't confirmed ) provides this solution:
# config/initializers/my_strategy.rb
Warden::Strategies.add(:my_strategy) do
def valid?
true
end
def authenticate!
u = User.find_for_authentication(:email => params[:email])
if u.nil? || !u.valid_password?(params[:password])
fail(:invalid)
elsif !u.confirmed?
fail!("Account needs confirmation.")
redirect!("your_root_url")
end
else
success!(u)
end
end
#config/initializers/devise.rb
config.warden do |manager|
manager.default_strategies(:scope => :user).unshift :my_strategy
end
which does not work.
generate your own session controller to override devise's default session controller with rails g controller sessions
(the name of this controller does not matter, you can have your own choice)
edit config/routes.rb
devise_for :users, :controllers => {:sessions => "sessions"}
edit app/controllers/sessions_controller.rb
class SessionsController < Devise::SessionsController
def create
@user = User.find_by_email(params[:user][:email])
if !@user or @user.confirmed?
super
else
redirect_to YOUR_DESIRED_path
end
end
end
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With