Skip to content

Open-source LLM knowledge platform: turn raw documents into a queryable RAG, an autonomous reasoning agent, and a self-maintaining Wiki.

fix(frontend): keep X-Tenant-ID override when switching back to home

wizardchenbccc27b1621c
diff --git a/frontend/src/components/TenantSelector.vue b/frontend/src/components/TenantSelector.vue
index 68cf215cb..f4519fd9c 100644
--- a/frontend/src/components/TenantSelector.vue
+++ b/frontend/src/components/TenantSelector.vue
@@ -103,7 +103,12 @@ const loading = ref(false)
const searchTimer = ref<number | null>(null)
const selectedTenantId = computed(() => authStore.selectedTenantId)
-const defaultTenantId = computed(() => authStore.tenant?.id ? Number(authStore.tenant.id) : null)
+// home 租户 id 来自 user.tenant_id(注册时分配、永不变)。不要读
+// authStore.tenant.id —— 那是当前激活租户,会随 X-Tenant-ID 切换;用它
+// 当 home 会让「切回 home」分支错判,详见 useHomeTenant() 注释。
+const defaultTenantId = computed(() =>
+ authStore.user?.tenant_id ? Number(authStore.user.tenant_id) : null,
+)
const currentTenantId = computed(() => {
return selectedTenantId.value || defaultTenantId.value
@@ -164,15 +169,21 @@ const selectTenant = (tenantId: number) => {
// 找到选中的租户信息
const selectedTenant = tenants.value.find(t => t.id === tenantId)
+ // 始终写入 override,让 request.ts 永远附 X-Tenant-ID 覆盖 JWT;不要因为
+ // 切到 home 就清空(详见 UserMenu.switchToTenant 同名注释)。服务端持久化
+ // 偏好仍然区分对待——切到 home 时清 last_active,让下次干净重登回到 home。
const switchingToHome = tenantId === defaultTenantId.value
- if (switchingToHome) {
- authStore.setSelectedTenant(null, null)
- } else {
- authStore.setSelectedTenant(tenantId, selectedTenant?.name || null)
- }
+ // 切到 home 时,selectedTenant 可能因为分页 / 搜索没把 home 加载进列表,
+ // 退而求其次从 memberships 上挑名字。注意不要回退到 authStore.tenant?.name
+ // —— 那是当前激活租户的名字,在 active != home 的会话里就是 peer 的名字。
+ const homeNameFallback = switchingToHome
+ ? (authStore.memberships ?? []).find((m) => Number(m.tenant_id) === tenantId)?.tenant_name
+ || null
+ : null
+ authStore.setSelectedTenant(tenantId, selectedTenant?.name || homeNameFallback || null)
closeDropdown()
const displayName = selectedTenant?.name
- || (tenantId === defaultTenantId.value ? authStore.tenant?.name : null)
+ || homeNameFallback
|| `#${tenantId}`
// Cross-tenant superusers may not have a membership row in the target
// tenant; in that case skip the role line rather than show a misleading
diff --git a/frontend/src/components/UserMenu.vue b/frontend/src/components/UserMenu.vue
index 7c44fdb6a..d2ed22d98 100644
--- a/frontend/src/components/UserMenu.vue
+++ b/frontend/src/components/UserMenu.vue
@@ -440,16 +440,15 @@ const switchToTenant = (m: Membership) => {
closeAll()
return
}
- // Treat switching back to the user's home tenant as "clear the
- // override" so request.ts stops attaching X-Tenant-ID. This mirrors
- // what TenantSelector.vue does in selectTenant().
+ // 始终把激活租户写进 selectedTenantId,让 request.ts 永远附 X-Tenant-ID。
+ // 历史实现里「切回 home 就清 override」会让请求落回 JWT 编码的租户,
+ // 而 JWT 在 last_active != home 的会话里恰好是 peer 租户(见
+ // userService.resolveLoginTenantID),结果切回 home 反而原地不动。
+ // 服务端持久化偏好仍然按 home/peer 区分:home 时清空 last_active,
+ // 让下次干净重登能正确回到 home。
const home = homeTenantId.value
const switchingToHome = home !== null && home === m.tenant_id
- if (switchingToHome) {
- authStore.setSelectedTenant(null, null)
- } else {
- authStore.setSelectedTenant(m.tenant_id, tenantDisplayName(m))
- }
+ authStore.setSelectedTenant(m.tenant_id, tenantDisplayName(m))
closeAll()
// Toast 在 reload 后由 App.vue 弹出(直接在这里弹会被 hard reload 干掉)。
stashTenantSwitchToast({