This guide is relevant for Ruby on Rails with ActiveRecord. It assumes you have a User model using Devise as the authentication framework.
If you want to see how doorkeeper integrates with an existing application, check out the doorkeeper-provider-app repository, which is based on this guide.
Installation
The first step is to add Doorkeeper to your project's dependencies:
bundle add doorkeeper
After that, you need to generate relevant files with:
bundle exec rails generate doorkeeper:install
This will introduce three changes:
A new initializer in config/initializers/doorkeeper.rb
Before executing the migration, you may want to add foreign keys to doorkeeper's tables to ensure data integrity. Go to the migration file and uncomment the lines below:
Now you're ready to run the migrations:
As the next step, you may want to add associations to your model. If you skip this step, you'll encounter ActiveRecord::InvalidForeignKeyerror when you try to destroy the User that has associated access grants or access tokens.
# Uncomment below to ensure a valid reference to the resource owner's table
add_foreign_key :oauth_access_grants, :users, column: :resource_owner_id
add_foreign_key :oauth_access_tokens, :users, column: :resource_owner_id
bundle exec rake db:migrate
app/models/user.rb
class User < ApplicationRecord
has_many :access_grants,
class_name: 'Doorkeeper::AccessGrant',
foreign_key: :resource_owner_id,
dependent: :delete_all # or :destroy if you need callbacks
has_many :access_tokens,
class_name: 'Doorkeeper::AccessToken',
foreign_key: :resource_owner_id,
dependent: :delete_all # or :destroy if you need callbacks
end