1

I am trying from last 7 hours to auto signin the user after email confirmation but when I click the confirmation link then it says "Your email address has been successfully confirmed." but not sign in the user. I have written this code in my confirmations controller and route

devise_for :users, controllers: {confirmations: 'users/confirmations'}

class ConfirmationsController < Devise::ConfirmationsController
  #private
  def after_confirmation_path_for(resource_name, resource)
    sign_in(resource)
    render plain: "here"
    redirect_to "/admins/view_account", notice: "User deleted."
  end
end

Any help would be highly appreciated Thanks.

john
  • 611
  • 1
  • 7
  • 33

2 Answers2

4

Notice that automatically sign in after e-mail confirmation used to be Devise's default behavior and was then changed (after 3.1) due to security measures as you can see more here and here.

If you still want to do it, depending on the Devise's version make sure to set the line below on the config/initializers/devise.rb file within your project:

config.allow_insecure_sign_in_after_confirmation=true

If you are using latest Devise version, you may have to instead of that, extend the default controller with this code on app/controllers/users/confirmations_controller.rb as a replacement of what you have mentioned above for controller code (please mind the namespaces and path mentioned):

class Users::ConfirmationsController < Devise::ConfirmationsController
  def show
    super do |resource|
      sign_in(resource) if resource.errors.empty?
    end
  end
end

And make sure the code you pasted in the beginning of the question belongs in config/routes.rb:

devise_for :users, controllers: { confirmations: 'users/confirmations' }

Hope it helps!

fagiani
  • 2,293
  • 2
  • 24
  • 31
0

I have solved it myself.

routes should be like this

 devise_for :users, controllers: {confirmations: 'confirmations'} do
    #put "confirm_user", to: "confirmations#confirm_user"
    get "confirmation", to: "confirmations#after_confirmation_path_for"
  end

Controller is like this

class ConfirmationsController < Devise::ConfirmationsController
  #private
   def after_confirmation_path_for(resource_name, resource)
    sign_in(resource)
    #render plain: "here"
    #redirect_to "/admins/"
   end

  def show
    self.resource = resource_class.confirm_by_token(params[:confirmation_token])

    if resource.errors.empty?
      set_flash_message(:notice, :confirmed) if is_flashing_format?
      sign_in(resource) # <= THIS LINE ADDED
      redirect_to "/your path/"
    else
      respond_with_navigational(resource.errors, :status => :unprocessable_entity){ render :new }
    end
  end


end
john
  • 611
  • 1
  • 7
  • 33