Skip to content

Instantly share code, notes, and snippets.

@poteto
Created September 15, 2012 15:38
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save poteto/3728497 to your computer and use it in GitHub Desktop.
Save poteto/3728497 to your computer and use it in GitHub Desktop.
[rails] using devise and oauth

Rails user authentication using Devise and Omniauth (OAuth). (Note that the Dropbox API uses OAuth v1)

First in Gemfile:

gem 'devise'
gem 'omniauth'
gem 'omniauth-dropbox'

Create a new app in Dropbox and get your app_key and app_secret: https://www.dropbox.com/developers/apps

Install devise and create a model:

$ rails g devise:install

Open config/initializers/devise.rb and under #==> OmniAuth:

  config.omniauth :dropbox, 'app_key', "app_secret", :require => "omniauth-dropbox"

Now generate a model for Devise to use. I will use User:

$ rails g devise model User

Now open app/models/user.rb and add :omniauthable to the end of devise :database_auth .... Then add :provider, :uid to attr_accessible

Add some columns to the User table:

$ rails g migration AddColumnsToUsers provider:string uid:integer

Now migrate your database and take a look at localhost:3000/users/sign_up:

$ rake db:migrate
$ rails s

You should see a link to Sign in with Dropbox at the bottom of the form with a link to http://localhost:3000/users/auth/dropbox. Clicking on this link sends the User to the Dropbox website with a prompt to add your new app to the User's account. When the User hits "Allow", Dropbox adds the app to your User's Dropbox account and sends you a callback like so:

tbc
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment