Login, tokens,
and roles.
Wired in one call.
FastAuth adds JWT access and refresh tokens, role-based access control, and ready-made login routes to any FastAPI + SQLModel app. Call auth.setup(app) and start protecting routes.
v0.7.0 · MIT · tested on Python 3.10–3.14
Features
Everything auth, nothing extra
FastAuth covers the auth work every FastAPI app repeats: token issuing, password hashing, session cookies, and role checks. Your code stays about your product.
One call mounts everything
Login, refresh, registration, logout, password reset, email verification, and the role API, all wired at once.
19 routes across auth, account flows, and role management, plus the error handlers.
Two tokens, two lifetimes
A short-lived access token for API calls, a long-lived refresh token so users are not asked to log in every half hour.
/token/refresh mints a new access token without asking for the password again.
Roles that read like English
Six standard roles out of the box. Protect a single route or a whole router.
- auth.roles(...)
- any of these roles
- auth.all_roles(...)
- all of these roles
- auth.admin
- the admin shortcut
Cookies with the right flags
An HTTP-only cookie that expires with its token. A Bearer header wins when both are present.
- HttpOnly
- JavaScript can never read it
- Secure
- HTTPS only, default in production
- SameSite=lax
- blocks cross-site sends
Errors in one shape
Every auth failure returns the same JSON with a machine-readable code, so clients handle errors once.
- code
- FASTAUTH_INVALID_CREDENTIALS
- message
- Incorrect username or password
- status_code
- 401
Account flows
Password reset, change, and email verification, with delivery hooks for your email service.
Production mode
production=True enforces a strong secret, secure cookies, and no default passwords.
CLI initialization
fastauth app.py creates tables, the six standard roles, and a superadmin.
SQLModel native
Bring your own engine and user model. No separate auth database, no second source of truth.
bcrypt hashing
Hashes with bcrypt directly, compatible with bcrypt 4 and 5, with no passlib dependency to keep pinned.
Token revocation
/logout/all invalidates every session, and password changes do it automatically.
Quick start
A working auth system in three steps
Login, refresh tokens, registration, roles, and protected routes. Run uv run fastapi dev main.py and every endpoint is in /docs, documented and ready to try.
Install the package
One dependency, plus FastAPI's standard extras for the dev server.
Wire FastAuth into your app
Point it at your engine and session. One call mounts every route and the error handlers.
Protect your routes
Depend on the current user, a set of roles, or the admin shortcut.
Stop rewriting auth
Install it, call auth.setup(app), and get back to the part of your app that is actually yours.