views:

2507

answers:

6

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.

A: 

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
A: 

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
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
+19  A: 

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)
  User.find_by_login(login) || User.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
Nice (this makes a lot of sense -- Authlogic is sick!)
Horace Loeb
Agree, I much prefer it to restful authentication, the design is super solid.
Sam Saffron
A: 

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)

ritchielee
+11  A: 

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
CaikeSouza
Just letting everyone know that this worked great. cheers
BenB
A: 

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