rails 3 / devise / oauth2 / mongoid: installation guide

26
ils 3 / Devise / Oauth2 / Mongo Steven Evatt, Chron.com Blog: http://www.evatt.com/blog Web: http://PriceChirp.com Twitter: @sevatt Houston-RoR Feb 2010 Installation Guide

Upload: steven-evatt

Post on 27-May-2015

7.298 views

Category:

Technology


0 download

DESCRIPTION

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.

TRANSCRIPT

Page 1: Rails 3 / Devise / Oauth2 / Mongoid: Installation Guide

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

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

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

Prerequisites

• Working Ruby / Rails 3 environment

• Mongodb installed

Page 5: Rails 3 / Devise / Oauth2 / Mongoid: Installation Guide

5

Starting the project

Page 6: Rails 3 / Devise / Oauth2 / Mongoid: Installation Guide

6

Add Required Gems to Gemfile

Page 7: Rails 3 / Devise / Oauth2 / Mongoid: Installation Guide

7

Bundler

Page 8: Rails 3 / Devise / Oauth2 / Mongoid: Installation Guide

8

Setup Mongoid

Page 9: Rails 3 / Devise / Oauth2 / Mongoid: Installation Guide

9

Installing Devise

Page 10: Rails 3 / Devise / Oauth2 / Mongoid: Installation Guide

10

Create a Facebook Application

http://www.facebook.com/developers/createapp.php

Page 11: Rails 3 / Devise / Oauth2 / Mongoid: Installation Guide

11

Facebook App Security

Page 12: Rails 3 / Devise / Oauth2 / Mongoid: Installation Guide

12

Facebook Application Data

Page 13: Rails 3 / Devise / Oauth2 / Mongoid: Installation Guide

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

14

Add :omniauthable to devise definition in model:

Apps/models/user.rb

Page 15: Rails 3 / Devise / Oauth2 / Mongoid: Installation Guide

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

16

Define find_for_facebook_oauth

In the user model (apps/model/user.rb)

Page 17: Rails 3 / Devise / Oauth2 / Mongoid: Installation Guide

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

18

Add login button Application Layout

App/views/layouts/applications.html.rb

Page 19: Rails 3 / Devise / Oauth2 / Mongoid: Installation Guide

19

Test Login Button

Strange error!

Caused by missing setting in facebook!

Page 20: Rails 3 / Devise / Oauth2 / Mongoid: Installation Guide

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

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

22

The Login Button Finally Works!

Page 23: Rails 3 / Devise / Oauth2 / Mongoid: Installation Guide

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

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

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

26

Thanks for Coming!

Steven Evatt

Email: [email protected]

Site: PriceChirp.com

Blog: www.evatt.com/blog

Twitter: @sevatt