At the Forge - Authenticating to a Rails Application
To install a new plugin—say, acts_as_authenticated—we must provide its URL to script/plugin. This is as easy as the following:
script/plugin install http://svn.techno-weenie.net/projects/plugins/acts_as_authenticated
Now, what happens when you install a plugin? Rails installs it into the vendor/plugins directory, under a new directory named after the plugin. Thus, my installation of acts_as_authenticated installed a number of files into vendor/plugins/acts_as_authenticated.
In and of itself, installing the plugin doesn't change my Rails installation or add any new functionality. Rather, a plugin typically creates one or more generators, which are used to create or modify files used by the application.
In the case of acts_as_authenticated, it comes with two different generators, which we can see by going into the generators subdirectory. Here, there are two generators, named authenticated and authenticated_mailer. If we go into the authenticated directory, we see authenticated_generator, which is what defines the generator. This allows us to go to the root directory of our Rails application and type:
script/generate authenticated user account
The above tells Rails that we want to use the authenticated plugin, which it finds in the plugin directory. The other arguments to this command indicate the model (and table name) we will use (user in this case), and the controller that should be generated to handle accounts.
The generator creates a migration file, defining the columns of the Users table using Ruby for greater database independence. In order to create the columns of the database, we must run the migration:
Using my PostgreSQL database client, I now can see that the migration did its job:
atf_development=# \d users Table "public.users" Column | Type | ---------------------------+-----------------------------+ id | integer | login | character varying(255) | email | character varying(255) | crypted_password | character varying(40) | salt | character varying(40) | created_at | timestamp without time zone | updated_at | timestamp without time zone | remember_token | character varying(255) | remember_token_expires_at | timestamp without time zone |
Now that I have incorporated acts_as_authenticated into my application, I should be able to do several simple things:
Mark pages as open to the public.
Mark pages as private—that is, open only to registered users.
Allow people to register.
Allow users to log in.
Allow users to log out.
All of this is not only possible with acts_as_authenticated, but it's also quite easy. To make pages require authentication by default, we can say:
Of course, if we require that people log in before they use the login page, users will find themselves in an infinite loop. So, we can add an exception for that at the top of account_controller.rb:
before_filter :login_required, :except => [:login, :signup]
Once this filter is in place, trying to visit any page other than login or :signup bounces us back to the login page.
I'm going to register, by entering my login name, my e-mail address and my password (twice) into the registration form. Once I click on the submit button, the application inserts my data into the database. I'm in there, with ID #1, and my plain-text data as well as my encrypted data.
Moreover, after registering with the site, I am now signed in as well. I can view any page I want, without having to log in again. My login will last until I go to the /account/logout URL. Unfortunately, the default index.rhtml page that comes with acts_as_authenticated does not make it clear when you have logged out. We can check that easily by adding a line to the top, showing the contents of non-blank notices:
<p><%= flash[:notice] if not flash[:notice].blank? %></p>
We now have a basically working version of an authenticated Web server. People can register (and log in if they are already registered), and we can add both restricted and unrestricted pages via the controller and the before_filter :login_required command.
Webinar: 8 Signs You’re Beyond Cron
11am CDT, April 29th
- New Products
- Users, Permissions and Multitenant Sites
- Not So Dynamic Updates
- Flexible Access Control with Squid Proxy
- Security in Three Ds: Detect, Decide and Deny
- DevOps: Everything You Need to Know
- Tighten Up SSH
- Solving ODEs on Linux
- Non-Linux FOSS: MenuMeters
- diff -u: What's New in Kernel Development