Rails 3 / Devise / Oauth2 / Mongoid: Installation Guide

7,438
-1

Published on

Talk given to the Houston Ruby on Rails user group in February of 2011 covering how to integrate Rails3, Devise, Ouath2 (for facebook), and Mongoid.

Published in: Technology
0 Comments
5 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
7,438
On Slideshare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
80
Comments
0
Likes
5
Embeds 0
No embeds

No notes for slide

Rails 3 / Devise / Oauth2 / Mongoid: Installation Guide

  1. 1. Rails 3 / Devise / Oauth2 / Mongoid Steven Evatt, Chron.com Blog: http://www.evatt.com/blog Web: http://PriceChirp.com Twitter: @sevatt Houston-RoR Feb 2010 Installation Guide
  2. 2. Today We'll Cover: <ul><li>Installing a new rails 3 project </li></ul><ul><ul><li>Devise </li></ul></ul><ul><ul><li>Oauth2 </li></ul></ul><ul><ul><li>Mongoid </li></ul></ul><ul><li>Issues </li></ul><ul><li>Tips </li></ul><ul><li>Take Aways </li></ul>
  3. 3. Why??? <ul><li>I found multiple Devise / Oauth2 install guides, all dated in the past 6 months, all but one were deprecated </li></ul><ul><li>No guide existed to add Mongoid into the mix </li></ul>
  4. 4. Prerequisites <ul><li>Working Ruby / Rails 3 environment </li></ul><ul><li>Mongodb installed </li></ul>
  5. 5. Starting the project
  6. 6. Add Required Gems to Gemfile
  7. 7. Bundler
  8. 8. Setup Mongoid
  9. 9. Installing Devise
  10. 10. Create a Facebook Application <ul><li>http://www.facebook.com/developers/createapp.php </li></ul>
  11. 11. Facebook App Security
  12. 12. Facebook Application Data
  13. 13. Passenger Setup <ul><li>/etc/apache2/site-enabled/007-dev-ror_fb_test </li></ul><ul><li>/etc/init.d/apache2 restart </li></ul>
  14. 14. Add :omniauthable to devise definition in model: <ul><li>Apps/models/user.rb </li></ul>
  15. 15. Declare the provider in your config/initializers/devise.rb: <ul><li>config.omniauth :facebook, &quot;APP_ID&quot;, &quot;APP_SECRET“ </li></ul><ul><li>Optional… add </li></ul><ul><li>{:scope => ‘email,read_stream,read_facebook_docs_for_scope_names’} </li></ul>
  16. 16. Define find_for_facebook_oauth <ul><li>In the user model (apps/model/user.rb) </li></ul>
  17. 17. Create a default view <ul><li>Generate a default page </li></ul><ul><li> rails generate controller pages index </li></ul><ul><li>Update config/routes.rb </li></ul><ul><li>root :to => 'pages#index' </li></ul><ul><li>Move default rails index out of the way </li></ul><ul><li>mv public/index.html public/index_org.html </li></ul>
  18. 18. Add login button Application Layout <ul><li>App/views/layouts/applications.html.rb </li></ul>
  19. 19. Test Login Button <ul><li>Strange error! </li></ul><ul><li>Caused by missing setting in facebook! </li></ul>
  20. 20. Test Login Button – Again <ul><li>New error </li></ul><ul><li>We have not defined the callback for this action </li></ul><ul><li>In config/routes.rb </li></ul>
  21. 21. Create Callback <ul><li>Create the callback controller: </li></ul><ul><ul><li>&quot;app/controllers/users/omniauth_callbacks_controller.rb&quot; with a callback named after the provider: </li></ul></ul>
  22. 22. The Login Button Finally Works!
  23. 23. Sessions <ul><li>Notice that Devise RegistrationsController by default calls &quot;User.new_with_session&quot; before building a resource. This means that, if we need to copy data from session whenever an user is initialized before sign up, we just need to implement new_with_session in our model. Here is an example that copies the facebook email if available: </li></ul>
  24. 24. Observations <ul><li>Oauth2 uses the email address as the key. Devise requires a password, so we insert a fake password in the database. By replacing the fake password with a real password, we can have both local and Oauth2 logins </li></ul><ul><li>Omniauth support requires Devise 1.2, currently only provided from GIT repository, master branch </li></ul><ul><li>Devise 1.2 only allows you to make one model :omniauthable </li></ul>
  25. 25. Take Aways <ul><li>Oauth2 uses the email field with a fake password. By adding a password you can have both Oauth2 and local logins </li></ul><ul><li>It is easier than it first appears to get Rails 3 / Devise / Oauth2 / Mongo all working together </li></ul><ul><li>The gem ecosystem is rapidly changing, check to see if advise has been superseded before blindly following an integration guide </li></ul>
  26. 26. Thanks for Coming! <ul><li>Steven Evatt </li></ul><ul><ul><li>Email: steven@evatt.com </li></ul></ul><ul><ul><li>Site: PriceChirp.com </li></ul></ul><ul><ul><li>Blog: www.evatt.com/blog </li></ul></ul><ul><ul><li>Twitter: @sevatt </li></ul></ul>

×