Rails

Rails is the most widely used framework in the ruby ecosystem. Its feature and capabilities are wide, and as a consequence, most of the OAuth libraries which work in rails, only work in Rails (ex: doorkeeper). The same can be said of the authentication case (ex: devise)

rodauth-rails changed that for rodauth. It integrates rodauth with the rails feature set, by bringing:

  • db migration generators;
  • view generators;
  • models/helpers/controllers;

and everything that one can expect from a rails app.

How to integrate rodauth-oauth in a rails app?

First thing you’ll have to do is install and integrate rodauth-rails.

Once you do it, you can then:

1. Generate migrations and models

> rails generate rodauth:oauth:install

This will:

  • generate the db migrations to create the necessary tables (you are recommended to open and tweak them according to your needs);
  • generate active record models for the oauth entities (rodauth-oauth does not use them internally, so feel free to update and use them yourself);

2. Generate the views

> rails generate rodauth:oauth:views

This will:

  • Generate a template under app/views with the authorize form;
  • Generate templates for the client application and grants management dashboard (if you pass -a or --features oauth_applications;

which you can then tweak according to your requirements.

3. Enable oauth feature

rodauth-specific configuration can be found under lib/rodauth_app.rb. It’s there where you’ll have to enable the feature:

# in lib/rodauth_app.rb
plugin :rodauth do
  enable :oauth_authorization_code_grant
  # ...

  # you can also add these overrides, so that +current_oauth_account+ and
  # +current_oauth_application+ controller helpers retur active record
  # instances instead:
  oauth_account_ds { |id| Account.where(account_id_column => id) }
  oauth_application_ds { |id| OAuthApplication.where(oauth_applications_id_column => id) }


And that’s it, really.

Check out this blog post on how to use rodauth-oauth with rails and rodauth.

Home