Story 6 – Add the ability for users to register & sign-in
Let’s add the ability for users to create accounts and authenticate. Another great thing about Rails is the various libraries called plugins and gems that we can use to give us functionality instead of re-creating the wheel.
Task 1 – Select the best open-source rails library that can do authentication
There are three good sources to use to find plugins and libraries.
- Github (try searching “rails authentication”, you can refine results by specifying the language “ruby”)
- Ruby Toolbox (look at “authentication”)
- Google (try searching “rails authentication”)
What appears to be the most popular library for authenticating in rails?
Task 2 – Install the plugin
Let’s use Authlogic (http://github.com/binarylogic/authlogic), you can find the tutorial on how to use it in your app here.
Another great resource is RailsCasts.com where there are videos that walk you through solving common problems in rails. There happens to be a railscast for using authlogic, lets watch the video first: Using Authlogic (http://railscasts.com/episodes/160-authlogic)
Follow along with a railscast video using asciicasts.com, the authlogic railscast can be found on asciicasts here (http://asciicasts.com/episodes/160-authlogic)
Task 3 – Secure all the game and team pages from being accessed unless the user is authenticated
Now that we installed and configured ascii casts, lets secure all of our game and team pages. We can do this using before_filters and the require_user method.
Here is an example of our application_controller.rb with require_user. Remeber that our other controllers inherit from this controller so we get these methods in our other controllers.