Files
classroomio/apps/dashboard/src/routes/+layout.server.ts
T

177 lines
5.2 KiB
TypeScript
Raw Normal View History

2024-08-01 14:04:50 +01:00
import type { MetaTagsProps } from 'svelte-meta-tags';
2024-04-19 05:45:48 +01:00
import { dev, browser } from '$app/environment';
2023-12-11 03:27:13 +03:00
import { redirect } from '@sveltejs/kit';
2023-08-14 06:22:03 +01:00
import { blockedSubdomain } from '$lib/utils/constants/app';
2023-08-20 19:08:48 +00:00
import { getCurrentOrg } from '$lib/utils/services/org';
2023-08-21 10:10:11 +01:00
import { getSupabase, supabase } from '$lib/utils/functions/supabase';
2024-04-19 05:45:48 +01:00
import { loadTranslations } from '$lib/utils/functions/translations';
2023-10-29 12:07:34 +00:00
import type { CurrentOrg } from '$lib/utils/types/org';
2024-10-09 14:59:56 +01:00
import { IS_SELFHOSTED } from '$env/static/private';
2024-10-09 15:00:30 +01:00
import { env } from '$env/dynamic/private';
2023-08-21 10:10:11 +01:00
if (!supabase) {
getSupabase();
}
2023-08-14 06:22:03 +01:00
2023-10-29 12:07:34 +00:00
interface LoadOutput {
orgSiteName: string;
isOrgSite: boolean;
skipAuth: boolean;
2023-12-03 16:29:27 +03:00
org: CurrentOrg | null;
2024-08-01 14:04:50 +01:00
baseMetaTags: MetaTagsProps;
2023-10-29 12:07:34 +00:00
}
2023-11-13 10:24:12 +00:00
export const load = async ({ url, cookies }): Promise<LoadOutput> => {
2023-12-03 16:29:27 +03:00
const response: LoadOutput = {
2023-08-14 06:22:03 +01:00
orgSiteName: '',
isOrgSite: false,
2023-08-20 19:08:48 +00:00
skipAuth: false,
2024-08-01 14:04:50 +01:00
org: null,
baseMetaTags: getBaseMetaTags(url)
2023-08-14 06:22:03 +01:00
};
2024-10-09 14:59:56 +01:00
console.log('IS_SELFHOSTED', IS_SELFHOSTED);
2024-10-09 14:51:32 +01:00
2024-06-15 08:55:46 +01:00
// Selfhosted usecase would be here
2024-10-09 14:59:56 +01:00
if (IS_SELFHOSTED === 'true') {
2024-06-15 08:55:46 +01:00
const subdomain = getSubdomain(url);
2024-10-09 14:51:32 +01:00
console.log('subdomain', subdomain);
2024-06-15 08:55:46 +01:00
// Student dashboard
if (subdomain) {
const org = (await getCurrentOrg(subdomain, true)) || null;
// Organization by subdomain not found
if (!org) {
return response;
}
response.org = org;
response.isOrgSite = true;
response.orgSiteName = subdomain;
}
return response;
}
2024-04-25 07:22:46 +01:00
const isLocalHost = url.host.includes('localhost');
const tempSiteName = url.searchParams.get('org');
if (isLocalHost && tempSiteName) {
console.log('setting sitename temp');
2024-06-16 05:01:35 +01:00
cookies.set('_orgSiteName', tempSiteName, {
path: '/'
});
2024-04-25 07:22:46 +01:00
}
const _orgSiteName = cookies.get('_orgSiteName');
2023-08-14 06:22:03 +01:00
const debugPlay = cookies.get('debugPlay');
const debugMode = _orgSiteName && _orgSiteName !== 'false';
2023-08-14 06:22:03 +01:00
2024-06-15 09:23:02 +01:00
const subdomain = getSubdomain(url) || '';
2023-08-14 06:22:03 +01:00
2024-04-25 07:22:46 +01:00
const isDev = dev || isLocalHost;
2024-04-20 04:39:16 +01:00
2024-04-25 07:22:46 +01:00
if (!url.host.includes('.classroomio.com') && !isLocalHost) {
2024-09-04 06:32:54 +01:00
// Custom domain
response.org = (await getCurrentOrg(url.host, true, true)) || null;
2024-09-04 06:32:54 +01:00
console.log('custom domain response.org', response.org);
if (!response.org) {
throw redirect(307, 'https://app.classroomio.com/404?type=org');
}
response.isOrgSite = true;
response.orgSiteName = response.org?.siteName || '';
return response;
} else if (!blockedSubdomain.includes(subdomain)) {
2024-06-15 09:23:02 +01:00
const answer = !!subdomain;
2023-08-14 06:22:03 +01:00
response.isOrgSite = debugMode || answer;
response.orgSiteName = debugMode ? _orgSiteName : subdomain;
2023-12-03 16:29:27 +03:00
response.org = (await getCurrentOrg(response.orgSiteName, true)) || null;
2023-12-11 03:27:13 +03:00
2024-04-20 04:39:16 +01:00
if (!response.org && !isDev) {
throw redirect(307, 'https://app.classroomio.com/404?type=org');
2024-04-25 07:22:46 +01:00
} else if (!response.org && _orgSiteName) {
cookies.delete('_orgSiteName');
2023-12-11 03:27:13 +03:00
}
2023-08-14 06:22:03 +01:00
} else if (subdomain === 'play' || debugPlay === 'true') {
response.skipAuth = true;
2024-09-20 11:16:19 +01:00
} else if (!env.PRIVATE_APP_SUBDOMAINS.split(',').includes(subdomain) && !isDev) {
2024-06-15 08:55:46 +01:00
// This case is for anything in our blockedSubdomains
throw redirect(307, 'https://app.classroomio.com');
2023-08-14 06:22:03 +01:00
}
2024-04-19 05:45:48 +01:00
// Load translations
const { pathname } = url;
const initLocale = getInitialLocale();
await loadTranslations(initLocale, pathname);
2023-08-14 06:22:03 +01:00
return response;
};
2024-04-19 05:45:48 +01:00
2024-08-01 14:04:50 +01:00
function getBaseMetaTags(url: URL) {
return Object.freeze({
2024-10-07 01:36:07 +01:00
title: 'ClassroomIO | The Open Source Learning Management System for Companies',
2024-08-01 14:04:50 +01:00
description:
2024-10-07 01:36:07 +01:00
'A flexible, user-friendly platform for creating, managing, and delivering courses for companies and training organisations',
2024-08-01 14:04:50 +01:00
canonical: new URL(url.pathname, url.origin).href,
openGraph: {
type: 'website',
url: new URL(url.pathname, url.origin).href,
locale: 'en_IE',
2024-10-07 01:36:07 +01:00
title: 'ClassroomIO | The Open Source Learning Management System for Companies',
2024-08-01 14:04:50 +01:00
description:
2024-10-07 01:36:07 +01:00
'A flexible, user-friendly platform for creating, managing, and delivering courses for companies and training organisations',
2024-08-01 14:04:50 +01:00
siteName: 'ClassroomIO',
images: [
{
2024-09-05 15:51:05 +01:00
url: 'https://brand.cdn.clsrio.com/og/classroomio-og.png',
2024-08-01 14:04:50 +01:00
alt: 'ClassroomIO OG Image',
width: 1920,
height: 1080,
2024-09-05 15:51:05 +01:00
secureUrl: 'https://brand.cdn.clsrio.com/og/classroomio-og.png',
2024-08-01 14:04:50 +01:00
type: 'image/jpeg'
}
]
},
twitter: {
handle: '@classroomio',
site: '@classroomio',
2024-08-25 21:35:59 +01:00
cardType: 'summary_large_image' as const,
2024-10-07 01:36:07 +01:00
title: 'ClassroomIO | The Open Source Learning Management System for Companies',
2024-08-01 14:04:50 +01:00
description:
2024-10-07 01:36:07 +01:00
'A flexible, user-friendly platform for creating, managing, and delivering courses for companies and training organisations',
2024-09-05 15:51:05 +01:00
image: 'https://brand.cdn.clsrio.com/og/classroomio-og.png',
2024-08-01 14:04:50 +01:00
imageAlt: 'ClassroomIO OG Image'
}
});
}
2024-04-19 05:45:48 +01:00
// Define getInitialLocale function
function getInitialLocale(): string {
if (browser) {
try {
return window.navigator.language.split('-')[0];
2024-04-20 04:39:16 +01:00
} catch (e) {
2024-04-19 05:45:48 +01:00
return 'en';
}
}
return 'en';
}
2024-06-15 08:55:46 +01:00
function getSubdomain(url: URL) {
2024-06-15 09:23:02 +01:00
const host = url.host.replace('www.', '');
const parts = host.split('.');
2024-09-20 11:16:19 +01:00
if (host.endsWith(env.PRIVATE_APP_HOST)) {
2024-06-15 08:55:46 +01:00
return parts.length >= 3 ? parts[0] : null;
}
2024-06-15 09:23:02 +01:00
2024-06-15 08:55:46 +01:00
return null;
}