分享

How to set up Restful Authentication and acts...

 Joshua 2009-04-07

While digging around for a couple of days, I noticed that I couldn’t find a readily available resource for setting up Rick Olson’s - Restful Authentication with Scott Barron’s acts_as_state_machine. In a quest to get these two to play nicely together, I figured I would try to document how to set it all up properly.

For a little background, Restful Authentication is one of the most popular generator plugins for creating a user management system so that visitors to your application can sign up for a membership, get emailed a link to activate your account and login / logout.

acts_as_state_machine (AASM) is used to create a model that handles a number of states. It helps to think of a state as a status. In this scenario, we are talking about the status of a user — such as :pending, :active, and :suspended . AASM also handles the transitional actions it will take to move from one state to another. For example, when a user signs up successfully, they are added to the user table with a state of “:pending”. Once they click the activation link in the automated user verification email, their status changes to “:active”. The restful_authentication plugin uses AASM to check the permission of each and to see whether they are allowed to log in.

  1. Make your app and jump into the vendor directory
  2. rails www
    cd www/vendor/plugins

  3. Download the latest version of the Restful Authentication plugin
  4. (this will require you to have the git utility installed). As of writing this article, the last big update to the plugin was in May of 2008 so keep an eye out to see that the same is true when you clone the plugin.
    git clone git://github.com/technoweenie/restful-authentication.git

  5. Remove the hypen in the name of the plugin folder
  6. Certain versions of Rails have returned an error due to the hyphen in the name of the folder, “restful-authentication”. Therefore, we rename the folder.
    mv restful-authentication/ restful_authentication/

  7. Install acts_as_state_machine
  8. cd ../../
    script/plugin install \
    http://elitists./svn/plugins/acts_as_state_machine/trunk/

  9. Generate the restful_authentication files and settings
  10. This is where the magic happens: this will build your restful authentication system. Just to briefly touch on the parameters and flag. “user” is the name of the model that will handle the user system: things like the name, the email address, the password, and :state (whose mechanics will be controlled by acts_as_state_machine). “sessions” is the name of the controller that will handle the sessions (logging in and out). The flag, “- -stateful”, tells restful_authentication that you plan on using acts_as_state_machine.script/generate authenticated user sessions --stateful

  11. Add a map for the activation link
  12. You can add this anywhere within the do block.
    config/routes.rbmap.activate '/activate/:activation_code', :controller => 'users', :action => 'activate', :activation_code => nil

  13. Add an observer to the user model
  14. config/enviroment.rb

    config.active_record.observers = :user_observer

  15. Add email configuration
  16. I added this in config/environments/development.rb , but if you want this setting to work in all environments, add it to config/environments.rb
    config/environments/development.rbconfig.action_mailer.delivery_method = :sendmail

  17. Tweek the settings that show up in the emails
  18. Here are a sample of my settings, you can do this however suits your app. Replace the domain name with your own, ex. “localhost”. When in development mode, append :3000 to the url, ex “localhost:3000/activate…”.

    app/model/user_mailer.rb

    class UserMailer < ActionMailer::Base
    def signup_notification(user)
    setup_email(user)
    @subject += 'Please activate your new account'
    @body[:url] = “http://www./activate/#{user.activation_code}”
    end
    def activation(user)
    setup_email(user)
    @subject += ‘Your account has been activated!’
    @body[:url] = “http://www./”
    end
    protected
    def setup_email(user)
    @recipients = “#{user.email}”
    @from = “do-not-reply@”
    @subject = “FakingFantastic.com - ”
    @sent_on = Time.now
    @body[:user] = user
    end
    end

With this, RA and AASM are now all set up and running. Now, i will quickly make a home page, and output flashes so you can see the messages RA makes while you are signing up.

  1. Make a controller for a homepage
  2. script/generate controller site index

  3. Add a root map to point to the page
  4. config/routes.rb

    map.root :controller => "site", :view => "index"

  5. Create application.html.erb, add flash outputs and yield to site to see messages from rest_auth
  6. app/views/layout/application.html.erb

    <%= flash[:notice] %>
    <%= flash[:error] %>
    <%= yield %>

  7. Build the database and remove the default homepage
  8. rake db:migrate
    rm public/index.html

That’s all it takes. To see the system in action, fire up your server using “script/server” and navigate to “/signup’. You will be greeted by the following screen.

Restful Authentication Signup Screen

Restful Authentication Signup Screen

After properly filling out the form, you will be registered into the system with a state of “pending”.

Successful Registration

Successful Registration

As it says, an email with an activation link has been sent out. You can tail the development log found in

log/development.log

and look for the email message. It should appear as something like this:

Email inside of development log

If you copy that activation link into your browser, it will trigger the User controller and the activate action thanks to the activation route we put in. This will change the users state from :pending to :active so that they can log in. Once complete, you should be redirected to the login screen with a message letting you know it worked.

Sign-Up Complete

Sign-Up Complete

Type in your credentials and you should be able to log in successfully.

Successful Login

Successful Login

That’s it. If you found this helpful, be sure to check out my next post where I change restful_authentication to use the email address as the login name.

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多