# Help with authlogin gem

**URL:** <https://www.ruby-forum.com/t/help-with-authlogin-gem/156266>\
**Category:** Rails\
**Created:** [December 23, 2008, 5:57pm UTC](https://www.ruby-forum.com/t/help-with-authlogin-gem/156266 "2008-12-23T17:57:12Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![byrnejb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/d2c977/32.png) [@byrnejb](https://www.ruby-forum.com/u/byrnejb)\
**Post date:** [December 23, 2008, 5:57pm UTC](https://www.ruby-forum.com/t/help-with-authlogin-gem/156266/1 "2008-12-23T17:57:12Z")

</div>

I am puzzled over how to normalize the login value provided by the user  
to authlogic to validate the existence of that user. I have threaded  
my way through the code and tried to understand what it and the examples  
are trying to tell me but I am just not making the connections.

Basically I have this in the login (user\_sessions\_controller):

def new  
@user\_session = UserSession.new  
end

def create  
@user\_session = UserSession.new(params[:user\_session])  
# debugger if ENV[‘RAILS\_ENV’] == ‘development’  
@user\_session.save do |result|  
if result  
flash[:notice] = “Welcome”  
redirect\_back\_or\_default account\_url  
else  
render :action =\> :new  
end  
end  
end

What I want to do is modify the input value of the login identity field  
before the User.find is performed. It seems that this should be  
possible in the User model.

Inside authlogic/session.config.rb I find this:

```
    # Authlogic tries to validate the credentials passed to it. One

```

part  
# of validation is actually finding the user and making sure it  
# exists. What method it uses the do this is up to you.  
#  
# Let’s say you have a UserSession that is authenticating a  
User.  
# By default UserSession will call User.find\_by\_login(login).  
You  
# can change what method UserSession calls by specifying it  
here. Then  
# in your User model you can make that method do anything you  
want,  
# giving you complete control of how users are found by the  
UserSession.  
#  
# Let’s take an example: You want to allow users to login by  
username  
# or email. Set this to the name of the class method that does  
this in  
# the User model. Let’s call it “find\_by\_username\_or\_email”  
#  
# class User \< ActiveRecord::Base  
# def self.find\_by\_username\_or\_email(login)  
# find\_by\_username(login) || find\_by\_email(login)  
# end  
# end  
#  
# \* Default: “find\_by\_#{login\_field}”  
# \* Accepts: Symbol or String  
def find\_by\_login\_method(value = nil)  
if value.nil?  
read\_inheritable\_attribute(:find\_by\_login\_method) ||  
find\_by\_login\_method(“find\_by\_#{login\_field}”)  
else  
write\_inheritable\_attribute(:find\_by\_login\_method, value)  
end  
end  
alias\_method :find\_by\_login\_method=, :find\_by\_login\_method

This probably tells me exactly what I have to do but I am afraid I  
cannot see what that is. I tried overriding find\_by\_username but doing  
so causes a nil object exception.

class User \< ActiveRecord::Base  
acts\_as\_authentic

def self.find\_by\_username(login)  
super.find\_by\_username(login.strip.squeeze(" ").mb\_chars.downcase)  
end

end

NoMethodError (You have a nil object when you didn’t expect it!  
The error occurred while evaluating nil.find\_by\_username):  
/app/models/user.rb:29:in `find_by_username' /usr/lib/ruby/gems/1.8/gems/authlogic-1.3.7/lib/authlogic/session/base.rb:397:in`send’

If I do this instead:

def find\_by\_username(login)  
self.find\_by\_username(login.strip.squeeze(" ").mb\_chars.downcase)  
end

OR

def find\_by\_username(login)  
super.find\_by\_username(login.strip.squeeze(" ").mb\_chars.downcase)  
end

Then the SELECT uses the unmodified value input for the username.

Can anyone provide me with some guidance on how to get this to work as I  
desire?

Regards,

---

<div class="post-metadata">

**Author:** ![byrnejb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/d2c977/32.png) [@byrnejb](https://www.ruby-forum.com/u/byrnejb)\
**Post date:** [December 23, 2008, 10:29pm UTC](https://www.ruby-forum.com/t/help-with-authlogin-gem/156266/2 "2008-12-23T22:29:11Z")

</div>

James B. wrote:

> I am puzzled over how to normalize the login value provided by the user  
> to authlogic to validate the existence of that user. I have threaded  
> my way through the code and tried to understand what it and the examples  
> are trying to tell me but I am just not making the connections.

This seems to be the place where the problem lies:

[150, 159] in  
/usr/lib/ruby/gems/1.8/gems/authlogic-1.3.7/lib/authlogic/session/base.rb  
150 def credentials=(values)  
151 return if values.blank? || !values.is\_a?(Hash)  
152 values.symbolize\_keys!  
153 values.each do |field, value|  
154 next if value.blank?  
=\> 155 send("#{field}=", value)  
156 end  
157 end  
158  
159 # Resets everything, your errors, record, cookies, and  
session. Basically “logs out” a user.  
/usr/lib/ruby/gems/1.8/gems/authlogic-1.3.7/lib/authlogic/session/base.rb:155  
send("#{field}=", value)  
(rdb:6) v l  
field =\> “username”  
value =\> “MYUSER”  
values =\> {“username”=\>“MYUSER”, “password”=\>“mypassword”,  
“remember\_me”=\>“0”}

361  
362 attr\_reader :#{login\_field}  
363  
=\> 364 def #{login\_field}=(value)  
365 self.authenticating\_with = :password  
366 @#{login\_field} = value  
367 end  
368  
/usr/lib/ruby/gems/1.8/gems/authlogic-1.3.7/lib/authlogic/session/base.rb:364  
def #{login\_field}=(value)  
(rdb:6) v l  
value =\> “MYUSER”

When the Session::Base code hits line 366 then the direct assignment of  
the @params value of the login\_field to the corresponding instance value  
simply bypasses the setter defined in the User model, does it not?

Given this construction I cannot see a way to invoke the model’s setter  
method for the login field. Am I missing something?
