Authentication System Help Needed

This is my first question posted here, I hope this goes well.

I'm trying to work out a simple authentication system for my first Rails site using authenticate_or_request_with_http_basic. Here is the code I am using right now:

def authenticate   authenticate_or_request_with_http_basic do |name, pass|     name == 'foo' && pass == 'bar'   end end

It works when it is like that, but I would like to be able to expand it out from there (obviously) in order to use the stored usernames and passwords of my users for them to log in. Nothing too serious, but I'm stuck trying to go from here.

My User table has fields named 'username' and 'password' and I'm looking for some direction on how to incorporate those two fields into the above method so that I will have the users use that.

Thanks for any help!

Welcome to Rails blog

There is a pretty simple example in Agile web development with ROR, and the first edition of that book is available online for free (Google it)

For an extensive system, go here, but it is overwhelmingly complete http://www.railsforum.com/viewtopic.php?id=14216&p=1

Don't be afraid to post questions. You will find a very helpful bunch of people here.

Good luck

You would recommend building my own authentication system instead of using the built-in help?

Just making sure I understand.

I would recoment

http://wiki.rubyonrails.com/rails/pages/LoginGenerator

or

http://wiki.rubyonrails.org/rails/pages/SaltedHashLoginGenerator

This is my first question posted here, I hope this goes well.

I'm trying to work out a simple authentication system for my first Rails site using authenticate_or_request_with_http_basic. Here is the code I am using right now:

def authenticate authenticate_or_request_with_http_basic do |name, pass|    name == 'foo' && pass == 'bar' end end

Setting to one side the question of whether to roll your own
authentication thing or use someone else's, what you want here is to
see whether or not a use exists with the supplied combination of name
and password. This boils down to just find :first, :conditions =>
["username = ? AND password = ?", name, pass] and seeing whether you
get back nil (no matching record) or something else.

For extra happiness bung this in the User model (eg in a method called
authenticate), and look at dynamic finders
(User.find_by_username_and_password(name, pass)).

Fred

And if you don’t roll your own, I recommend authenticated_system or restful_authentication, the latter of which is just a restful version of the former.

Thanks to everyone for the help and discussion. I'm going to sit on this for a little bit and see what I can come up with. Thanks again!

I second you on that, also Bobnation might want to check out the great tutorial about the RESTful Authentication plugin here : http://www.railsforum.com/viewtopic.php?id=14216

After going through the tutorial, Bobnation, you'll have understood most of the issues of an authenticated login system AND the RESTful way of doing things in Rails 2.0. Hardly a waste of time :wink:

Bobnation wrote:

Thanks to everyone for the help and discussion. I'm going to sit on this for a little bit and see what I can come up with. Thanks again!

And if you don't roll your own, I recommend authenticated_system or restful_authentication, the latter of which is just a restful version of the former.

I saw a link to a restful_authentication tutorial from the ruby forums in this thread and thought I'd add my own tutorial in case you find it helpful. I won't try and tell you mine's better, just different, and I've always found that learning something in more than one way helps me really get a grasp of the subject.

http://railsdotnext.com/

It's in blog format so it reads back-to-front. Step one is here: http://railsdotnext.com/2008/02/16/web-bookmarks-on-rails-step-one/

The tutorial has more commentary/explanatory text than most. It's written for the Linux command line and has instructions for keeping track of things in subversion along the way, though it would be easy enough to just skip those steps.

- Aaron

Thanks, your tutorial looks really interesting so I might peruse it to try and pick things form it that I might need. Right now I think I'm in the middle of knowing a little Rails but not enough to really get going ... so the frustrating stage. I'm hoping I'll get through this and then get to the fun part. :slight_smile: