fix(dashboard): respect URL tenant org for multi-org students (#727)

* fix(dashboard): respect URL tenant org for multi-org students

When a student belongs to multiple organizations, setOrgStore was falling
back to localStorage or the first /account organization even on tenant
sites (subdomain or custom domain). That overwrote the server-resolved
org from the URL and sent the wrong cio-org-id on API calls.

Pin currentOrg to the URL tenant on org sites, preserve the server org
when membership is missing, sync org from /org/[slug] routes, and fix
the goToLMS single-org length check.

Co-authored-by: Rotimi Best <[email protected]>

* fix(dashboard): update currentOrg when switching tenant sites

Address Greptile P1: shouldSetPublicOrg only ran when the site name
matched, so navigating from one tenant subdomain to another left the
previous org in currentOrg. React to URL tenant changes in an effect
and re-pin via setOrgStore when account data is already loaded.

Co-authored-by: Rotimi Best <[email protected]>

* fix(dashboard): harden tenant sync when switching org sites

- Reset globalStore when leaving a tenant URL
- Split org-site seeding from account-backed tenant sync
- Add syncTenantOrgFromUrl with deduping and auto-enroll on switch
- Stop calling setOrgStore directly from the root layout

Co-authored-by: Rotimi Best <[email protected]>

* refactor(dashboard): consolidate org context sync in root layout

Address PR review: document why post-setup sync is needed, merge setupApp
and syncOrgContext into one effect, resolve /org/[slug] from the root
layout via resolveAppOrgParams, and remove duplicate sync from org slug layout.

Co-authored-by: Rotimi Best <[email protected]>

---------

Co-authored-by: Cursor Agent <[email protected]>
Co-authored-by: Rotimi Best <[email protected]>
This commit is contained in:
Rotimi Best
2026-07-11 09:17:45 +01:00
committed by GitHub
co-authored by Rotimi Best Cursor Agent
parent 0836d9b5bb
commit ce0dd376e6
3 changed files with 173 additions and 34 deletions
@@ -3,6 +3,7 @@ import { currentOrg, getOrgPublicUrl, mergeAccountOrgFromServer, orgs } from '$l
import { defaultProfileState, defaultUserState, profile, user } from '$lib/utils/store/user'; import { defaultProfileState, defaultUserState, profile, user } from '$lib/utils/store/user';
import type { AccountResponse } from './types'; import type { AccountResponse } from './types';
import { resolveAppOrgParams, type AppOrgParams } from './resolve-app-org-params';
import { PUBLIC_IS_SELFHOSTED } from '$env/static/public'; import { PUBLIC_IS_SELFHOSTED } from '$env/static/public';
import type { TUser } from '@cio/db/types'; import type { TUser } from '@cio/db/types';
import { authClient } from '$lib/utils/services/auth/client'; import { authClient } from '$lib/utils/services/auth/client';
@@ -23,12 +24,19 @@ import { setupAnalyticsBasedOnLicense } from '$lib/utils/functions/appSetup';
import shouldRedirectOnAuth from '$lib/utils/functions/routes/shouldRedirectOnAuth'; import shouldRedirectOnAuth from '$lib/utils/functions/routes/shouldRedirectOnAuth';
import { ROLE } from '@cio/utils/constants'; import { ROLE } from '@cio/utils/constants';
type AppSetupParams = { type AppSetupParams = AppOrgParams;
isOrgSite: boolean;
orgSiteName: string; function tenantSyncKey(params: AppSetupParams): string | null {
/** Tenant org id from `getOrgSiteInfo`; used to auto-enroll the user if they aren't a member yet. */ if (!params.orgSiteName) {
orgId?: string | null; return null;
}; }
if (params.isOrgSite) {
return `tenant:${params.orgSiteName}:${params.orgId ?? ''}`;
}
return `admin:${params.orgSiteName}`;
}
/* /*
Manages everything related to loading the logged in user and setting up the organization. Manages everything related to loading the logged in user and setting up the organization.
@@ -36,6 +44,7 @@ type AppSetupParams = {
class AppInitApi extends BaseApi { class AppInitApi extends BaseApi {
data = $state<AccountResponse>(null); data = $state<AccountResponse>(null);
session = $state<App.Locals | null>(null); session = $state<App.Locals | null>(null);
private syncedTenantKey: string | null = null;
get loading() { get loading() {
return this.isLoading; return this.isLoading;
@@ -81,7 +90,7 @@ class AppInitApi extends BaseApi {
}); });
} }
private async autoEnrollOnTenantSite(orgId: string): Promise<void> { private async autoEnrollOnTenantSite(orgId: string): Promise<boolean> {
try { try {
const response = await classroomio.organization['auto-enroll-student'].$post( const response = await classroomio.organization['auto-enroll-student'].$post(
{}, {},
@@ -93,18 +102,53 @@ class AppInitApi extends BaseApi {
// visited a tenant site but isn't allowed to enroll. Other failures // visited a tenant site but isn't allowed to enroll. Other failures
// shouldn't block the rest of setupApp. // shouldn't block the rest of setupApp.
console.warn('auto-enroll-student failed', response.status, await response.text().catch(() => '')); console.warn('auto-enroll-student failed', response.status, await response.text().catch(() => ''));
return; return false;
} }
// The user is a new member, so the session cookie cache (orgRoles) // The user is a new member, so the session cookie cache (orgRoles)
// is stale. Force Better Auth to refetch from DB and rewrite the // is stale. Force Better Auth to refetch from DB and rewrite the
// session_data cookie — Better Auth manages the cookie name itself. // session_data cookie — Better Auth manages the cookie name itself.
await authClient.getSession({ query: { disableCookieCache: true } }); await authClient.getSession({ query: { disableCookieCache: true } });
return true;
} catch (error) { } catch (error) {
console.warn('auto-enroll-student threw', error); console.warn('auto-enroll-student threw', error);
return false;
} }
} }
/**
* Re-pin org context after setupApp when the URL org changes.
*
* setupApp only runs once per session; tenant subdomains and `/org/[slug]`
* routes can change without another /account fetch. This keeps `currentOrg`
* aligned with the URL and attempts auto-enroll on org sites when needed.
*/
async syncOrgContext(params: AppSetupParams): Promise<void> {
if (!this.data?.success || !params.orgSiteName) {
return;
}
const nextTenantKey = tenantSyncKey(params);
if (nextTenantKey && nextTenantKey === this.syncedTenantKey) {
return;
}
const isMember = this.data.organizations.some((org) => org.siteName === params.orgSiteName);
if (params.isOrgSite && params.orgId && !isMember) {
const enrolled = await this.autoEnrollOnTenantSite(params.orgId);
if (enrolled) {
const response = await classroomio.account.$get();
const accountData = (await response.json()) as AccountResponse;
if (accountData?.success) {
this.data = accountData;
}
}
}
this.setOrgStore(params);
licenseApi.syncFromAccount(this.data.licenseFeatures, get(currentOrg));
}
/* /*
1. Update user store 1. Update user store
2. Update profile store 2. Update profile store
@@ -133,30 +177,60 @@ class AppInitApi extends BaseApi {
return; return;
} }
if (!this.data.organizations.length) {
return;
}
orgs.set(this.data.organizations.map((org) => mergeAccountOrgFromServer(org))); orgs.set(this.data.organizations.map((org) => mergeAccountOrgFromServer(org)));
// On a tenant site, pin currentOrg to that tenant — never fall back to const nextOrg = this.pickCurrentOrg(params);
// localStorage / the user's first org, which is what was making a user
// logged in on dblocked.* see the Dblocked dashboard on ciodevs.*.
let nextOrg: (typeof this.data.organizations)[number] | undefined;
if (params?.isOrgSite && params.orgSiteName) {
nextOrg = this.data.organizations.find((org) => org.siteName === params.orgSiteName);
}
if (!nextOrg) { if (!nextOrg) {
const lastOrgSiteName = localStorage.getItem('classroomio_org_sitename'); return;
nextOrg = this.data.organizations.find((org) => org.siteName === lastOrgSiteName) ?? this.data.organizations[0];
} }
currentOrg.set(mergeAccountOrgFromServer(nextOrg)); currentOrg.set(mergeAccountOrgFromServer(nextOrg));
if (nextOrg.siteName) {
localStorage.setItem('classroomio_org_sitename', nextOrg.siteName);
}
const theme = get(currentOrg)?.theme; const theme = get(currentOrg)?.theme;
setTheme(theme || 'blue'); setTheme(theme || 'blue');
this.syncedTenantKey = tenantSyncKey(params ?? { isOrgSite: false, orgSiteName: '' });
}
/**
* Prefer the tenant from the current URL on org sites. Only fall back to
* localStorage / the first account org on the admin host (`app.*`).
*/
private pickCurrentOrg(params?: AppSetupParams) {
const organizations = this.data?.success ? this.data.organizations : [];
const urlResolvedOrg = get(currentOrg);
if (params?.orgSiteName) {
const membership = organizations.find((org) => org.siteName === params.orgSiteName);
if (membership) {
return membership;
}
if (params.isOrgSite) {
// Keep the server-resolved tenant org (set from URL in +layout.svelte) instead
// of switching to another membership from /account.
if (urlResolvedOrg.siteName === params.orgSiteName && urlResolvedOrg.id) {
return urlResolvedOrg;
}
if (params.orgId && urlResolvedOrg.id === params.orgId) {
return urlResolvedOrg;
}
return undefined;
}
}
if (!organizations.length) {
return undefined;
}
const lastOrgSiteName = localStorage.getItem('classroomio_org_sitename');
return organizations.find((org) => org.siteName === lastOrgSiteName) ?? organizations[0];
} }
routeUserToNextPage({ isOrgSite }: AppSetupParams) { routeUserToNextPage({ isOrgSite }: AppSetupParams) {
@@ -216,7 +290,7 @@ class AppInitApi extends BaseApi {
const isCloud = PUBLIC_IS_SELFHOSTED !== 'true'; const isCloud = PUBLIC_IS_SELFHOSTED !== 'true';
const isStudent = get(isOrgStudent); const isStudent = get(isOrgStudent);
const selectedOrg = get(currentOrg); const selectedOrg = get(currentOrg);
const userHasOneOrg = this.data.organizations === 1; const userHasOneOrg = this.data.organizations.length === 1;
const userIsStudentInAllOrgs = this.data.organizations.every((org) => org.roleId === ROLE.STUDENT); const userIsStudentInAllOrgs = this.data.organizations.every((org) => org.roleId === ROLE.STUDENT);
const userIsAdminInAtleastOneOrg = this.data.organizations.some((org) => org.roleId === ROLE.ADMIN); const userIsAdminInAtleastOneOrg = this.data.organizations.some((org) => org.roleId === ROLE.ADMIN);
@@ -270,6 +344,7 @@ class AppInitApi extends BaseApi {
reset() { reset() {
super.reset(); super.reset();
this.data = null; this.data = null;
this.syncedTenantKey = null;
licenseApi.reset(); licenseApi.reset();
user.set(defaultUserState); user.set(defaultUserState);
@@ -0,0 +1,36 @@
export type AppOrgParams = {
isOrgSite: boolean;
orgSiteName: string;
/** Tenant org id from `getOrgSiteInfo`; used to auto-enroll on org sites. */
orgId?: string | null;
};
type LayoutOrgData = {
isOrgSite: boolean;
orgSiteName: string;
org: { id?: string } | null;
};
/**
* Resolve which organization the dashboard should treat as "current" for API calls.
*
* - Org sites: subdomain/custom domain from `getOrgSiteInfo`.
* - App host: `/org/[slug]` when the user is in the admin dashboard.
*/
export function resolveAppOrgParams(layoutData: LayoutOrgData, pathname: string, slugParam?: string): AppOrgParams {
if (layoutData.isOrgSite) {
return {
isOrgSite: true,
orgSiteName: layoutData.orgSiteName,
orgId: layoutData.org?.id ?? null
};
}
const adminOrgSlug = pathname.startsWith('/org/') && slugParam && slugParam !== '*' ? slugParam : '';
return {
isOrgSite: false,
orgSiteName: adminOrgSlug,
orgId: null
};
}
+39 -11
View File
@@ -4,9 +4,11 @@
import { Snackbar } from '$features/ui'; import { Snackbar } from '$features/ui';
import { appInitApi } from '$features/app/init.svelte'; import { appInitApi } from '$features/app/init.svelte';
import { resolveAppOrgParams } from '$features/app/resolve-app-org-params';
import { setupCloudAnalytics } from '$lib/utils/functions/appSetup'; import { setupCloudAnalytics } from '$lib/utils/functions/appSetup';
import { globalStore } from '$lib/utils/store/app'; import { globalStore } from '$lib/utils/store/app';
import { currentOrg, mergeAccountOrgFromServer } from '$lib/utils/store/org'; import { currentOrg, mergeAccountOrgFromServer } from '$lib/utils/store/org';
import { get } from 'svelte/store';
import { user } from '$lib/utils/store/user'; import { user } from '$lib/utils/store/user';
import { setTheme } from '$lib/utils/functions/theme'; import { setTheme } from '$lib/utils/functions/theme';
import { authClient } from '$lib/utils/services/auth/client'; import { authClient } from '$lib/utils/services/auth/client';
@@ -42,26 +44,52 @@
currentSession: data.locals.user currentSession: data.locals.user
}); });
} }
});
if (data.isOrgSite && data.org) { $effect(() => {
$globalStore.orgSiteName = data.orgSiteName || ''; if (!data.isOrgSite || !data.org) {
$globalStore.isOrgSite = true; $globalStore.isOrgSite = false;
currentOrg.set(mergeAccountOrgFromServer(data.org)); $globalStore.orgSiteName = '';
setTheme(data.org.theme || 'blue'); return;
} }
$globalStore.orgSiteName = data.orgSiteName || '';
$globalStore.isOrgSite = true;
const existingOrg = get(currentOrg);
const shouldSetPublicOrg =
!existingOrg.id || existingOrg.siteName !== data.org.siteName || existingOrg.roleId === 0;
if (shouldSetPublicOrg) {
currentOrg.set(mergeAccountOrgFromServer(data.org));
}
setTheme(data.org.theme || 'blue');
}); });
const session = authClient.useSession(); const session = authClient.useSession();
const isSessionReady = $derived(!$session.isPending && !$session.isRefetching && $session.data); const isSessionReady = $derived(!$session.isPending && !$session.isRefetching && $session.data);
const appOrgParams = $derived(resolveAppOrgParams(data, page.url.pathname, page.params.slug));
/*
Auth + org context for the whole dashboard.
setupApp runs once per session to load /account. After that, org context can
still change when a logged-in user navigates to a different tenant subdomain
or opens another /org/[slug] on the app host — without another setupApp run.
syncOrgContext re-pins currentOrg from the URL + cached account data.
*/
$effect(() => { $effect(() => {
if (isSessionReady && !appInitApi.isInitializedAndReady && !appInitApi.loading) { if (!isSessionReady || appInitApi.loading) {
appInitApi.setupApp($session.data as App.Locals, { return;
isOrgSite: data.isOrgSite,
orgSiteName: data.orgSiteName,
orgId: data.org?.id ?? null
});
} }
if (!appInitApi.isInitializedAndReady) {
appInitApi.setupApp($session.data as App.Locals, appOrgParams);
return;
}
void appInitApi.syncOrgContext(appOrgParams);
}); });
</script> </script>