Long-lived refresh tokens are the weak point of JWT auth: if one leaks, the attacker can mint access tokens until it expires. Rotation plus reuse detection limits the damage. Here is a compact implementation with FastAPI and async SQLAlchemy.
The idea
Every refresh token has a unique id ( jti ) and belongs to a family , which is created at login.
Each refresh token is single use . Calling /refresh revokes the old token and issues a new one in the same family.
If a token that was already revoked shows up again, someone replayed it (the legitimate client or the thief used it first). Revoke the whole family , which logs out both.
The model
The server keeps a record of every issued refresh token.…