# Authenticate

**URL:** <https://www.ruby-forum.com/t/authenticate/115638>\
**Category:** Rails\
**Created:** [September 27, 2007, 3:50am UTC](https://www.ruby-forum.com/t/authenticate/115638 "2007-09-27T03:50:01Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![snantz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c2a13f/32.png) [@snantz](https://www.ruby-forum.com/u/snantz)\
**Post date:** [September 27, 2007, 3:50am UTC](https://www.ruby-forum.com/t/authenticate/115638/1 "2007-09-27T03:50:01Z")

</div>

I have been struggling with this for a while and am at a loss. I have a  
form allowing current users to log in and new users to create an  
account. Upon hitting the submit buttons (one for each action) a method  
gets called either ‘new\_user’ or ‘create\_user’, both makes use of a  
method in my user model called ‘authenticate’. The really weird part is  
that for creating a new user authenticate works fine but not for  
‘current\_user’. To make things weirder, the calls are EXACTLY the same!

In method new\_user:

```
 if request.post? and @user.save
    session[:user] = User.authenticate( params[:user][:email],

```

params[:user]  
[:password] ).id  
redirect\_to :action =\> ‘welcome’, :id =\> @user.id

In current\_user

```
if !@user.nil? and User.authenticate( params[:user][:email],

```

params[:user]  
[:password] )  
session[:user] = User.authenticate( params[:user][:email],  
params[:user]  
[:password] ).id  
flash[:notice] = “\<b style = “color:red”\>Worked”  
redirect\_to :action =\> ‘welcome’, :id =\> @user.id

Authenticate looks like:

def self.authenticate(email, password)  
user = User.find(:first, :conditions =\> [‘upper(email) = ?’,  
email.upcase])  
if user.blank? ||  
Digest::SHA256.hexdigest(password+user.password\_salt)  
!= user.password\_hash  
#raise “Unknown userid or password”  
else  
user  
end  
end

Erro message when current\_user method is called:

can’t convert nil into String

#{RAILS\_ROOT}/app/models/user.rb:18:in `+' #{RAILS_ROOT}/app/models/user.rb:18:in`authenticate’  
#{RAILS\_ROOT}/app/controllers/music\_controller.rb:64:in `current\_user’

Now, given the fact that the call to new user works without throwing any  
errors I’m amusing (aparently wrongfully) that the call to current\_user  
should work as well, especially since I can see that all the correct  
parameters are being passed and therefore know that password is not  
infact nil? Anyone have any ideas? Thanks,

-Shandy

---

<div class="post-metadata">

**Author:** ![snantz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c2a13f/32.png) [@snantz](https://www.ruby-forum.com/u/snantz)\
**Post date:** [September 27, 2007, 4:10am UTC](https://www.ruby-forum.com/t/authenticate/115638/2 "2007-09-27T04:10:50Z")

</div>

can password\_salt be nil ? Are use sure that the parameters are arriving  
as params[:user][:password] and not params[:password] ?  
Have you tried using rdebug to step through your code ?

Fred

---

<div class="post-metadata">

**Author:** ![snantz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c2a13f/32.png) [@snantz](https://www.ruby-forum.com/u/snantz)\
**Post date:** [September 27, 2007, 3:56pm UTC](https://www.ruby-forum.com/t/authenticate/115638/3 "2007-09-27T15:56:05Z")

</div>

Frederick C. wrote:

> can password\_salt be nil ? Are use sure that the parameters are arriving  
> as params[:user][:password] and not params[:password] ?  
> Have you tried using rdebug to step through your code ?
> 
> Fred

I’m sure that the parameters are arriving correctly. On the error page  
that is displayed I can see that the correct password is arriving. I do  
believe that the salt is not nill, but I’m not entirly sure and I’ll  
have to check later.
