{"record":{"id":"098a346476b78db1","repo":"infiniflow/ragflow","slug":"user-username-not-found","errorCode":null,"errorMessage":"User '{username}' not found","messagePattern":"User '(.+?)' not found","errorType":"http","errorClass":"UserNotFoundError","httpStatus":404,"severity":"error","filePath":"admin/server/auth.py","lineNumber":141,"sourceCode":"        \"parser_ids\": settings.PARSERS,\n        \"img2txt_id\": settings.VISION_MDL,\n        \"rerank_id\": settings.RERANK_MDL,\n    }\n    usr_tenant = {\"tenant_id\": user_info[\"id\"], \"user_id\": user_info[\"id\"], \"invited_by\": user_info[\"id\"], \"role\": role}\n\n    # tenant_llm = get_init_tenant_llm(user_info[\"id\"])\n    TenantService.insert(**tenant)\n    UserTenantService.insert(**usr_tenant)\n    # TenantLLMService.insert_many(tenant_llm)\n    logging.info(f\"Added tenant for email: {user_info['email']}, A default tenant has been set; changing the default models after login is strongly recommended.\")\n\n\ndef check_admin_auth(func):\n    @wraps(func)\n    def wrapper(*args, **kwargs):\n        user = UserService.filter_by_id(current_user.id)\n        if not user:\n            raise UserNotFoundError(current_user.email)\n        if not user.is_superuser:\n            raise AdminException(\"Not admin\", 403)\n        if user.is_active == ActiveEnum.INACTIVE.value:\n            raise AdminException(f\"User {current_user.email} inactive\", 403)\n\n        return func(*args, **kwargs)\n\n    return wrapper\n\n\ndef login_admin(email: str, password: str):\n    \"\"\"\n    :param email: admin email\n    :param password: string before decrypt (RSA encrypted + base64 encoded)\n    \"\"\"\n    users = UserService.query(email=email)\n    if not users:\n        raise UserNotFoundError(email)","sourceCodeStart":123,"sourceCodeEnd":159,"githubUrl":"https://github.com/infiniflow/ragflow/blob/554fb1133ac3861732235ad9c377eb5e0a770665/admin/server/auth.py#L123-L159","documentation":"UserNotFoundError raised inside the check_admin_auth decorator (admin/server/auth.py:141) when flask-login's current_user.id no longer resolves to a row via UserService.filter_by_id. The request presented a structurally valid JWT/access token (load_user succeeded) but the user behind that UUID has since been deleted from the database. Note the message uses '{username}' although the lookup is by id.","triggerScenarios":"Any @check_admin_auth-decorated admin endpoint called with a token belonging to a deleted user: admin deleted the account (or DB was pruned) while the client kept using a previously issued access_token (get_uuid() stored on login, wrapped in a JWT that may still decode).","commonSituations":"Long-lived tokens in scripts/CLIs after the user row was removed; restoring a DB dump that lacks the user whose token a CI job holds; deleting a test admin account without revoking its tokens.","solutions":["Re-login to obtain a fresh token for an existing admin account.","If the user should still exist, restore its row in the user table (the id is embedded in the token's UUID).","On the server, treat UserNotFoundError as 401 so clients know to re-authenticate rather than retry the same token."],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":"from api.db.services import UserService\nuser = UserService.filter_by_id(user_id_from_token)\nif not user:\n    # token points at a deleted account: refresh credentials before calling admin APIs\n    relogin()","typeGuard":"def token_user_exists(user_id: str) -> bool:\n    return UserService.filter_by_id(user_id) is not None","tryCatchPattern":"from api.common.exceptions import UserNotFoundError\ntry:\n    admin_client.users.list()\nexcept UserNotFoundError:\n    # stored token references a deleted user; drop it and re-authenticate\n    admin_client.logout(); token = fresh_login()","preventionTips":["Re-login on 401-class errors instead of retrying with the same token.","When deleting users, revoke/invalidate their stored tokens in automation configs.","Prefer short-lived tokens in scripts so deletions self-heal at next login."],"tags":["authentication","admin","deleted-user","token"],"backgroundTag":null,"analyzedSha":"554fb1133ac3861732235ad9c377eb5e0a770665","analyzedAt":"2026-08-15T09:20:16.380Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}