Authlogic: How to find if a user was not logged in because the user was not logged in
I am using authlogic with rails application for authentication. The perishable token开发者_JAVA百科 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
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 && !@user.active?
@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 }
Use your database to capture the registration, activation, reset and other details. So that you know what action needs to to taken.
Mike:
Instead of adding a new filter, you should skip the filter you already have in your application.rb
The way to do this is:
skip_before_filter :require_user
I hope this will be useful for someone
精彩评论