GitHub X (Twitter) LinkedIn
← All projects
FastAPI & RBAC

RBAC Auth

A FastAPI authentication and authorization template for multi tenant apps: declarative role guards, rotating refresh tokens with reuse detection, Redis sessions and async SQLAlchemy migrations.

RoleDesign + Build StatusOpen source TypeBackend template
RBAC Auth request flow through roles, Redis and PostgreSQL

Overview

RBAC Auth is a starting point you fork and tailor: keep the auth and role skeleton, swap the domain models. Authorization is declared on the route with @Roles(...) and enforced by RolesGuard, so adding a protected endpoint never means editing the guard itself.

POST /auth/login -> access + refresh pair POST /auth/refresh -> rotate the pair (reuse revokes every session) GET /admin/... -> @Roles(ADMIN) + RolesGuard Redis -> session set, TTL eviction, access revocation PostgreSQL -> users, roles, refresh tokens, email log

Key features

  • Declarative roles@Roles(Role.ADMIN) plus RolesGuard; no inline permission checks in controllers.
  • Refresh rotationOne time refresh tokens with reuse detection; reuse invalidates every session for that user.
  • Redis sessionsNative TTL eviction, access token revocation, logout and logout all.
  • Roles as dataA roles table and a many to many user_roles, seeded with ADMIN and USER.
  • Email verificationHashed verification tokens and an EmailLog state machine.
  • Migrations + CIAlembic autogenerate, ruff + pre-commit, and pytest with an 80% coverage gate.

How it works

  1. Sync dependencies with uv and copy .env.example to .env.
  2. Run alembic upgrade head against PostgreSQL.
  3. Bootstrap the first admin (environment mode, or the hidden HTTP route).
  4. Start the app and sign in; the Scalar docs live at /docs.
uv sync --all-groups alembic upgrade head uv run uvicorn app.main:app --reload # endpoints are mounted under /api/v1

Stack

FastAPI SQLAlchemy 2 async PostgreSQL Redis Alembic bcrypt PyJWT Scalar uv pytest

Have a project in mind?

Email me
Have a project? Let's talk