feat: migrate auth from supabase to better auth (#596)
* feat: add README for @cio/db package and remove existingAccounts.json * load session, load profile and organization, add create profile hook * chore: cache session request The trick was passing all the cookies to the backend. Previously I was only passing the better-auth.session_token and not better-auth.session_data. Read about the difference here https://www.better-auth.com/docs/concepts/cookies#custom-cookies * feat: login, logout, onboarding, verify email, reset password. * feat: complete change email and email verification * feat: standardise how we make rpc calls and create org endpoints * fix ui errors * fix: handle potential undefined customerId and standardize avatar URL property * refactor: standardize custom domain handling and update telegram chat ID references * chore: add tailwind prefix to @cio/ui this fixed the media query issues because @cio/ui classes were overriding this dashboard tailwind build * chore: clean up org sidebar * chore: persist mode state * feat: implement order breadcrumbs * chore: upgrade to better auth 1.4 * chore: address common auth flow bugs and setup app for others to setup locally.
This commit is contained in:
@@ -3,10 +3,9 @@ import { getSupabase, supabase } from '$lib/utils/functions/supabase';
|
||||
import type { CurrentOrg } from '$lib/utils/types/org';
|
||||
import type { MetaTagsProps } from 'svelte-meta-tags';
|
||||
import { PUBLIC_IS_SELFHOSTED } from '$env/static/public';
|
||||
import { blockedSubdomain } from '$lib/utils/constants/app';
|
||||
import { dev } from '$app/environment';
|
||||
import { env } from '$env/dynamic/private';
|
||||
import { getCurrentOrg } from '$lib/utils/services/org';
|
||||
import { getOrgSiteInfo } from '$lib/features/app/layout-setup';
|
||||
import { redirect } from '@sveltejs/kit';
|
||||
|
||||
if (!supabase) {
|
||||
@@ -15,6 +14,8 @@ if (!supabase) {
|
||||
|
||||
export const ssr = PUBLIC_IS_SELFHOSTED === 'true' ? false : true;
|
||||
|
||||
const APP_SUBDOMAINS = env.PRIVATE_APP_SUBDOMAINS?.split(',') || [];
|
||||
|
||||
interface LoadOutput {
|
||||
orgSiteName: string;
|
||||
isOrgSite: boolean;
|
||||
@@ -22,115 +23,34 @@ interface LoadOutput {
|
||||
org: CurrentOrg | null;
|
||||
baseMetaTags: MetaTagsProps;
|
||||
serverLang: string;
|
||||
locals: App.Locals;
|
||||
}
|
||||
|
||||
const APP_SUBDOMAINS = env.PRIVATE_APP_SUBDOMAINS?.split(',') || [];
|
||||
export const load = async ({ url, cookies, request, locals }): Promise<LoadOutput> => {
|
||||
const debugPlay = cookies.get('debugPlay');
|
||||
|
||||
const orgSiteInfo = await getOrgSiteInfo(url, cookies);
|
||||
|
||||
export const load = async ({ url, cookies, request }): Promise<LoadOutput> => {
|
||||
const response: LoadOutput = {
|
||||
orgSiteName: '',
|
||||
isOrgSite: false,
|
||||
skipAuth: false,
|
||||
org: null,
|
||||
orgSiteName: orgSiteInfo.orgSiteName,
|
||||
isOrgSite: orgSiteInfo.isOrgSite,
|
||||
skipAuth: orgSiteInfo.subdomain === 'play' || debugPlay === 'true',
|
||||
org: orgSiteInfo.org,
|
||||
baseMetaTags: getBaseMetaTags(url),
|
||||
serverLang: request.headers?.get('accept-language') || ''
|
||||
serverLang: request.headers?.get('accept-language') || '',
|
||||
locals
|
||||
};
|
||||
|
||||
console.log('PUBLIC_IS_SELFHOSTED', PUBLIC_IS_SELFHOSTED);
|
||||
|
||||
// Selfhosted usecase would be here
|
||||
if (PUBLIC_IS_SELFHOSTED === 'true') {
|
||||
const subdomain = getSubdomain(url);
|
||||
console.log('subdomain', subdomain);
|
||||
|
||||
// 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;
|
||||
}
|
||||
|
||||
// Never go beyond this for selfhosted instances
|
||||
return response;
|
||||
}
|
||||
|
||||
const isLocalHost = url.host.includes('localhost');
|
||||
|
||||
const tempSiteName = url.searchParams.get('org');
|
||||
|
||||
if (isLocalHost && tempSiteName) {
|
||||
console.log('setting sitename temp');
|
||||
cookies.set('_orgSiteName', tempSiteName, {
|
||||
path: '/'
|
||||
});
|
||||
}
|
||||
|
||||
const _orgSiteName = cookies.get('_orgSiteName');
|
||||
const debugPlay = cookies.get('debugPlay');
|
||||
const debugMode = _orgSiteName && _orgSiteName !== 'false';
|
||||
|
||||
const subdomain = getSubdomain(url) || '';
|
||||
|
||||
const isDev = dev || isLocalHost;
|
||||
|
||||
if (isURLCustomDomain(url)) {
|
||||
// Custom domain
|
||||
response.org = (await getCurrentOrg(url.host, true, true)) || null;
|
||||
|
||||
console.log('custom domain response.org', response.org);
|
||||
|
||||
if (!response.org) {
|
||||
console.error('Custom domain org not found, loading dashboard');
|
||||
return response;
|
||||
}
|
||||
|
||||
response.isOrgSite = true;
|
||||
response.orgSiteName = response.org?.siteName || '';
|
||||
return response;
|
||||
} else if (!blockedSubdomain.includes(subdomain)) {
|
||||
if (APP_SUBDOMAINS.includes(subdomain)) {
|
||||
// This is an app domain specified in the .env file
|
||||
return response;
|
||||
}
|
||||
|
||||
console.log('subdomain', subdomain);
|
||||
|
||||
response.isOrgSite = debugMode || !!subdomain;
|
||||
response.orgSiteName = debugMode ? _orgSiteName : subdomain;
|
||||
response.org = (await getCurrentOrg(response.orgSiteName, true)) || null;
|
||||
|
||||
if (!response.org && !isDev) {
|
||||
redirect(307, 'https://app.classroomio.com/404?type=org');
|
||||
} else if (!response.org && _orgSiteName) {
|
||||
cookies.delete('_orgSiteName', { path: '/' });
|
||||
}
|
||||
} else if (subdomain === 'play' || debugPlay === 'true') {
|
||||
response.skipAuth = true;
|
||||
} else if (!APP_SUBDOMAINS.includes(subdomain) && !isDev) {
|
||||
// This case is for anything in our blockedSubdomains
|
||||
// If it isn't a registered dashboard domain and also not a valid sub domain.
|
||||
if (!APP_SUBDOMAINS.includes(orgSiteInfo.subdomain) && !dev && !orgSiteInfo.isOrgSite) {
|
||||
redirect(307, 'https://app.classroomio.com');
|
||||
}
|
||||
|
||||
return response;
|
||||
};
|
||||
|
||||
function isURLCustomDomain(url: URL) {
|
||||
if (url.host.includes('localhost')) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const notCustomDomainHosts = [env.PRIVATE_APP_HOST || '', 'classroomio.com', 'vercel.app'].filter(Boolean);
|
||||
|
||||
return !notCustomDomainHosts.some((host) => url.host.endsWith(host));
|
||||
}
|
||||
|
||||
function getBaseMetaTags(url: URL) {
|
||||
return Object.freeze({
|
||||
title: 'ClassroomIO | The Open Source Learning Management System for Companies',
|
||||
@@ -168,19 +88,3 @@ function getBaseMetaTags(url: URL) {
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
function getSubdomain(url: URL) {
|
||||
const host = url.hostname.replace('www.', '');
|
||||
const parts = host.split('.');
|
||||
const appHost = env.PRIVATE_APP_HOST;
|
||||
|
||||
const appHostParts = appHost.split('.');
|
||||
const isAppHost = parts.slice(-appHostParts.length).join('.') === appHost;
|
||||
|
||||
if (isAppHost) {
|
||||
// Subdomain exists only if extra part(s) before main domain
|
||||
return parts.length > appHostParts.length ? parts[0] : null;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user