Rails 3 / Devise / Oauth2 / Mongoid: Installation Guide
-
Upload
steven-evatt -
Category
Technology
-
view
7.289 -
download
0
description
Transcript of Rails 3 / Devise / Oauth2 / Mongoid: Installation Guide
![Page 1: Rails 3 / Devise / Oauth2 / Mongoid: Installation Guide](https://reader036.fdocuments.us/reader036/viewer/2022062705/5565a9aad8b42a083a8b4e4a/html5/thumbnails/1.jpg)
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
![Page 2: Rails 3 / Devise / Oauth2 / Mongoid: Installation Guide](https://reader036.fdocuments.us/reader036/viewer/2022062705/5565a9aad8b42a083a8b4e4a/html5/thumbnails/2.jpg)
2
Today We'll Cover:
Installing a new rails 3 project Devise Oauth2 Mongoid
Issues Tips Take Aways
![Page 3: Rails 3 / Devise / Oauth2 / Mongoid: Installation Guide](https://reader036.fdocuments.us/reader036/viewer/2022062705/5565a9aad8b42a083a8b4e4a/html5/thumbnails/3.jpg)
3
Why???
I found multiple Devise / Oauth2 install guides, all dated in the past 6 months, all but one were deprecated
No guide existed to add Mongoid into the mix
![Page 4: Rails 3 / Devise / Oauth2 / Mongoid: Installation Guide](https://reader036.fdocuments.us/reader036/viewer/2022062705/5565a9aad8b42a083a8b4e4a/html5/thumbnails/4.jpg)
Prerequisites
• Working Ruby / Rails 3 environment
• Mongodb installed
![Page 5: Rails 3 / Devise / Oauth2 / Mongoid: Installation Guide](https://reader036.fdocuments.us/reader036/viewer/2022062705/5565a9aad8b42a083a8b4e4a/html5/thumbnails/5.jpg)
5
Starting the project
![Page 6: Rails 3 / Devise / Oauth2 / Mongoid: Installation Guide](https://reader036.fdocuments.us/reader036/viewer/2022062705/5565a9aad8b42a083a8b4e4a/html5/thumbnails/6.jpg)
6
Add Required Gems to Gemfile
![Page 7: Rails 3 / Devise / Oauth2 / Mongoid: Installation Guide](https://reader036.fdocuments.us/reader036/viewer/2022062705/5565a9aad8b42a083a8b4e4a/html5/thumbnails/7.jpg)
7
Bundler
![Page 8: Rails 3 / Devise / Oauth2 / Mongoid: Installation Guide](https://reader036.fdocuments.us/reader036/viewer/2022062705/5565a9aad8b42a083a8b4e4a/html5/thumbnails/8.jpg)
8
Setup Mongoid
![Page 9: Rails 3 / Devise / Oauth2 / Mongoid: Installation Guide](https://reader036.fdocuments.us/reader036/viewer/2022062705/5565a9aad8b42a083a8b4e4a/html5/thumbnails/9.jpg)
9
Installing Devise
![Page 10: Rails 3 / Devise / Oauth2 / Mongoid: Installation Guide](https://reader036.fdocuments.us/reader036/viewer/2022062705/5565a9aad8b42a083a8b4e4a/html5/thumbnails/10.jpg)
10
Create a Facebook Application
http://www.facebook.com/developers/createapp.php
![Page 11: Rails 3 / Devise / Oauth2 / Mongoid: Installation Guide](https://reader036.fdocuments.us/reader036/viewer/2022062705/5565a9aad8b42a083a8b4e4a/html5/thumbnails/11.jpg)
11
Facebook App Security
![Page 12: Rails 3 / Devise / Oauth2 / Mongoid: Installation Guide](https://reader036.fdocuments.us/reader036/viewer/2022062705/5565a9aad8b42a083a8b4e4a/html5/thumbnails/12.jpg)
12
Facebook Application Data
![Page 13: Rails 3 / Devise / Oauth2 / Mongoid: Installation Guide](https://reader036.fdocuments.us/reader036/viewer/2022062705/5565a9aad8b42a083a8b4e4a/html5/thumbnails/13.jpg)
13
Passenger Setup
/etc/apache2/site-enabled/007-dev-ror_fb_test
/etc/init.d/apache2 restart
![Page 14: Rails 3 / Devise / Oauth2 / Mongoid: Installation Guide](https://reader036.fdocuments.us/reader036/viewer/2022062705/5565a9aad8b42a083a8b4e4a/html5/thumbnails/14.jpg)
14
Add :omniauthable to devise definition in model:
Apps/models/user.rb
![Page 15: Rails 3 / Devise / Oauth2 / Mongoid: Installation Guide](https://reader036.fdocuments.us/reader036/viewer/2022062705/5565a9aad8b42a083a8b4e4a/html5/thumbnails/15.jpg)
15
Declare the provider in your config/initializers/devise.rb:
config.omniauth :facebook, "APP_ID", "APP_SECRET“
Optional… add
{:scope => ‘email,read_stream,read_facebook_docs_for_scope_names’}
![Page 16: Rails 3 / Devise / Oauth2 / Mongoid: Installation Guide](https://reader036.fdocuments.us/reader036/viewer/2022062705/5565a9aad8b42a083a8b4e4a/html5/thumbnails/16.jpg)
16
Define find_for_facebook_oauth
In the user model (apps/model/user.rb)
![Page 17: Rails 3 / Devise / Oauth2 / Mongoid: Installation Guide](https://reader036.fdocuments.us/reader036/viewer/2022062705/5565a9aad8b42a083a8b4e4a/html5/thumbnails/17.jpg)
17
Create a default view
Generate a default page rails generate controller pages index
Update config/routes.rb root :to => 'pages#index'
Move default rails index out of the way mv public/index.html public/index_org.html
![Page 18: Rails 3 / Devise / Oauth2 / Mongoid: Installation Guide](https://reader036.fdocuments.us/reader036/viewer/2022062705/5565a9aad8b42a083a8b4e4a/html5/thumbnails/18.jpg)
18
Add login button Application Layout
App/views/layouts/applications.html.rb
![Page 19: Rails 3 / Devise / Oauth2 / Mongoid: Installation Guide](https://reader036.fdocuments.us/reader036/viewer/2022062705/5565a9aad8b42a083a8b4e4a/html5/thumbnails/19.jpg)
19
Test Login Button
Strange error!
Caused by missing setting in facebook!
![Page 20: Rails 3 / Devise / Oauth2 / Mongoid: Installation Guide](https://reader036.fdocuments.us/reader036/viewer/2022062705/5565a9aad8b42a083a8b4e4a/html5/thumbnails/20.jpg)
20
Test Login Button – Again
New error
We have not defined the callback for this action In config/routes.rb
![Page 21: Rails 3 / Devise / Oauth2 / Mongoid: Installation Guide](https://reader036.fdocuments.us/reader036/viewer/2022062705/5565a9aad8b42a083a8b4e4a/html5/thumbnails/21.jpg)
21
Create Callback
Create the callback controller: "app/controllers/users/omniauth_callbacks_controller.rb" with a callback
named after the provider:
![Page 22: Rails 3 / Devise / Oauth2 / Mongoid: Installation Guide](https://reader036.fdocuments.us/reader036/viewer/2022062705/5565a9aad8b42a083a8b4e4a/html5/thumbnails/22.jpg)
22
The Login Button Finally Works!
![Page 23: Rails 3 / Devise / Oauth2 / Mongoid: Installation Guide](https://reader036.fdocuments.us/reader036/viewer/2022062705/5565a9aad8b42a083a8b4e4a/html5/thumbnails/23.jpg)
23
Sessions
Notice that Devise RegistrationsController by default calls "User.new_with_session" 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:
![Page 24: Rails 3 / Devise / Oauth2 / Mongoid: Installation Guide](https://reader036.fdocuments.us/reader036/viewer/2022062705/5565a9aad8b42a083a8b4e4a/html5/thumbnails/24.jpg)
Observations
• 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
• Omniauth support requires Devise 1.2, currently only provided from GIT repository, master branch
• Devise 1.2 only allows you to make one model :omniauthable
![Page 25: Rails 3 / Devise / Oauth2 / Mongoid: Installation Guide](https://reader036.fdocuments.us/reader036/viewer/2022062705/5565a9aad8b42a083a8b4e4a/html5/thumbnails/25.jpg)
25
Take Aways Oauth2 uses the email field with a fake
password. By adding a password you can have both Oauth2 and local logins
It is easier than it first appears to get Rails 3 / Devise / Oauth2 / Mongo all working together
The gem ecosystem is rapidly changing, check to see if advise has been superseded before blindly following anintegration guide
![Page 26: Rails 3 / Devise / Oauth2 / Mongoid: Installation Guide](https://reader036.fdocuments.us/reader036/viewer/2022062705/5565a9aad8b42a083a8b4e4a/html5/thumbnails/26.jpg)
26
Thanks for Coming!
Steven Evatt
Email: [email protected]
Site: PriceChirp.com
Blog: www.evatt.com/blog
Twitter: @sevatt