So I have Authlogic working fine with this user_sessions/new
view:
<% form_for @user_session, :url => user_session_path do |f| %>
<% if @user_session.errors.present? %>
Invalid username or password
<% end %>
<%= f.label :login %><br />
<%= f.text_field :login %><br />
<br />
<%= f.label :password %><br />
<%= f.password_field :password %><br />
<br />
<%= f.check_box :remember_me, :style => 'margin-right: 10px' %>
<%= f.label :remember_me %><br />
<br />
<%= f.submit "Login" %>
<% end %>
But when I change
<%= f.label :login %><br />
<%= f.text_field :login %><br />
to
<%= f.label :email %><br />
<%= f.text_field :email %><br />
I get this error when I load the view:
undefined method `email' for #<UserSession: no credentials provided>
But of course my users table has an email
field, etc.
You are going about this the wrong way:
Try:
class UserSession < Authlogic::Session::Base
find_by_login_method :find_by_login_or_email
end
and in user.rb
def self.find_by_login_or_email(login)
find_by_login(login) || find_by_email(login)
end
Your UserSession has no concept of email (it just knows about login).
It runs through a process of finding a User given a login, which you can intercept and override (with the find_by_login_method
). For an added bonus you can also override, login_field
and password_field
or even verify_password_method
Assuming you're using a User model, the easiest way to use email for authentication in Authlogic is:
class User < ActiveRecord::Base
acts_as_authentic do |c|
c.login_field = 'email'
end
end
Please check that your user_session inherits UserSession < Authlogic::Session::Base but not < ActiveRecord::Base
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With