Files
classroomio/apps/dashboard/src/routes/org/[slug]/+layout.server.ts
T

45 lines
1.3 KiB
TypeScript
Raw Normal View History

2026-03-09 11:18:22 +01:00
import { getOrgBySiteName } from '$features/org/api/org.server';
import { getApiKeyHeaders } from '$lib/utils/services/api/server';
import { env } from '$env/dynamic/private';
import { getSubdomain } from '$features/app/layout-setup';
import { redirect } from '@sveltejs/kit';
const APP_SUBDOMAINS = env.PRIVATE_APP_SUBDOMAINS?.split(',') || ['app'];
2026-03-09 11:18:22 +01:00
const ORG_ID_COOKIE_PREFIX = 'cio_org_id_';
2026-03-09 11:18:22 +01:00
export const load = async ({ params, url, cookies }) => {
const subdomain = getSubdomain(url);
const isOrgSite = subdomain && !APP_SUBDOMAINS.includes(subdomain);
2026-03-09 11:18:22 +01:00
// If this is LMS but user is on org site, redirect to LMS
if (isOrgSite) {
redirect(307, `/lms`);
}
2026-03-09 11:18:22 +01:00
const siteName = params.slug;
const cookieKey = `${ORG_ID_COOKIE_PREFIX}${siteName}`;
const cachedOrgId = cookies.get(cookieKey);
let orgId: string | undefined = cachedOrgId;
if (!orgId) {
const apiKeyHeaders = getApiKeyHeaders();
const org = await getOrgBySiteName(siteName, apiKeyHeaders);
if (org?.id) {
orgId = org.id;
cookies.set(cookieKey, org.id, {
path: '/',
maxAge: 60 * 60 * 24 * 7, // 7 days
sameSite: 'lax',
httpOnly: false // Allow client-side access if needed
});
}
}
2024-04-07 06:08:34 +01:00
return {
2026-03-09 11:18:22 +01:00
orgName: siteName,
orgId
2024-04-07 06:08:34 +01:00
};
};