tiangolo/fastapi · error · HTTPException

Inactive user

Error message

Inactive user

What it means

In the JWT-based tutorial, `get_current_active_user` raises HTTP 400 'Inactive user' when `current_user.disabled` is True. The token has already been decoded and the user fetched from `fake_users_db`, so the JWT itself is valid - the account is just disabled.

Source

Thrown at docs_src/security/tutorial004_an_py310.py:117

    try:
        payload = jwt.decode(token, SECRET_KEY, algorithms=[ALGORITHM])
        username = payload.get("sub")
        if username is None:
            raise credentials_exception
        token_data = TokenData(username=username)
    except InvalidTokenError:
        raise credentials_exception
    user = get_user(fake_users_db, username=token_data.username)
    if user is None:
        raise credentials_exception
    return user


async def get_current_active_user(
    current_user: Annotated[User, Depends(get_current_user)],
):
    if current_user.disabled:
        raise HTTPException(status_code=400, detail="Inactive user")
    return current_user


@app.post("/token")
async def login_for_access_token(
    form_data: Annotated[OAuth2PasswordRequestForm, Depends()],
) -> Token:
    user = authenticate_user(fake_users_db, form_data.username, form_data.password)
    if not user:
        raise HTTPException(
            status_code=status.HTTP_401_UNAUTHORIZED,
            detail="Incorrect username or password",
            headers={"WWW-Authenticate": "Bearer"},
        )
    access_token_expires = timedelta(minutes=ACCESS_TOKEN_EXPIRE_MINUTES)
    access_token = create_access_token(
        data={"sub": user.username}, expires_delta=access_token_expires
    )

View on GitHub (pinned to 42a41db11f)

Solutions

  1. Log in as an active user to get a fresh JWT.
  2. Set the resolved user's `disabled` field to `False`.
  3. Inform the user the account is disabled; do not silently retry the same token.

Example fix

# before
# JWT 'sub' -> a user with disabled: True  => 400
# after
# JWT 'sub' -> a user with disabled: False => 200
Defensive patterns

Strategy: try-catch

Validate before calling

None

Type guard

def is_inactive(resp) -> bool:
    return getattr(resp, 'status_code', None) == 400 and resp.json().get('detail') == 'Inactive user'

Try / catch

try:
    me = client.get('/users/me/', headers=auth_header(token))
    me.raise_for_status()
except httpx.HTTPStatusError as e:
    if e.response.status_code == 400 and e.response.json().get('detail') == 'Inactive user':
        prompt_reactivation()
    raise

Prevention

When it happens

Trigger: `GET /users/me/` (or `/users/me/items/`) with a valid JWT whose `sub` claim resolves to a disabled user. The sample DB only ships `johndoe` (`disabled: False`), so you trigger this only after adding/disabling a user.

Common situations: Admin deactivating an account while valid JWTs are still in the wild; the `disabled` flag not toggled on reactivation; test fixtures seeding disabled users.

Related errors


AI-assisted analysis of tiangolo/fastapi@42a41db11f (2026-08-04). Data as JSON: /data/errors/3baadc72a93d71de.json. Report an issue: GitHub.