{"record":{"id":"8c93c3ab373f4bff","repo":"tiangolo/fastapi","slug":"inactive-user-8c93c3","errorCode":null,"errorMessage":"Inactive user","messagePattern":"Inactive user","errorType":"http","errorClass":"HTTPException","httpStatus":400,"severity":"error","filePath":"docs_src/security/tutorial004_py310.py","lineNumber":114,"sourceCode":"        headers={\"WWW-Authenticate\": \"Bearer\"},\n    )\n    try:\n        payload = jwt.decode(token, SECRET_KEY, algorithms=[ALGORITHM])\n        username = payload.get(\"sub\")\n        if username is None:\n            raise credentials_exception\n        token_data = TokenData(username=username)\n    except InvalidTokenError:\n        raise credentials_exception\n    user = get_user(fake_users_db, username=token_data.username)\n    if user is None:\n        raise credentials_exception\n    return user\n\n\nasync def get_current_active_user(current_user: User = Depends(get_current_user)):\n    if current_user.disabled:\n        raise HTTPException(status_code=400, detail=\"Inactive user\")\n    return current_user\n\n\n@app.post(\"/token\")\nasync def login_for_access_token(\n    form_data: OAuth2PasswordRequestForm = Depends(),\n) -> Token:\n    user = authenticate_user(fake_users_db, form_data.username, form_data.password)\n    if not user:\n        raise HTTPException(\n            status_code=status.HTTP_401_UNAUTHORIZED,\n            detail=\"Incorrect username or password\",\n            headers={\"WWW-Authenticate\": \"Bearer\"},\n        )\n    access_token_expires = timedelta(minutes=ACCESS_TOKEN_EXPIRE_MINUTES)\n    access_token = create_access_token(\n        data={\"sub\": user.username}, expires_delta=access_token_expires\n    )","sourceCodeStart":96,"sourceCodeEnd":132,"githubUrl":"https://github.com/tiangolo/fastapi/blob/3e8d1526d83a90aaf7d6eb6dc682bf150f180b25/docs_src/security/tutorial004_py310.py#L96-L132","documentation":"Legacy-DI variant of error 47 in tutorial004. After a valid JWT resolves to a real user, get_current_active_user raises HTTP 400 'Inactive user' when current_user.disabled is truthy. Only the DI syntax differs from the Annotated version; the semantics are identical.","triggerScenarios":"Mint a valid JWT for a disabled user, then call GET /users/me/ — JWT decodes, user loads, then line 113 raises.","commonSituations":"Suspended accounts in production; a disabled fixture user used by mistake.","solutions":["Use a user with disabled=False (johndoe in the shipped fixture).","Set disabled=False and re-login.","Prefer 403 Forbidden over 400 for the disabled-but-authenticated case."],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":"import jwt\ndef user_disabled_for_token(token: str, db: dict) -> bool:\n    payload = jwt.decode(token, SECRET_KEY, algorithms=[\"HS256\"])\n    u = db.get(payload.get(\"sub\"))\n    return bool(u and u.get(\"disabled\"))","typeGuard":"from typing import TypeGuard\ndef is_active_user(u: dict) -> TypeGuard[dict]:\n    return not u.get(\"disabled\", False)","tryCatchPattern":"import httpx\ntry:\n    r = httpx.get(\"/users/me/\", headers={\"Authorization\": f\"Bearer {token}\"})\n    r.raise_for_status()\nexcept httpx.HTTPStatusError as e:\n    if e.response.status_code in (400, 403) and \"Inactive\" in e.response.text:\n        prompt_reactivation()","preventionTips":["Keep JWT TTLs short so disabled state is re-checked quickly.","Revoke tokens on disable.","Use 403 Forbidden for disabled-but-authenticated users."],"tags":["fastapi","authentication","jwt","authorization","python"],"backgroundTag":null,"analyzedSha":"3e8d1526d83a90aaf7d6eb6dc682bf150f180b25","analyzedAt":"2026-08-11T02:34:52.986Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}