← homeProgramming (Програмування)

Main methods of authentication in API

When we create an API in Ruby on Rails, it is important to control who has access to resources. Here are the main approaches to authentication: Basic Authentication The simplest, but least secure method. In Basic Auth...

Table of contentsClick link to navigate to the desired location
This content has been automatically translated from Ukrainian.
When we create an API in Ruby on Rails, it is important to control who has access to resources. Here are the main approaches to authentication:

Basic Authentication

The simplest, but least secure method. In Basic Authentication, the Authorization header transmits username and password encoded in Base64.
Header format:
Authorization: Basic <base64_string>
<base64_string> = Base64-encoded string username:password
For example, if you have:
  • username = apiuser
  • password = secret123
First, we form the string:
apiuser:secret123
Next, we encode it in Base64:
require 'base64'

credentials = "apiuser:secret123"
encoded = Base64.strict_encode64(credentials)
puts encoded
# => YXBpdXNlcjpzZWNyZXQxMjM=
So, the header will look like this:
Authorization: Basic YXBpdXNlcjpzZWNyZXQxMjM=
When Rails sees Authorization: Basic ..., the method authenticate_or_request_with_http_basic decodes Base64, splits username:password, and checks them on the server.

Token Authentication

The user receives a unique token that is added to each request.
A more secure option for mobile or frontend applications.
class Api::V1::BaseController < ApplicationController
  before_action :authenticate_user!

  private

  def authenticate_user!
    token = request.headers['Authorization']&.split(' ')&.last
    @current_user = User.find_by(api_token: token)
    render json: { error: 'Unauthorized' }, status: :unauthorized unless @current_user
  end
end
Request header:
Authorization: Token abc123

JWT (JSON Web Token)

A popular method for stateless API. The server does not store sessions, and the client sends a signed token.
Example with the jwt gem:
# Creating a token
payload = { user_id: user.id, exp: 24.hours.from_now.to_i }
token = JWT.encode(payload, Rails.application.secret_key_base)

# Verifying the token
decoded = JWT.decode(token, Rails.application.secret_key_base).first
user_id = decoded["user_id"]
Header:
Authorization: Bearer <jwt_token>

OAuth 2.0

A standard for authorization and access to resources through third-party services or APIs.
To create your own OAuth 2 server in Rails, use Doorkeeper:
# Gemfile
gem 'doorkeeper'
After configuration, you can issue access tokens to external clients:
Authorization: Bearer <access_token>
If you need to allow users to log in through third-party services (Google, Facebook, GitHub), use OmniAuth in conjunction with Devise for authentication.
In simple terms: Doorkeeper — for API access, OmniAuth — for user login through other services.

🔥 More posts

All posts
What is exponential growth?
Sep 16, '25 18:57

What is exponential growth?

Exponential growth is the process where a quantity increases in a geometric progression. In other...

What is factorial complexity?
Programming (Програмування)Sep 16, '25 19:03

What is factorial complexity?

Factorial complexity is a situation where the number of options or combinations grows like the fa...

What is NP-complexity?
Programming (Програмування)Sep 16, '25 19:31

What is NP-complexity?

NP-completeness is a class of problems for which it is very difficult to find a solution, but eas...

What is ivar in Ruby / Rails?
Programming (Програмування)Oct 19, '25 20:12

What is ivar in Ruby / Rails?

ivar is short for instance variable. In Ruby, it is written with a @ before the name, for example...

What is ORM and why is it needed?
Programming (Програмування)Oct 26, '25 14:00

What is ORM and why is it needed?

When we work with databases, we usually have to write SQL queries - selections, inserts, updates,...