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