{"id":"3baadc72a93d71de","repo":"tiangolo/fastapi","slug":"inactive-user-3baadc","errorCode":null,"errorMessage":"Inactive user","messagePattern":"Inactive user","errorType":"http","errorClass":"HTTPException","httpStatus":400,"severity":"error","filePath":"docs_src/security/tutorial004_an_py310.py","lineNumber":117,"sourceCode":"    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(\n    current_user: Annotated[User, Depends(get_current_user)],\n):\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: Annotated[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":99,"sourceCodeEnd":135,"githubUrl":"https://github.com/tiangolo/fastapi/blob/42a41db11f6882807ac3c057b942178d53b97438/docs_src/security/tutorial004_an_py310.py#L99-L135","documentation":"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.","triggerScenarios":"`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.","commonSituations":"Admin deactivating an account while valid JWTs are still in the wild; the `disabled` flag not toggled on reactivation; test fixtures seeding disabled users.","solutions":["Log in as an active user to get a fresh JWT.","Set the resolved user's `disabled` field to `False`.","Inform the user the account is disabled; do not silently retry the same token."],"exampleFix":"# before\n# JWT 'sub' -> a user with disabled: True  => 400\n# after\n# JWT 'sub' -> a user with disabled: False => 200","handlingStrategy":"try-catch","validationCode":"None","typeGuard":"def is_inactive(resp) -> bool:\n    return getattr(resp, 'status_code', None) == 400 and resp.json().get('detail') == 'Inactive user'","tryCatchPattern":"try:\n    me = client.get('/users/me/', headers=auth_header(token))\n    me.raise_for_status()\nexcept httpx.HTTPStatusError as e:\n    if e.response.status_code == 400 and e.response.json().get('detail') == 'Inactive user':\n        prompt_reactivation()\n    raise","preventionTips":["Treat 'Inactive user' as terminal for that token - do not retry.","Invalidate outstanding JWTs (rotate SECRET_KEY or use a denylist) when disabling users.","Keep the disabled flag authoritative in your admin tooling."],"tags":["authorization","security","jwt","user-management","fastapi"],"analyzedSha":"42a41db11f6882807ac3c057b942178d53b97438","analyzedAt":"2026-08-04T19:23:32.007Z","schemaVersion":2}