fix: avoid invite logout

This commit is contained in:
Rotimi Best
2024-09-17 14:24:37 +01:00
parent 79839fca42
commit 27f333fdcf
4 changed files with 70 additions and 56 deletions
@@ -1,5 +1,5 @@
import { get } from 'svelte/store'; import { get } from 'svelte/store';
import type { PostgrestError } from '@supabase/supabase-js';
import { goto } from '$app/navigation'; import { goto } from '$app/navigation';
import { supabase } from '$lib/utils/functions/supabase'; import { supabase } from '$lib/utils/functions/supabase';
import { orgs, currentOrg, orgAudience, orgTeam } from '$lib/utils/store/org'; import { orgs, currentOrg, orgAudience, orgTeam } from '$lib/utils/store/org';
@@ -204,34 +204,42 @@ export async function getCourseBySiteName(siteName: string) {
return data; return data;
} }
const CURRENT_ORG_QUERY = `
id,
name,
siteName,
avatar_url,
landingpage,
is_restricted,
customization,
theme,
favicon,
customDomain,
isCustomDomainVerified,
customCode,
organization_plan(
plan_name,
is_active
)
`;
export async function getCurrentOrg(siteName: string, justGet = false, isCustomDomain = false) { export async function getCurrentOrg(siteName: string, justGet = false, isCustomDomain = false) {
const { data, error } = await supabase let response: { data: CurrentOrg[] | null; error: PostgrestError | null } | null = null;
.from('organization')
.select( if (isCustomDomain) {
` response = await supabase
id, .from('organization')
name, .select(CURRENT_ORG_QUERY)
siteName, .eq('customDomain', siteName)
avatar_url, .filter('isCustomDomainVerified', 'eq', true)
landingpage, .returns<CurrentOrg[]>();
is_restricted, } else {
customization, response = await supabase
theme, .from('organization')
favicon, .select(CURRENT_ORG_QUERY)
customDomain, .eq('siteName', siteName)
isCustomDomainVerified, .returns<CurrentOrg[]>();
customCode, }
organization_plan( const { data, error } = response;
plan_name,
is_active
)
`
)
.eq(isCustomDomain ? 'customDomain' : 'siteName', siteName)
.filter('isCustomDomainVerified', 'eq', isCustomDomain ? true : false)
.returns<CurrentOrg[]>();
console.log('data =', data);
console.log('error =', error);
const isDataEmpty = !data?.[0]; const isDataEmpty = !data?.[0];
+4 -8
View File
@@ -272,15 +272,11 @@
// } // }
}); });
if (data.isOrgSite) { if (data.isOrgSite && data.org) {
if (!data.org) { $globalStore.orgSiteName = data.orgSiteName;
goto('/404'); $globalStore.isOrgSite = data.isOrgSite;
} else {
$globalStore.orgSiteName = data.orgSiteName;
$globalStore.isOrgSite = data.isOrgSite;
currentOrg.set(data.org); currentOrg.set(data.org);
}
} }
return () => { return () => {
@@ -16,16 +16,18 @@ export const load = async ({ params = { hash: '' } }) => {
const { orgId, email, orgSiteName } = JSON.parse(hashData); const { orgId, email, orgSiteName } = JSON.parse(hashData);
const _currentOrg = await getCurrentOrg(orgSiteName, true); const currentOrg = await getCurrentOrg(orgSiteName, true);
const profile = await getProfile(email); const profile = await getProfile(email);
console.log('profile data', profile); console.log('profile data', profile);
return { return {
orgId, invite: {
email, orgId,
currentOrg: _currentOrg, email,
profile currentOrg,
profile
}
}; };
} catch (error) { } catch (error) {
console.error('Error decoding invite params.hash', error); console.error('Error decoding invite params.hash', error);
@@ -48,12 +48,12 @@
try { try {
loading = true; loading = true;
let profile: Profile | null = data.profile; let profile: Profile | null = data.invite.profile;
if (!data.profile) { if (!data.invite.profile) {
// Signup // Signup
const { data: signupData, error } = await supabase.auth.signUp({ const { data: signupData, error } = await supabase.auth.signUp({
email: data.email, email: data.invite.email,
password: fields.password password: fields.password
}); });
@@ -72,7 +72,7 @@
id: userId, id: userId,
username: fields.name.toLowerCase().replace(' ', '-') + new Date().getTime(), username: fields.name.toLowerCase().replace(' ', '-') + new Date().getTime(),
fullname: fields.name, fullname: fields.name,
email: data.email email: data.invite.email
}) })
.select(); .select();
@@ -96,12 +96,12 @@
verified: true, verified: true,
profile_id: profile.id profile_id: profile.id
}) })
.match({ email: data.email, organization_id: data.currentOrg?.id }); .match({ email: data.invite.email, organization_id: data.invite.currentOrg?.id });
console.log('Update member response', updateMemberRes); console.log('Update member response', updateMemberRes);
const res = await supabase.auth.signInWithPassword({ const res = await supabase.auth.signInWithPassword({
email: data.email, email: data.invite.email,
password: fields.password password: fields.password
}); });
@@ -110,6 +110,8 @@
} }
formRef?.reset(); formRef?.reset();
console.log({ path: $currentOrgPath });
return goto($currentOrgPath); return goto($currentOrgPath);
} catch (error) { } catch (error) {
if (error instanceof Error) { if (error instanceof Error) {
@@ -124,20 +126,26 @@
function setCurOrg(cOrg: CurrentOrg) { function setCurOrg(cOrg: CurrentOrg) {
if (!cOrg) return; if (!cOrg) return;
console.log(cOrg);
currentOrg.set(cOrg); currentOrg.set(cOrg);
} }
onMount(() => { onMount(async () => {
setTheme(data.currentOrg?.theme || ''); await logout(false);
logout(false); setTheme(data.invite.currentOrg?.theme || '');
setCurOrg(data.invite.currentOrg as CurrentOrg);
}); });
$: errors.confirmPassword = getConfirmPasswordError(fields); $: errors.confirmPassword = getConfirmPasswordError(fields);
$: disableSubmit = getDisableSubmit(fields); $: disableSubmit = getDisableSubmit(fields);
$: setCurOrg(data.currentOrg as CurrentOrg); $: console.log('data', data.invite);
$: console.log('data.profile', data.profile);
$: console.log('org', $currentOrg);
$: console.log('path', $currentOrgPath);
</script> </script>
<svelte:head> <svelte:head>
@@ -147,7 +155,7 @@
<AuthUI {supabase} isLogin={false} {handleSubmit} isLoading={loading} showLogo={true} bind:formRef> <AuthUI {supabase} isLogin={false} {handleSubmit} isLoading={loading} showLogo={true} bind:formRef>
<div class="mt-4 w-full"> <div class="mt-4 w-full">
<p class="dark:text-white text-lg font-semibold mb-6"> <p class="dark:text-white text-lg font-semibold mb-6">
{#if data.profile} {#if data.invite.profile}
Log in to join Log in to join
{:else} {:else}
Create a free account to join Create a free account to join
@@ -155,14 +163,14 @@
</p> </p>
<TextField <TextField
label="Your Email" label="Your Email"
value={data.email} value={data.invite.email}
type="email" type="email"
placeholder="[email protected]" placeholder="[email protected]"
className="mb-6" className="mb-6"
inputClassName="w-full" inputClassName="w-full"
isDisabled={true} isDisabled={true}
/> />
{#if !data.profile} {#if !data.invite.profile}
<TextField <TextField
label="Full Name" label="Full Name"
bind:value={fields.name} bind:value={fields.name}
@@ -188,7 +196,7 @@
helperMessage="Password must be more than 6 characters" helperMessage="Password must be more than 6 characters"
isRequired isRequired
/> />
{#if !data.profile} {#if !data.invite.profile}
<TextField <TextField
label="Confirm Password" label="Confirm Password"
bind:value={fields.confirmPassword} bind:value={fields.confirmPassword}