RailsStarter

rails-api-auth-template — Rails Starter Template

By rustam-tolipov

An authentication starter template based on Rails API-only mode, integrating Devise with hand-written JWT, providing registration, login, refresh, logout, role authorization, rate limiting, and Swagger documentation.

Explore the design

Compare the page layouts and mobile experience. Open any screenshot for a closer look.

1 previews
Author preview
Author-provided preview

Preview provided in the project README.

One original preview is available. Open the full image to inspect the design.

Screenshots show visual appearance; interactive behavior may differ.

Recorded 2026-10-13View source

What stands out

A quick read of the recorded project description.

01

Devise user authentication combined with hand-written JWT, providing registration, login, refresh, and logout endpoints

02

Logout invalidates the current token immediately via a token blacklist, with JTI recorded for precise control

03

Access tokens last 1 hour and refresh tokens 7 days, supporting seamless renewal

04

Built-in user, moderator, and admin roles; controllers can restrict access by role

05

Rack::Attack rate limiting on login, registration, and refresh endpoints

06

RSpec tests and Swagger UI API documentation generated by rswag

Template Details

This is a Rails API-only starter template designed to pre-configure the repetitive authentication setup in Rails APIs so developers can go straight into business development. The template uses Devise for user authentication, with JWT implemented by hand rather than relying on devise-jwt.

The authentication flow covers registration, login, token refresh, a protected profile endpoint, and logout. Logout adds the current token to a blacklist, invalidating it immediately. Access tokens are valid for 1 hour, and refresh tokens for 7 days. User roles are user (default), moderator, and admin. Controllers can restrict access by including the AuthorizeRequest and AuthorizeRole modules and calling require_admin or require_moderator.

For security and operations, the template requires JWT_SECRET_KEY to be set, otherwise startup fails; login, registration, and refresh endpoints are rate-limited using Rack::Attack; tokens include a JTI for precise control; and migrations add indexes for blacklisted tokens, refresh tokens, and user roles. Tests use RSpec, and API documentation is generated via rswag into Swagger UI.

The template does not yet include email confirmation, password reset, remember-me tokens, or OAuth login; these are listed in the README's to-do list. Before use, evaluate security settings, monitoring logs, and SSL/TLS configuration yourself.

RailsStarter

Is it right for your project?

Use Cases

  • Quickly setting up a Rails API authentication layer without configuring Devise and JWT from scratch
  • Providing token-based registration, login, and session renewal endpoints for mobile or frontend applications
  • Controlling API access by user, moderator, and admin roles
  • Generating Swagger documentation for authentication endpoints and running RSpec tests

Suitable For

  • Backend developers familiar with Ruby on Rails who need an API-only authentication starting point
  • Teams that want to use Devise and JWT together rather than assembling an authentication flow themselves
  • Projects that need basic security capabilities such as role authorization, token blacklisting, and endpoint rate limiting

Getting started & customization

Use these recorded setup notes alongside the repository instructions for your chosen version.

  1. 1

    Click Use this template on GitHub to create a new repository, or clone it directly and enter the project directory

  2. 2

    Run bundle install, and yarn install if needed

  3. 3

    Copy .env.example to .env and set environment variables such as JWT_SECRET_KEY

  4. 4

    Run rails db:create db:migrate to initialize the database

  5. 5

    Run rspec to execute tests; to generate Swagger documentation, run RAILS_ENV=test bundle exec rake rswag:specs:swaggerize, then start rails s and visit /api-docs

Read repository instructions

License & source material

Usage conditions & limitations

Free to use · keep license notice

This entry covers the free version under the MIT license in the public repository; the free scope is the code of that version and does not imply that external services or third-party assets are free.

Must comply with when using

  • Free use, modification, commercial use, and distribution of this version's code are permitted.
  • When distributing copies or substantial portions of the code, retain the original author's copyright notice and the full MIT license notice.
  • The code is provided as is; warranties and limitations of liability are governed by the original license and applicable law.

