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

57 lines
1.7 KiB
TypeScript
Raw Normal View History

2023-12-11 21:18:31 +03:00
import { dev } 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';
2023-10-29 12:07:34 +00:00
import type { CurrentOrg } from '$lib/utils/types/org';
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;
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,
2023-12-03 16:29:27 +03:00
org: null
2023-08-14 06:22:03 +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
2023-12-03 16:29:27 +03:00
const matches = url.host.match(/([a-z 0-9 -]+).*classroomio[.]com/);
2023-08-14 06:22:03 +01:00
const subdomain = matches?.[1] ?? '';
if (!url.host.includes('.classroomio.com')) {
// TODO: We can verify if custom domain here
return response;
}
2023-08-14 06:22:03 +01:00
if (!blockedSubdomain.includes(subdomain)) {
const answer = Array.isArray(matches) ? !!subdomain && subdomain !== 'www' : false;
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
2023-12-11 21:18:31 +03:00
if (!response.org && !dev) {
throw redirect(307, 'https://app.classroomio.com/404?type=org');
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;
2023-12-11 21:18:31 +03:00
} else if (subdomain !== 'app' && !dev) {
throw redirect(307, 'https://app.classroomio.com');
2023-08-14 06:22:03 +01:00
}
return response;
};