From 2125c768417579ef69e0a85df33c0fedc2619e97 Mon Sep 17 00:00:00 2001 From: Anish Sarkar <104695310+AnishSarkar22@users.noreply.github.com> Date: Mon, 2 Feb 2026 19:03:05 +0530 Subject: [PATCH] feat: merge new credentials with existing connector configurations to preserve user settings --- .../app/connectors/google_drive/credentials.py | 7 ++++++- surfsense_backend/app/routes/composio_routes.py | 10 +++++++++- 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/surfsense_backend/app/connectors/google_drive/credentials.py b/surfsense_backend/app/connectors/google_drive/credentials.py index 5b1900ab2..951d8e3e3 100644 --- a/surfsense_backend/app/connectors/google_drive/credentials.py +++ b/surfsense_backend/app/connectors/google_drive/credentials.py @@ -127,7 +127,12 @@ async def get_valid_credentials( ) creds_dict["_token_encrypted"] = True - connector.config = creds_dict + # IMPORTANT: Merge new credentials with existing config to preserve + # user settings like selected_folders, selected_files, indexing_options, + # folder_tokens, etc. that would otherwise be wiped on token refresh. + existing_config = connector.config.copy() if connector.config else {} + existing_config.update(creds_dict) + connector.config = existing_config flag_modified(connector, "config") await session.commit() diff --git a/surfsense_backend/app/routes/composio_routes.py b/surfsense_backend/app/routes/composio_routes.py index a28361132..c0a23a665 100644 --- a/surfsense_backend/app/routes/composio_routes.py +++ b/surfsense_backend/app/routes/composio_routes.py @@ -20,6 +20,7 @@ from pydantic import ValidationError from sqlalchemy.exc import IntegrityError from sqlalchemy.ext.asyncio import AsyncSession from sqlalchemy.future import select +from sqlalchemy.orm.attributes import flag_modified from app.config import config from app.db import ( @@ -330,10 +331,17 @@ async def composio_callback( ) # Update existing connector with new connected_account_id + # IMPORTANT: Merge new credentials with existing config to preserve + # user settings like selected_folders, selected_files, indexing_options, + # drive_page_token, etc. that would otherwise be wiped on reconnection. logger.info( f"Updating existing Composio connector {existing_connector.id} with new connected_account_id {final_connected_account_id}" ) - existing_connector.config = connector_config + existing_config = existing_connector.config.copy() if existing_connector.config else {} + existing_config.update(connector_config) + existing_connector.config = existing_config + + flag_modified(existing_connector, "config") await session.commit() await session.refresh(existing_connector)