Additional costs & items to verify

  • Demo images, fonts, icons, and other third-party assets must be checked against their respective licenses; the code's MIT license alone is not sufficient to judge.
  • External services such as domains, hosting, databases, payments, or AI may incur separate charges.
  • The JWT_SECRET_KEY environment variable must be set before use, otherwise the application will crash.
  • Rack::Attack rate limiting depends on Redis and requires REDIS_URL to be configured (optional, but required when rate limiting is enabled).
  • Expired token cleanup requires configuring an external scheduled task (such as sidekiq or cron); the template does not do this automatically.
  • The template does not yet include registration email confirmation and requires you to implement it.
  • The template does not yet include password reset and requires you to implement it.
  • The template does not yet include remember-me (long-term session) functionality and requires you to implement it.
  • The template does not yet include OAuth third-party login and requires you to implement it.
  • SSL/TLS must be configured for production yourself.
  • Monitoring and logging must be configured for production yourself.
  • Review security settings for your own scenario before use.
View Original Text Basis

Permission is hereby granted, free of charge

Original License Text

JWT_SECRET_KEY must be set (crashes if missing)

Original README

REDIS_URL=your_redis_url (optional, for rack-attack)

Original README

add these to your scheduled jobs (sidekiq, cron, etc):

Original README

add email confirmation for signup 📧

Original README

add password reset functionality 🔑

Original README

add remember me token (long-lived sessions) 💾

Original README

add oauth providers (google, github, etc) 🔗

Original README

configure ssl/tls in production

Original README

set up proper monitoring and logging

Original README

review security settings for your specific use case

Original README
https://github.com/rustam-tolipov/rails-api-auth-template/blob/main/LICENSEhttps://github.com/rustam-tolipov/rails-api-auth-template/blob/main/README.md

Compiled by DeepSeek based on repository license and README ·2026-10-13. Results only cover the materials read; dependencies, images, fonts, and subsequent term changes are subject to their respective original texts.

Project checks & description sources

Template Information

Source platforms
GitHub
Documentation and maintenance reference score
More documented · 83 / 100
Technologies
Rails
Website type
Starter
How this score is calculated

Our heuristic uses license records (up to 30), a demo link (12), a preview (10), stars (15), recorded maintenance (15), description completeness (8), feature records (5) and classification data (5). It is not a code, security or performance test, or a Google score. A link does not establish tested functionality.

What to verify for this project

Repository update recorded
2026-01-28
Screenshot last checked
2026-10-13

A demo link is listed. Check it with your own content and viewport sizes; its presence does not confirm that forms, authentication or payments work.

The catalog identifies Rails as the primary stack. Check the actual dependency manifest and lockfile for versions and hosting requirements. These notes use recorded metadata, not a code audit or a deployment test.

Use a selection guide →

How this description was prepared

AI-assisted description based on catalog records and repository material. Repository authors are not automatically the reviewers of this description.

Review declaration: [email protected] · 2026-10-13

Inspect the recorded source material

SHA-256: a4f442ef87334564c9bf0eb9ac7c8687769d0d57eaa8f142202709ab60a3e1d2

2026-10-13T04:39:16.352Z

