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