# Problems creating a simple auth, please help

**URL:** <https://www.ruby-forum.com/t/problems-creating-a-simple-auth-please-help/132182>\
**Category:** Rails\
**Created:** [March 17, 2008, 6:32pm UTC](https://www.ruby-forum.com/t/problems-creating-a-simple-auth-please-help/132182 "2008-03-17T18:32:09Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![ReK2](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/9de053/32.png) [@ReK2](https://www.ruby-forum.com/u/ReK2)\
**Post date:** [March 17, 2008, 6:32pm UTC](https://www.ruby-forum.com/t/problems-creating-a-simple-auth-please-help/132182/1 "2008-03-17T18:32:09Z")

</div>

## Hi, I am new to ruby/rails. learning fast but still getting stuck on key places… so I have a db and a column called email and crypt one is obviously 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 I have another model called users for something else.

```
   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 appreciated.

Thanks

---

<div class="post-metadata">

**Author:** ![ReK2](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/9de053/32.png) [@ReK2](https://www.ruby-forum.com/u/ReK2)\
**Post date:** [March 17, 2008, 7:03pm UTC](https://www.ruby-forum.com/t/problems-creating-a-simple-auth-please-help/132182/2 "2008-03-17T19:03:30Z")

</div>

On 17 Mar 2008, at 17:28, ReK2 wrote:

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

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

Like I said the last time you asked, you can’t use a random salt like  
that - you need to use the same salt as when you created the user  
Users.crypt will try and call the crypt method on the Users class. I  
suspect you actually want self.crypt (to get the value for that user).

Fred

---

<div class="post-metadata">

**Author:** ![ReK2](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/9de053/32.png) [@ReK2](https://www.ruby-forum.com/u/ReK2)\
**Post date:** [March 17, 2008, 7:24pm UTC](https://www.ruby-forum.com/t/problems-creating-a-simple-auth-please-help/132182/3 "2008-03-17T19:24:44Z")

</div>

Hi, sorry I must to have missed your other email…  
but if that is the only issue then why is not doing anythign at all?  
I mean it should “flash” me that the user/passwod was wrong… this is  
why I suspect  
there is something mayor that I am not doing right here…

Thanks for your response hopefully someone can help me with this.

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

---

<div class="post-metadata">

**Author:** ![ReK2](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/9de053/32.png) [@ReK2](https://www.ruby-forum.com/u/ReK2)\
**Post date:** [March 17, 2008, 7:35pm UTC](https://www.ruby-forum.com/t/problems-creating-a-simple-auth-please-help/132182/4 "2008-03-17T19:35:59Z")

</div>

On 17 Mar 2008, at 18:23, rek2 wrote:

> Hi, sorry I must to have missed your other email…  
> but if that is the only issue then why is not doing anythign at all?  
> I mean it should “flash” me that the user/passwod was wrong… this is  
> why I suspect  
> there is something mayor that I am not doing right here…

Yes: your form\_tag doesn’t specify an action, so is just posting to  
the login page, whereas you want it to post to the check action.

Fred
