views:

387

answers:

2

I am using authlogic with rails application for authentication. The perishable token is reset after the user resets his password. Consider this case. A user signs up, he forgets to activate the account and he also forgets the passwords. So he resets the password. Due to this his activation link no longer remains valid. So he cannot activate his account. When he tries to login he get an error that the account is not activated. The user is stuck!.

What solution I found was to resend the activation link every time the login is prevented due the problem that the account is not activated.

Now the problem is that I need to check what type of error it is when the user tries to login, so that I can resend the activation email.

Regards, Pankaj

A: 

Use your database to capture the registration, activation, reset and other details. So that you know what action needs to to taken.

Script Runner
+1  A: 

Rather than check for the actual error, I was able to check for the conditions that cause it using @user_session.attempted_record && !@user_session.invalid_password? && !@user_session.attempted_record.active?

Here's my full solution to resending activation emails (after setting up activation using matthook's tutorial):

# /app/controllers/users_sessions_controler.rb
def create
  @user_session = UserSession.new(params[:user_session])
  if @user_session.save
    redirect_back_or_default root_path
  elsif @user_session.attempted_record &&
    !@user_session.invalid_password? &&
    !@user_session.attempted_record.active?
    flash[:notice] = render_to_string(:partial => 'user_sessions/not_active.erb', :locals => { :user => @user_session.attempted_record })
    redirect_to :action => :new
  else
    render :action => :new
  end
end

# /app/views/user_sessions/_not_active.erb
Sorry, before you can sign in you need to confirm your email address. <%= link_to('Resend confirmation email', resend_activation_users_path(:login => user.login)) %>

# /app/controllers/users_controller.rb
def resend_activation
  if params[:login]
    @user = User.find_by_login params[:login]
    if @user && [email protected]?
      @user.deliver_activation_instructions!
      flash[:notice] = "Please check your e-mail for your account activation instructions!"
      redirect_to root_path
    end
  end
end

# /config/routes.rb
map.resources :users, :collection => { :resend_activation => :get }
Tobias Cohen