This is a tutorial about how to integrate restful-authentication plugin to your Ruby on Rails application. restufl-authentication is a plugin that makes user authentication easy for you. Installation and configuration is rather simple.
Before starting, I want to give information about my system. I’ve Linux Ubuntu 7.04 installed on my computer. Installed Ruby version is 1.8.6. And the Rails version running on my desktop is 1.2.1.
First of all, we’ll handle requirements by installing required gems. This step is necessary if you plan to use rspec tests in your rails apps. Since our installation requires this feature, first we need to install the following 2 gems.
Create your rails app which will be used during this tutorial. I called it “restauth”. You are free to give any name you like. For the sake of simplicty i chose to use “sqlite” instaead of “mysql” database. If you want to use mysql rather than sqlite, run the “rails” command with “-d mysql” parameter. After creating your rails app, change your current working directory to your newly created rails app.
Install acts_as_state_machine
plugin.
Running the commands above will add some files to our rails app and also add piece of code to existing files.
Routes created: (config/routes.rb)
Models generated:
Controllers generated:
Views generated:
Migration generated: (xxxxxxxxxxxxxx depends on the time you create migration)
rspec tests and stories generated:
So far so good. If you still haven’t run into any errors it’s time to run migration:
Add user activation routes to config/routes.rb
file. (Because we used —include-activation
parameter.)
We used “—stateful”, so we need to add an observer to enviroments and resource for users to routes.
Add user observer to config/environment.rb file (inside “Rails::Initializer.run do |
config | ” block) |
Add users resource to config/routes.rb
file.
As you may remember, we had used “—include-activation” parameter while we were installing the plugin, so we need to make a few quick and easy changes.
Now, open the file app/models/user_mailer.rb
and find and replace YOURSITE
with your website url, ADMINEMAIL
with your email address, [YOURSITE]
with your website’s title/name.
If you don’t want to use randomly generated site key, you could change the value of REST_AUTH_SITE_KEY
located in config/initializers/site_keys.rb
file to something you want.
More Customization
If you want to change the contents of the activation email or signup notification mail sent to users, feel free to alter the following templates to meet your needs.
Default installation of the plugin doesn’t enable the “Remember Me” option. However, you don’t need to worry about it, it’s so very easy to enable it. Just edit the file app/views/sessions/new.html.erb
and uncomment the following lines.
Show Time!
Now, fasten your seatbelt and prepare to take off!
Signup: Make sure your mail server is running and enter a valid email address of yours.
Login
That’s all. If you have any questions about this tutorial don’t hesitate to ask. Thanks for reading.