preloader
軟體工程

Associate Devise with Doorkeeper gem for OAuth | 結合Devise帳戶認證和Doorkeeper的OAuth Client認證

If you want to associate your user model, which are built by Devise gem, with OAuth clients, which are authenticated and authorized by Doorkeeper gem, you should follow below instructions. Below instructions come from wiki pages of Doorkeeper gem.

Command for Database changes

rails generate doorkeeper:application_owner
rake db:migrate

 

Configuration at config/initializers/doorkeeper.rb

Uncomment or add this statement into block of Doorkeeper.configure

enable_application_confirmation => false

If this is true for enable_application_confirmation, it will enforce that every OAuth client must belong someone in your user model, which is built by Devise gem.

Whether you add enable_application_confirmation statement into the block or not, it’s better to do the following thing together at your user model(built by Devise gem):

class User

...

has_many :oauth_applications, class_name: 'Doorkeeper::Application', as: :owner

end

 

After this, you can run this command at terminal:

rails console

And typing below statements in console to test first oauth client for binding an user in your system:

app = Doorkeeper::Application.new :name => 'test', :redirect_uri => 'http://yourappuri.com'
app.owner = User.last #or any user in other classes, any class you want to associate with. For example, I use the latest user in my User model
app.save

Now the database have already associated an OAuth client and an user, and you could see related values in database if you key in below statements at console:

User.last.oauth_applications

If you want to see how this work with controllers, you visit the link at reference of this article. 

 

Reference:

https://github.com/doorkeeper-gem/doorkeeper/wiki/Associate-users-to-OAuth-applications-(ownership)