Acts_as_Authenticated Plugin

Unfortunately, Rails does not (surprisingly) contain any kind of built-in login system.

There are several plugins you can use–probably the best solution is Acts_as_authenticated.

To set up Acts_as_authenticated, you need to:

  1. Install the plugin: script/plugin install http://svn.techno-weenie.net/projects/plugins/acts_as_authenticated
  2. Generate the user and account models: script/generate authenticated user account
  3. Rake the DB to add the users table: rake db:migrate
  4. Finally, add this line to application.rb controller so you can use the gem: include AuthenticatedSystem

That’s it! To create a new account, hit up the signup page: http://localhost:3000/account/signup.

To protect pages so they require login, simply add this line to the beginning of your controller class:

before_filter :login_required

If you want to exclude certain views from requiring authentication, just add the list of exceptions:


before_filter :login_required, :except => [:list]

def list
# list view stuff ...
end

# ...

If you want to get information about the current user, you can tap into the current_user variable from any of your controllers. If the user is not logged in, it’s set to nil; otherwise, it’s an instance of User.

And that’s all! The user and account classes come with some pre-built code–signing up, logging in, deleting your own account, etc. All that functionality for free!

About Ashiq Alibhai

Ashiq Alibhai, PMP, has been a Rails aficionado since 2007, and developed web applications since early 2003, where he learned PHP in one summer. As the driving-force behind RailsRocket and the Launchpad project, he seeks to share the ease of development with Rails far and wide.
This entry was posted in Development, Gems and Plugins and tagged . Bookmark the permalink.

One Response to Acts_as_Authenticated Plugin