Naturaily logo with transparent background

API authentication using Devise and Doorkeeper (minimal setup)

Have you ever tried to setup Devise and Doorkeeper in the simplest possible way, without oauth applications etc? Yeah, preparing Rails API authentication can be more flawless than you think. In this article, I’ll show you in a few easy steps the setup you searched for.

Stone temple interior with ornate columns and a small, lit central altar. The area is dimly lit, highlighting the intricate carvings.

Have you ever tried to setup Devise and Doorkeeper in the simplest possible way, without oauth applications etc? Here it is! In this article, I'll show you in a few easy steps that setup you searched for!

Let’s start by creating new, simple Rails application (or clone this one https://github.com/Naturaily/devise-doorkeeper).

  1. rails new myapp

  2. cd myapp

  3. rails g scaffold items name:string description:text

  4. rake db:migrate

  5. add root to: 'items#index' to config/routes.rb

Now we have a simple app with Items CRUD. Let’s add some code to handle Users.

  1. add gem 'devise' to Gemfile and run bundle install

  2. rails g devise:install

  3. rails g devise User

  4. rake db:migrate

  5. add before_action :authenticate_user! to items_controller

OK, only logged in users can CRUD items now. Off to the most exciting part. We want the very same feature on the API, because mobile app is being created. We want the Items CRUD available and we will authenticate every action using Doorkeeper for this, because it’s the easiest thing you can do.

Let’s install Doorkeeper.

  1. add gem 'doorkeeper' to Gemfile and run bundle install

  2. rails g doorkeeper:install

  3. rails g doorkeeper:migration

Now edit that new migration, it should look like this:

tsx

We removed oauth_applications and oauth_access_grants tables (we simply don’t need them). We need to remove associated foreing keys and indexes too. I also removed previous_refresh_token field from oauth_access_tokens table (please read the comment generated by Doorkeeper). And there is a little hack too. We need to change t.references :application, null: false to t.integer :application_id Without that our example won’t work!

Now we can run migrations

tsx

We need to mount doorkeeper in our router. It can be easily done by use_doorkeeper method. But we should remember that we need nothing but tokens! So our code in config/routes.rb can looks like the code below:

tsx

Now let’s integrate Doorkeeper with Devise. First, we need a method to find user by email and password. Let’s edit app/models/user.rb.

tsx

Next we configure Doorkeeper in config/initializers/doorkeeper.rb to use this method.

tsx

Don’t forget to let Doorkeeper access token with a password.

tsx

We also want refresh tokens, so we need to uncomment the line with use_refresh_token.

Next, we skip app authorization.

tsx

There we go! We can now log in and log out to our API. Try this (please remember to keep the server launched):

tsx

DON’T FORGET TO USE SSL on production and staging environments!

OK, it’s time to use our tokens! Let’s retrieve some Items from our API. How? We need two new controllers. Why two? Because we should have separate controllers for API, so we need ItemsController and base controller for API.

We need app/controllers/api/base_controller.rb, a really simple one.

tsx

And app/controller/api/items_controller.rb (exemplary implementation).

tsx

The most important part of code here is before_action :doorkeeper_authorize!. doorkeeper_authorize! is equaivalent of authenticate_user!. Without that every user could CRUD ours items.

The last one thing: add a new route

tsx

And that’s it! Let’s give it a try.

tsx

Let’s Create a Great Website Together

We'll shape your web platform the way you win it!

More posts in this category

  • Core Web Vitals increase in traffic and sales. Graphic presenting man checking the website's metrics.

    September 30, 2025 • 9 min read

    How Much Will Improving Core Web Vitals Actually Increase My Traffic or Sales?

    Are you one of those business owners asking yourself, ‘Why isn’t my site ranking or converting as well as my competitors’? Even after investing in SEO and marketing, something can still feel missing. The problem often goes beyond keywords or ad spend and comes down to how fast and seamless the website feels to users. This is where Core Web Vitals optimization becomes essential.

    READ MORE
  • Illustration of people interacting with a large screen displaying analytics and a magnifying glass highlighting "#1," on a green background. It is a cover photo of a blogpost about Next.js.

    September 24, 2025 • 8 min read

    Next.js Websites for SaaS Scale-Ups: Why They’re the Growth Engine You Need

    SaaS companies in their scale-up phase face a bottleneck that rarely comes from a lack of product vision. The real slowdown lies in their digital infrastructure, most often their marketing and website layer.

    READ MORE
  • Illustration of a person wearing sunglasses and pointing out to a computer monitor. Above the monitor are icons representing visual, hearing, and speech impairments, symbolizing web accessibility and inclusive design.

    September 15, 2025 • 9 min read

    How Does Web Accessibility Impact SEO and Business Growth?

    Think of your website as a store where 1 in 4 customers can’t get through the door. That’s what happens when web accessibility is ignored. Small issues—like missing alt text or tiny buttons—block users, hurt conversions, and increase legal risk. Fixing them is simple, improves usability for everyone, and builds a stronger, more inclusive brand.

    READ MORE