Aen Tan
Aen Tan

Reputation: 3325

Devise: Run code after confirmation

I have a bunch of code that hands over stuff from a logged in user's guest/lazy registered account to his new account which I run when a new session is created.

class SessionsController < Devise::SessionsController

  def new
    super
  end

  def create
    super
    logging_in # this is the method which will run
  end

  def destroy
    super
  end

end

It works when the user logs in. However when Devise logs a user in after confirmation, the above does not get run. Where should I put the method if I want it to run after a user logs in? whether by logging in or confirmation.

Upvotes: 2

Views: 1604

Answers (1)

Aen Tan
Aen Tan

Reputation: 3325

Thanks nash. Here's how I did it.

class ConfirmationsController < Devise::ConfirmationsController

  def new
    super
  end

  def create
    super
  end

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

    if resource.errors.empty?
      set_flash_message(:notice, :confirmed) if is_navigational_format?
      sign_in(resource_name, resource)
      logging_in # Here it is
      respond_with_navigational(resource){ redirect_to after_confirmation_path_for(resource_name, resource) }
    else
      respond_with_navigational(resource.errors, :status => :unprocessable_entity){ render :new }
    end
  end

  protected

    def after_resending_confirmation_instructions_path_for(resource_name)
      new_session_path(resource_name)
    end

    def after_confirmation_path_for(resource_name, resource)
      after_sign_in_path_for(resource)
    end

end

It needs to be added after sign_in because my logging_in method uses current_user.

Upvotes: 6

Related Questions