Source file / repository
![Ruby](https://img.shields.io/badge/ruby-3.3.1-red)
![Rails](https://img.shields.io/badge/rails-7.2.2.1-red)
![RSpec](https://img.shields.io/badge/tested%20with-rspec-blue)
![Swagger](https://img.shields.io/badge/docs-swagger-yellow)

# rails-api-auth-template

> fast start rails api auth setup with devise + jwt

## 🚀 what is this?

this is a rails 7.2 api-only template with jwt authentication using devise.
you can skip the boring setup and jump straight into building cool stuff.

![screenshot](https://github.com/user-attachments/assets/278b23fd-46d0-4085-9170-45a8da140e6f)

## 🧠 why tho?

because every time you start a new project, you forget one step.
or five.
or all of them.

this template saves you from:

* repeating the same setup 900 times
* googling “rails api jwt devise setup” again
* crying over untracked .env files

## 🔧 stack

* ruby 3.3.1
* rails 7.2.2.1 (api-only)
* devise (user auth)
* jwt (hand-rolled, no devise-jwt dependency)
* rspec + rswag (for testing + swagger docs)
* dotenv (for managing secrets)
* rack-cors (so your frontend doesn’t scream)
* rack-attack (rate limiting — no room for brute force bots)

## 🧪 how to use this as a template

1. click the green **“Use this template”** button on the top-right
2. name your new repo (e.g. `my-next-api`)
3. clone it
4. run the setup:

```bash
bundle install
cp .env.example .env
rails db:create db:migrate
```

## ⚙️ or setup as a starter project

```bash
git clone https://github.com/yourname/rails-api-auth-template.git
cd rails-api-auth-template
bundle install
yarn install # (if needed)
cp .env.example .env
rails db:create db:migrate
```

## 🔐 auth flow

### signup

```bash
POST /api/v1/signup
{
  "email": "[email protected]",
  "password": "123456",
  "password_confirmation": "123456"
}
```

returns access token + refresh token + user json

### login

```bash
POST /api/v1/login
{
  "email": "[email protected]",
  "password": "123456"
}
```

returns access token + refresh token + user json

### refresh

```bash
POST /api/v1/refresh
{
  "refresh_token": "<your_refresh_token>"
}
```

returns a new access token (keeps you logged in without re-entering credentials)

### profile (protected)

```bash
GET /api/v1/profile
Authorization: Bearer <your_access_token>
```

returns current user

### logout

```bash
POST /api/v1/logout
Authorization: Bearer <your_access_token>
```

blacklists the current token (real logout, token becomes invalid)

## 👥 role-based authorization

users have roles: `user` (default), `moderator`, or `admin`

### example: admin-only endpoint

```bash
GET /api/v1/admin/dashboard
Authorization: Bearer <admin_access_token>
```

returns admin dashboard data (403 forbidden for non-admins)

### using roles in your controllers

```ruby
class MyController < ApplicationController
  include AuthorizeRequest
  include AuthorizeRole

  before_action :require_admin  # only admins
  # or
  before_action :require_moderator  # admins + moderators
end
```

## 🔒 security features

* **no secret fallbacks**: JWT_SECRET_KEY must be set (crashes if missing)
* **token blacklisting**: logout actually invalidates tokens
* **refresh tokens**: short-lived access tokens (1 hour) + long-lived refresh tokens (7 days)
* **rate limiting**: login, signup, and refresh endpoints are throttled
* **JTI tracking**: every token has a unique identifier for precise control
* **automatic cleanup**: expired tokens can be cleaned via scheduled jobs

## 📖 swagger ui

run:

```bash
RAILS_ENV=test bundle exec rake rswag:specs:swaggerize
rails s
```

open [http://localhost:3000/api-docs](http://localhost:3000/api-docs)

## 🧪 test

```bash
rspec
```

## 🚀 production considerations

### cleanup jobs

add these to your scheduled jobs (sidekiq, cron, etc):

```ruby
# clean up expired blacklisted tokens
BlacklistedToken.cleanup_expired

# clean up old refresh tokens
RefreshToken.cleanup_old_tokens
```

### environment variables

make sure to set these in production:

```bash
JWT_SECRET_KEY=your_super_secret_key_here_use_rails_secret
DATABASE_URL=your_database_url
REDIS_URL=your_redis_url (optional, for rack-attack)
```

### database indexes

migrations include proper indexes for performance:
* `blacklisted_tokens.jti` (unique)
* `blacklisted_tokens.exp`
* `refresh_tokens.token` (unique)
* `refresh_tokens.user_id + revoked`
* `users.role`

## 🤝 contribute

open to contributions, improvements, or just saying hi.
open issues or pull requests.

## ✨ features

* ✅ JWT authentication with secure token generation (includes JTI for tracking)
* ✅ Token blacklisting for real logout (tokens are invalidated on logout)
* ✅ Refresh tokens (7-day expiry, keeps users logged in securely)
* ✅ Role-based authorization (user, moderator, admin roles)
* ✅ Rate limiting with Rack::Attack (prevents brute force attacks)
* ✅ Comprehensive test coverage with RSpec
* ✅ Swagger API documentation via rswag
* ✅ Security best practices (no fallback secrets, proper validation)

## 🧼 todo

* add email confirmation for signup 📧
* add password reset functionality 🔑
* add remember me token (long-lived sessions) 💾
* add oauth providers (google, github, etc) 🔗

## 📢 shoutout

built to help devs like you (and me) avoid setup fatigue.
feel free to fork, star, share, or improve.

## ⚠️ disclaimer

this template includes production-grade features like token blacklisting, refresh tokens, and role-based auth.
however, you should still:
* review security settings for your specific use case
* set up proper monitoring and logging
* configure ssl/tls in production
* add email confirmation if needed
* implement proper error tracking

use responsibly and test thoroughly before deploying.

---

made with ♥ by rustam
Report a correction

Explore similar templates

Compare previews and descriptions within the same stack and category.