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!

Tags:     Posted in Development, Gems and Plugins

Rate this article:
1 Star2 Stars3 Stars4 Stars5 Stars (2 votes, average: 2.5 out of 5)
Loading ... Loading ...

Further Reading

Leave a Reply