feat(tenant): remember user's last-active workspace across logins
Until now a fresh login (new device, expired refresh token, cleared
browser) always dropped the user into their home tenant, even if they
spend most of their time in a peer workspace. The session-local
X-Tenant-ID override in localStorage gave a "same browser sticky"
effect, but never crossed devices or new sessions.
This adds a small server-side preference, `users.preferences.
last_active_tenant_id`, persisted in the existing jsonb column (no
new migration), and threads it through:
* Backend
* `UserPreferences` gains `LastActiveTenantID *uint64` with sentinel
semantics (`*0` from the PATCH endpoint = clear preference).
* `resolveLoginTenantID` validates the stored id (tenant still
exists + active membership, or CanAccessAllTenants) and falls
back to home on any failure, best-effort clearing the stale
preference so subsequent logins don't pay for it again.
* `Login` and `LoginWithOIDC` resolve once and use the result for
both the JWT `tenant_id` claim and the returned `active_tenant`,
keeping the two in sync. `RefreshToken` rides through
`GenerateTokens` so refresh rotations also land the user back in
their preferred tenant instead of bouncing to home.
* `UpdateUserPreferences` learns to merge the new key.
* `PUT /auth/me/preferences` accepts the new field.
* Frontend
* `Login.vue` now writes the user's HOME tenant id into
`user.tenant_id` (matching the field's documented semantics) and
expresses any active-vs-home divergence via `setSelectedTenant`,
so `useHomeTenant` and the "current"/"home" badges stay correct
after the backend honours a remembered preference. `App.vue`'s
OIDC sync does the same reconciliation.
* `TenantSelector`, `UserMenu` and the post-tenant-create handlers
fire `persistLastActiveTenantPreference` after every successful
user-initiated switch (switching to home sends `0` to clear).
The call is raced against the existing reload-grace window so
most writes finish before the page tears down; lost writes are
recoverable on the next switch.
No new UI. Users will simply notice that, after re-logging in on
another device, they land back in the workspace they were last
using rather than always in their home tenant.
Note: `make docs` is unrelated-broken on `main` (audit_log.go
references `errors.AppError` which swag can't resolve), so the
Swagger artifacts under docs/ are intentionally not regenerated
in this PR. The handler code is the source of truth.
This commit is contained in:
@@ -76,7 +76,11 @@ import { useAuthStore } from '@/stores/auth'
|
||||
import { searchTenants, type TenantInfo } from '@/api/tenant'
|
||||
import { useI18n } from 'vue-i18n'
|
||||
import { MessagePlugin } from 'tdesign-vue-next'
|
||||
import { navigateAfterTenantSwitch, stashTenantSwitchToast } from '@/utils/tenantSwitch'
|
||||
import {
|
||||
navigateAfterTenantSwitch,
|
||||
persistLastActiveTenantPreference,
|
||||
stashTenantSwitchToast,
|
||||
} from '@/utils/tenantSwitch'
|
||||
import CreateTenantDialog from '@/components/CreateTenantDialog.vue'
|
||||
import { useRoleLabel } from '@/composables/useRoleLabel'
|
||||
|
||||
@@ -160,7 +164,8 @@ const selectTenant = (tenantId: number) => {
|
||||
// 找到选中的租户信息
|
||||
const selectedTenant = tenants.value.find(t => t.id === tenantId)
|
||||
|
||||
if (tenantId === defaultTenantId.value) {
|
||||
const switchingToHome = tenantId === defaultTenantId.value
|
||||
if (switchingToHome) {
|
||||
authStore.setSelectedTenant(null, null)
|
||||
} else {
|
||||
authStore.setSelectedTenant(tenantId, selectedTenant?.name || null)
|
||||
@@ -176,10 +181,13 @@ const selectTenant = (tenantId: number) => {
|
||||
const roleLabel = membership ? formatRole(membership.role) : ''
|
||||
// Toast 在 reload 后由 App.vue 弹出(直接在这里弹会被 hard reload 干掉)。
|
||||
stashTenantSwitchToast({ name: displayName, role: roleLabel || undefined })
|
||||
// 切换租户后跳转到新租户下安全的入口(详见 tenantSwitch.ts 注释)。
|
||||
setTimeout(() => {
|
||||
navigateAfterTenantSwitch()
|
||||
}, 500)
|
||||
// Persist "last active tenant" preference (switching to home clears
|
||||
// it). Fire-and-forget, but race it against the existing 500ms grace
|
||||
// window so most writes finish before the hard reload tears the page
|
||||
// down. 切换租户后跳转到新租户下安全的入口(详见 tenantSwitch.ts 注释)。
|
||||
const persist = persistLastActiveTenantPreference(switchingToHome ? null : tenantId)
|
||||
Promise.race([persist, new Promise((r) => setTimeout(r, 500))])
|
||||
.finally(() => navigateAfterTenantSwitch())
|
||||
}
|
||||
|
||||
const loadTenants = async (append = false) => {
|
||||
@@ -265,9 +273,11 @@ const onTenantCreated = async (newTenant: TenantInfo) => {
|
||||
authStore.setAllTenants(tenants.value)
|
||||
await authStore.refreshFromAuthMe()
|
||||
authStore.setSelectedTenant(newTenant.id, newTenant.name)
|
||||
setTimeout(() => {
|
||||
navigateAfterTenantSwitch()
|
||||
}, 300)
|
||||
// Newly-created tenant becomes the user's "last active" so re-login
|
||||
// lands here. Race against the existing grace window before reload.
|
||||
const persist = persistLastActiveTenantPreference(newTenant.id)
|
||||
Promise.race([persist, new Promise((r) => setTimeout(r, 300))])
|
||||
.finally(() => navigateAfterTenantSwitch())
|
||||
}
|
||||
|
||||
onMounted(() => {
|
||||
|
||||
@@ -236,7 +236,11 @@ import { useI18n } from 'vue-i18n'
|
||||
import IMChannelsOverviewPanel from '@/components/IMChannelsOverviewPanel.vue'
|
||||
import CreateTenantDialog from '@/components/CreateTenantDialog.vue'
|
||||
import { listAllIMChannels, type IMChannelOverview } from '@/api/agent'
|
||||
import { navigateAfterTenantSwitch, stashTenantSwitchToast } from '@/utils/tenantSwitch'
|
||||
import {
|
||||
navigateAfterTenantSwitch,
|
||||
persistLastActiveTenantPreference,
|
||||
stashTenantSwitchToast,
|
||||
} from '@/utils/tenantSwitch'
|
||||
import type { TenantInfo } from '@/api/tenant'
|
||||
import { useRoleLabel, useHomeTenant } from '@/composables/useRoleLabel'
|
||||
|
||||
@@ -381,9 +385,9 @@ const openCreateTenantDialog = () => {
|
||||
const onTenantCreated = async (newTenant: TenantInfo) => {
|
||||
await authStore.refreshFromAuthMe()
|
||||
authStore.setSelectedTenant(newTenant.id, newTenant.name)
|
||||
setTimeout(() => {
|
||||
navigateAfterTenantSwitch()
|
||||
}, 300)
|
||||
const persist = persistLastActiveTenantPreference(newTenant.id)
|
||||
Promise.race([persist, new Promise((r) => setTimeout(r, 300))])
|
||||
.finally(() => navigateAfterTenantSwitch())
|
||||
}
|
||||
|
||||
// ---------- Tenant switcher submenu ----------
|
||||
@@ -440,7 +444,8 @@ const switchToTenant = (m: Membership) => {
|
||||
// override" so request.ts stops attaching X-Tenant-ID. This mirrors
|
||||
// what TenantSelector.vue does in selectTenant().
|
||||
const home = homeTenantId.value
|
||||
if (home !== null && home === m.tenant_id) {
|
||||
const switchingToHome = home !== null && home === m.tenant_id
|
||||
if (switchingToHome) {
|
||||
authStore.setSelectedTenant(null, null)
|
||||
} else {
|
||||
authStore.setSelectedTenant(m.tenant_id, tenantDisplayName(m))
|
||||
@@ -451,14 +456,15 @@ const switchToTenant = (m: Membership) => {
|
||||
name: tenantDisplayName(m),
|
||||
role: formatRole(m.role) || undefined,
|
||||
})
|
||||
// Hard reload so every cached store / open SSE stream / in-flight
|
||||
// request gets re-keyed under the new tenant. If the current path
|
||||
// embeds a tenant-scoped resource id, reload would white-screen the
|
||||
// user; navigateAfterTenantSwitch redirects to the platform home in
|
||||
// that case. Same helper as TenantSelector.vue.
|
||||
setTimeout(() => {
|
||||
navigateAfterTenantSwitch()
|
||||
}, 400)
|
||||
// Persist "last active tenant" preference (switching to home clears
|
||||
// it). Hard reload so every cached store / open SSE stream / in-flight
|
||||
// request gets re-keyed under the new tenant; navigateAfterTenantSwitch
|
||||
// redirects to the platform home so tenant-scoped resource paths don't
|
||||
// white-screen. Race the persist against the existing 400ms grace
|
||||
// window so most writes complete before the page tears down.
|
||||
const persist = persistLastActiveTenantPreference(switchingToHome ? null : m.tenant_id)
|
||||
Promise.race([persist, new Promise((r) => setTimeout(r, 400))])
|
||||
.finally(() => navigateAfterTenantSwitch())
|
||||
}
|
||||
|
||||
let lastTenantSubmenuMembershipRefresh = 0
|
||||
|
||||
Reference in New Issue
Block a user