14

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.

Tom Lehman
  • 85,973
  • 71
  • 200
  • 272

7 Answers7

31

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

Sam Saffron
  • 128,308
  • 78
  • 326
  • 506
18

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
caike
  • 8,769
  • 2
  • 19
  • 10
1

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

user785878
  • 11
  • 1
0

I had the same problem and discovered that you have to make sure login is not in your schema and database (you won't need it anyway as you've decided to employ email as I have)

0

I could be entirely wrong about this but I believe that if you create a user model with an email field and no login field then Authlogic will use this for the UserSession. However, if your user model has a login field but you're not using it (i.e. you switched the view to :email rather than :login) then UserSession will still be looking for login and not email so you'll get the error message.

If your user model doesn't have a login field then this problem should go away as Authlogic will default to using :email instead.

Kevin Monk
  • 1,434
  • 1
  • 16
  • 23
-1

I don't know exactly how Authlogic works, but my guess is kind of what you ended with

But of course my users table has an email field, etc.

From the sound of it, that doesn't necessarily mean that your UserSession has it, or does it?

theIV
  • 25,434
  • 5
  • 54
  • 58
  • An Authlogic Session is a model that doesn't touch any database, but instead interacts with the model it's acting as a session for. – Aupajo Jan 05 '10 at 16:50
-2

It is saying that in UserSession there is no email field. It is not looking at the user, since you have :

form_for @user_session
ez.
  • 7,604
  • 5
  • 30
  • 29