# Problems with Auth to my mysql db

**URL:** <https://www.ruby-forum.com/t/problems-with-auth-to-my-mysql-db/132113>\
**Category:** Rails\
**Created:** [March 17, 2008, 4:57am UTC](https://www.ruby-forum.com/t/problems-with-auth-to-my-mysql-db/132113 "2008-03-17T04:57:06Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [March 17, 2008, 4:57am UTC](https://www.ruby-forum.com/t/problems-with-auth-to-my-mysql-db/132113/1 "2008-03-17T04:57:06Z")

</div>

## Hi, I am new to ruby/rails. learning fast but still getting stuck on key places… so I have a db and a colum called email and crypt one is obviosly the email of the users and the other the password column. so I have this controller for the main application controller:

## protected def login\_required session[:auth] ? yield : render(:template =\> ‘login/login’) end

this login controller:

* * *

skip\_filter :login\_required

def logout(msg = “”)  
reset\_session  
flash[:notice] = msg if msg.length  
redirect\_to ‘/’  
end

def check  
if not request.post?  
logout(“Invalid request.”)  
elsif session[:auth] = Login.authenticate(params[:email,  
params[:crypt])  
redirect\_to :back  
else  
logout(“Your user name and password are invalid.”)  
end  
end

* * *

and this as model:

set\_table\_name “users” # this is because the table is not named login  
but users.

```
    validates_presence_of :email

    def self.authenticate(email, crypt)
    password = crypt
    if email
            salt = 

```

## [Array.new(6){rand(256).chr}.join].pack(“m”).chomp expected\_password = password.crypt(salt) if Users.crypt != expected\_password email = nil end end email end

and here is my views:

\<%= error\_messages\_for ‘login’ %\>

Enter your email and password \<% form\_tag do %\> 

Email  
 \<%= text\_field\_tag :email, params[:email] %\>

Password  
 \<%= password\_field\_tag :crypt, params[:crypt] %\>

 \<%= submit\_tag "Login" %\> \<% end %\>

the issue: I see the form… that is easy 😉  
but I put in a real email and password and does not work…  
when I put a wrong one it does not give me the result I expect…

I use the path and no matter were I go it will redirect me to the  
login(this is good)  
[www.domain.org/login](http://www.domain.org/login)

Any help will be VERY apreciated.

Thanks

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [March 17, 2008, 5:03am UTC](https://www.ruby-forum.com/t/problems-with-auth-to-my-mysql-db/132113/2 "2008-03-17T05:03:33Z")

</div>

sorry the title of my last email is confusing since looks like I am  
having trouble with mysql, but no is with my code.

rek2  
escribió:\> Hi, I am new to ruby/rails. learning fast but still getting stuck on key

> ```
> password = crypt
> 
> <label for="email">Email</label><br/>
> 
> ```
> 
> Any help will be VERY apreciated.
> 
> Thanks
> 
> >

## – [http://rek2.binaryfreedom.info](http://rek2.binaryfreedom.info) [http://www.spboston.org](http://www.spboston.org)

a social revolution is the only feasible route  
to the establishment of socialism.

- Karl Marx -

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [March 17, 2008, 9:42am UTC](https://www.ruby-forum.com/t/problems-with-auth-to-my-mysql-db/132113/3 "2008-03-17T09:42:26Z")

</div>

> > ```
> > def self.authenticate(email, crypt)
> > password = crypt
> > if email
> > salt = [Array.new(6){rand(256).chr}.join].pack("m").chomp
> > expected_password = password.crypt(salt)
> > if Users.crypt != expected_password
> > email = nil
> > 
> > ```

You do know that you need the salt to be the same as when you  
originally created the user right? (if not what would be the point of  
it?)

Fred
