diff --git a/apps/dashboard/src/lib/features/app/init.svelte.ts b/apps/dashboard/src/lib/features/app/init.svelte.ts index 9d35ffe9..2be743d2 100644 --- a/apps/dashboard/src/lib/features/app/init.svelte.ts +++ b/apps/dashboard/src/lib/features/app/init.svelte.ts @@ -1,5 +1,5 @@ import { BaseApi, classroomio } from '$lib/utils/services/api'; -import { currentOrg, mergeAccountOrgFromServer, orgs } from '$lib/utils/store/org'; +import { currentOrg, getOrgPublicUrl, mergeAccountOrgFromServer, orgs } from '$lib/utils/store/org'; import { defaultProfileState, defaultUserState, profile, user } from '$lib/utils/store/user'; import type { AccountResponse } from './types'; @@ -201,12 +201,26 @@ class AppInitApi extends BaseApi { if (!shouldRedirectOnAuth(page.url.pathname)) return; + // Students with existing org memberships who open /onboarding may intend to create their own academy. + if (path.includes('/onboarding') && isStudent && userHasOrganizations) { + return; + } + const shouldGoToLMS = isCloud ? isOrgSite || !!isStudent : !!isStudent; console.log('redirecting to', shouldGoToLMS ? 'lms' : 'org'); - return shouldGoToLMS ? this.goToLMS() : this.goToOrg(); + return shouldGoToLMS ? this.goToLMS(isOrgSite) : this.goToOrg(); } - goToLMS() { + goToLMS(isOrgSite: boolean) { + const isCloud = PUBLIC_IS_SELFHOSTED !== 'true'; + const isStudent = get(isOrgStudent); + const selectedOrg = get(currentOrg); + + if (isCloud && !isOrgSite && isStudent && selectedOrg.siteName) { + window.location.href = getOrgPublicUrl(selectedOrg, '/lms'); + return; + } + goto(resolve('/lms', {})); } diff --git a/apps/dashboard/src/lib/utils/store/org.ts b/apps/dashboard/src/lib/utils/store/org.ts index 2fdf0349..d26ad994 100644 --- a/apps/dashboard/src/lib/utils/store/org.ts +++ b/apps/dashboard/src/lib/utils/store/org.ts @@ -104,6 +104,26 @@ export function getOrgPublicOrigin(org: OrgPublicOrigin): string { return browser ? window.location.origin : ''; } +/** Absolute URL on an org's public tenant site (e.g. `/lms`, `/course/{slug}`). Client-only. */ +export function getOrgPublicUrl(org: OrgPublicOrigin, pathname = '/'): string { + if (!browser) { + return pathname; + } + + const origin = getOrgPublicOrigin(org); + if (!origin) { + return pathname; + } + + const url = new URL(pathname, origin); + + if (window.location.host.includes('localhost') && org.siteName) { + url.searchParams.set('org', org.siteName); + } + + return url.toString(); +} + export const currentOrgDomain = derived(currentOrg, ($currentOrg) => getOrgPublicOrigin($currentOrg)); export const isFreePlan = derived(currentOrg, ($currentOrg) => {