# Problem autenticating user

**URL:** <https://www.ruby-forum.com/t/problem-autenticating-user/152844>\
**Category:** Rails\
**Created:** [November 7, 2008, 11:57pm UTC](https://www.ruby-forum.com/t/problem-autenticating-user/152844 "2008-11-07T23:57:41Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Margareth\_FloriSSSS](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ba9def/32.png) [@Margareth\_FloriSSSS](https://www.ruby-forum.com/u/Margareth_FloriSSSS)\
**Post date:** [November 7, 2008, 11:57pm UTC](https://www.ruby-forum.com/t/problem-autenticating-user/152844/1 "2008-11-07T23:57:41Z")

</div>

hi every body…

I need help…  
I have the following code in a user model. But when i try to autenticate  
an  
user always returns nil, my session controller calls the autenticate  
method.  
I proves comparing only the database password with new encrypted  
password  
but always returns nil. The database password was encryted with the same  
encrypt method. I dont know what i need to do.  
TNKS a lot…

# Authenticates a user by their login name and unencrypted password.

Returns  
the user or nil.  
\*

def self\*.authenticate(login, password)

u = find\_by\_login(login) # need to get the salt and user\_password

u && u.authenticated?(password) ? u :\* nil

end  
\*

# Encrypts some data with the salt.

- 

def self\*.encrypt(password, salt)

Digest::SHA1.hexdigest("–#{salt}–#{password}–")  
\*

end  
\*

# Encrypts the password with the user salt

- 

def\* encrypt(password)  
\*

self\*.class.encrypt(password, salt)  
\*

end  
\*

- 

def\* authenticated?(password)

user\_password == encrypt(password)  
\*

end  
\*

---

<div class="post-metadata">

**Author:** ![Margareth\_FloriSSSS](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ba9def/32.png) [@Margareth\_FloriSSSS](https://www.ruby-forum.com/u/Margareth_FloriSSSS)\
**Post date:** [November 8, 2008, 10:09pm UTC](https://www.ruby-forum.com/t/problem-autenticating-user/152844/2 "2008-11-08T22:09:11Z")

</div>

On 7 Nov 2008, at 22:57, Margareth Florián wrote:

> hi every body…
> 
> I need help…  
> I have the following code in a user model. But when i try to  
> autenticate an user always returns nil, my session controller calls  
> the autenticate method. I proves comparing only the database  
> password with new encrypted password but always returns nil. The  
> database password was encryted with the same encrypt method. I dont  
> know what i need to do.  
> TNKS a lot…

“When you have excluded the impossible, whatever remains, however  
improbable, must be the truth.”. How are you creating/saving users?  
Fred
