Rails Acts As Authenticated Plugin

What is the first model you would create when developing a Ruby on Rails based web service? Mostly likely you would create a user model so as to authenticate users and grant them certain business specific privileges. Creating the required table, model, and views for a user is so common that there exists a great Rails plugin for the task. Acts as Authenticated is a plugin generator that will create your base user model and views required for a user-centric web application. Just like previous plugins, you need download and install the plugin by using the ‘script/plugin install URL’ command where you replace URL with the following.

http://svn.techno-weenie.net/projects/plugins/acts_as_authenticated

Unlike other plugins that provide additional methods to ActiveRecord models, this plugin is a generator. Run the following command to create a user model and a login controller.

script/generate authenticated user login

As mentioned, the above command will create a login controller with associated views (index, login, signup), a user model, some files under lib (authenticated_system, authenticated_test_helper), a controller helper, a controller functional test, a user unit test, a user fixture yml, and a create users migration. Wow, that is a powerful.

At this point you need to run the migration to update your database with the Acts as Authenticated users table.

rake migrate

At this point you can direct your browser to http://localhost:3000/login/signup to create a new user or http://localhost:3000/login/login to log into the system with a user and password.

The Acts as Authenticated plugin provides some functionality that allows any and all of your controllers to validate that your a user has logged in, if not they will be sent to the login page. There is also a ‘remember me’ feature that stores a cookie in the client’s browser that helps to detect that the user has previously logged in. To activate these functionality open the login controller that was created by the generator, cut the following lines and paste them in the application controller.

include AuthenticatedSystem
before_filter :login_from_cookie

With the above two lines in your base application controller you can add the a before_filter to any other controller to ensure that your visitors first login.

before_filter :login_required

To have access to the currently logged in user you can do so through session[:user] or current_user.

The Acts as Authenticated also provides a authenticated_mailer generator. Once activated correctly the mailer will send an email to new users with an activation URL. New users would be able to login only after they activate their account by clicking the activation URL.

Technorati Tags: , , , , , , , , , , , ,

Related posts:

  1. Acts As Voteable Rails Plugin
  2. Acts As Bookmarkable Plugin
  3. Acts As Taggable Plugin
  4. Ruby On Rails Controller Path
  5. Acts As Versioned Plugin
  6. Updating A Rails Plugin


4 Responses to “Rails Acts As Authenticated Plugin”

  • rebecca Says:

    We have noticed that there is a problem with this plugin where if the user has not finished activating their account, and they attempt to sign in they get a wrong username/password error rather than a notice that they haven’t activated their account, with an option to re-activate. Just wondering if anyone has scripted this functionality in and if so if it has been or could be added back to the plugin. If not we are going to add it, but if it’s done no need to double up!

  • Bill Says:

    Just wanted to say thanks for covering AAA so well. I’m new to RoR and was having trouble getting a user set up. I had everything working but didn’t know how to populate the database. I simply needed to go to http://localhost:3000/login/signup.
    Thanks again.

  • sufiyan Says:

    am using acts as authenticated plugin, am unable to access username in my controllers

  • Fer Martin Says:

    Thank you! great article!

    one small remark… should change:

    rake migrate –> rake db:migrate

    good job!

Leave a Reply