Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

redirect to a certain page instead of :notice when non-confirmed user logs in

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.

like image 603
Rubytastic Avatar asked Jan 18 '13 01:01

Rubytastic


1 Answers

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
like image 85
Brian Avatar answered Nov 14 '22 18:11

Brian