Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't put email address field on login form (Authlogic)

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.

like image 789
Tom Lehman Avatar asked Aug 26 '09 00:08

Tom Lehman


3 Answers

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

like image 200
Sam Saffron Avatar answered Nov 01 '22 02:11

Sam Saffron


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
like image 37
caike Avatar answered Nov 01 '22 00:11

caike


Please check that your user_session inherits UserSession < Authlogic::Session::Base but not < ActiveRecord::Base

like image 1
user785878 Avatar answered Nov 01 '22 02:11

user785878