2023-09-01 15:28:47 +01:00
|
|
|
<script lang="ts">
|
|
|
|
|
import { goto } from '$app/navigation';
|
2025-09-10 18:27:49 +01:00
|
|
|
import { page } from '$app/stores';
|
2024-12-28 23:11:55 +01:00
|
|
|
import AuthUI from '$lib/components/AuthUI/index.svelte';
|
|
|
|
|
import type { Profile } from '$lib/components/Course/components/People/types';
|
2023-09-01 15:28:47 +01:00
|
|
|
import TextField from '$lib/components/Form/TextField.svelte';
|
|
|
|
|
import PrimaryButton from '$lib/components/PrimaryButton/index.svelte';
|
2025-09-10 20:05:37 +01:00
|
|
|
import { VARIANTS } from '$lib/components/PrimaryButton/constants';
|
2024-12-28 23:11:55 +01:00
|
|
|
import { SIGNUP_FIELDS } from '$lib/utils/constants/authentication';
|
|
|
|
|
import { logout } from '$lib/utils/functions/logout';
|
2023-09-01 15:28:47 +01:00
|
|
|
import { getSupabase } from '$lib/utils/functions/supabase';
|
2024-12-28 23:11:55 +01:00
|
|
|
import { setTheme } from '$lib/utils/functions/theme';
|
|
|
|
|
import { t } from '$lib/utils/functions/translations';
|
2025-09-10 20:39:16 +01:00
|
|
|
import { profile, user } from '$lib/utils/store/user';
|
2023-09-01 15:28:47 +01:00
|
|
|
import {
|
|
|
|
|
authValidation,
|
|
|
|
|
getConfirmPasswordError,
|
|
|
|
|
getDisableSubmit
|
|
|
|
|
} from '$lib/utils/functions/validator';
|
2023-09-14 06:42:16 +01:00
|
|
|
import { currentOrg, currentOrgPath } from '$lib/utils/store/org';
|
2024-07-08 07:13:00 +01:00
|
|
|
import type { CurrentOrg } from '$lib/utils/types/org';
|
2023-09-01 15:28:47 +01:00
|
|
|
import { onMount } from 'svelte';
|
2025-09-10 18:27:49 +01:00
|
|
|
import { snackbar } from '$lib/components/Snackbar/store.js';
|
2023-09-01 15:28:47 +01:00
|
|
|
|
|
|
|
|
export let data;
|
|
|
|
|
|
|
|
|
|
let supabase = getSupabase();
|
|
|
|
|
let fields = Object.assign({}, SIGNUP_FIELDS);
|
|
|
|
|
let loading = false;
|
2025-09-10 20:05:37 +01:00
|
|
|
let isLoggingOut = false;
|
|
|
|
|
let shouldLogout = false;
|
2023-09-01 15:28:47 +01:00
|
|
|
|
|
|
|
|
let errors: {
|
|
|
|
|
name?: string;
|
|
|
|
|
email?: string;
|
|
|
|
|
password?: string;
|
|
|
|
|
confirmPassword?: string;
|
|
|
|
|
} = {};
|
|
|
|
|
|
|
|
|
|
let submitError: string;
|
|
|
|
|
let disableSubmit = false;
|
|
|
|
|
let formRef: HTMLFormElement;
|
|
|
|
|
|
2025-09-10 20:05:37 +01:00
|
|
|
async function joinOrg(profileId: string, email: string) {
|
|
|
|
|
if (!profileId || !email || !data.invite.currentOrg?.id) return;
|
|
|
|
|
|
|
|
|
|
// Update member response
|
|
|
|
|
const updateMemberRes = await supabase
|
|
|
|
|
.from('organizationmember')
|
|
|
|
|
.update({
|
|
|
|
|
verified: true,
|
|
|
|
|
profile_id: profileId
|
|
|
|
|
})
|
|
|
|
|
.match({ email: email, organization_id: data.invite.currentOrg?.id });
|
|
|
|
|
|
|
|
|
|
console.log('Update member response', updateMemberRes);
|
|
|
|
|
|
|
|
|
|
formRef?.reset();
|
|
|
|
|
|
|
|
|
|
return goto($currentOrgPath);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async function signUserIn(profileId: string, email: string) {
|
|
|
|
|
if (!profileId || !email) {
|
|
|
|
|
throw $t('login.validation.unable_to_create_profile');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const res = await supabase.auth.signInWithPassword({
|
|
|
|
|
email,
|
|
|
|
|
password: fields.password
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (res.error) {
|
|
|
|
|
throw res.error;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-09-01 15:28:47 +01:00
|
|
|
async function handleSubmit() {
|
2025-09-10 20:05:37 +01:00
|
|
|
if ($profile && $profile.id) {
|
|
|
|
|
loading = true;
|
|
|
|
|
await joinOrg($profile.id, $profile.email);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2023-09-14 06:42:16 +01:00
|
|
|
const validationRes = authValidation({
|
|
|
|
|
...fields,
|
2025-09-10 20:05:37 +01:00
|
|
|
email: data.invite.email // validation for this ema
|
2023-09-14 06:42:16 +01:00
|
|
|
});
|
2023-09-01 15:28:47 +01:00
|
|
|
console.log('validationRes', validationRes);
|
|
|
|
|
|
|
|
|
|
if (Object.keys(validationRes).length) {
|
|
|
|
|
errors = Object.assign(errors, validationRes);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
loading = true;
|
2024-09-17 14:24:37 +01:00
|
|
|
let profile: Profile | null = data.invite.profile;
|
2023-09-01 15:28:47 +01:00
|
|
|
|
2024-09-17 14:24:37 +01:00
|
|
|
if (!data.invite.profile) {
|
2023-09-14 06:42:16 +01:00
|
|
|
// Signup
|
|
|
|
|
const { data: signupData, error } = await supabase.auth.signUp({
|
2024-09-17 14:24:37 +01:00
|
|
|
email: data.invite.email,
|
2023-09-14 06:42:16 +01:00
|
|
|
password: fields.password
|
|
|
|
|
});
|
2023-09-01 15:28:47 +01:00
|
|
|
|
2023-09-14 06:42:16 +01:00
|
|
|
console.log('Signup', signupData);
|
|
|
|
|
if (error) throw error;
|
|
|
|
|
|
|
|
|
|
if (!signupData.user) {
|
2024-12-28 23:11:55 +01:00
|
|
|
throw $t('login.validation.user_cannot_be_created');
|
2023-09-14 06:42:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Insert profile
|
|
|
|
|
const userId = signupData.user.id;
|
|
|
|
|
const profileRes = await supabase
|
|
|
|
|
.from('profile')
|
|
|
|
|
.insert({
|
|
|
|
|
id: userId,
|
|
|
|
|
username: fields.name.toLowerCase().replace(' ', '-') + new Date().getTime(),
|
|
|
|
|
fullname: fields.name,
|
2024-09-17 14:24:37 +01:00
|
|
|
email: data.invite.email
|
2023-09-14 06:42:16 +01:00
|
|
|
})
|
|
|
|
|
.select();
|
|
|
|
|
|
|
|
|
|
console.log('Insert profile', profileRes.data);
|
|
|
|
|
|
|
|
|
|
if (profileRes.error) {
|
|
|
|
|
throw profileRes.error;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
profile = profileRes.data[0] || {};
|
2023-09-01 15:28:47 +01:00
|
|
|
}
|
|
|
|
|
|
2023-09-14 06:42:16 +01:00
|
|
|
if (!profile?.id) {
|
2024-12-28 23:11:55 +01:00
|
|
|
throw $t('login.validation.unable_to_create_profile');
|
2023-09-01 15:28:47 +01:00
|
|
|
}
|
|
|
|
|
|
2025-09-10 20:05:37 +01:00
|
|
|
await signUserIn(profile.id, profile.email);
|
2023-09-14 06:42:16 +01:00
|
|
|
|
2025-09-10 20:05:37 +01:00
|
|
|
await joinOrg(profile.id, profile.email);
|
2023-09-01 15:28:47 +01:00
|
|
|
} catch (error) {
|
|
|
|
|
if (error instanceof Error) {
|
|
|
|
|
submitError = error.message;
|
2023-09-14 06:42:16 +01:00
|
|
|
} else {
|
|
|
|
|
submitError = error?.toString() || '';
|
2023-09-01 15:28:47 +01:00
|
|
|
}
|
|
|
|
|
} finally {
|
|
|
|
|
loading = false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function setCurOrg(cOrg: CurrentOrg) {
|
|
|
|
|
if (!cOrg) return;
|
2024-09-17 14:24:37 +01:00
|
|
|
|
|
|
|
|
console.log(cOrg);
|
2023-09-01 15:28:47 +01:00
|
|
|
currentOrg.set(cOrg);
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-17 14:24:37 +01:00
|
|
|
onMount(async () => {
|
|
|
|
|
setTheme(data.invite.currentOrg?.theme || '');
|
|
|
|
|
|
|
|
|
|
setCurOrg(data.invite.currentOrg as CurrentOrg);
|
2023-09-01 15:28:47 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
$: errors.confirmPassword = getConfirmPasswordError(fields);
|
|
|
|
|
$: disableSubmit = getDisableSubmit(fields);
|
|
|
|
|
|
2025-09-10 18:27:49 +01:00
|
|
|
$: autoLogout($profile?.email);
|
|
|
|
|
function autoLogout(email?: string) {
|
|
|
|
|
if (!email) return;
|
|
|
|
|
|
|
|
|
|
if (email !== data.invite.email) {
|
|
|
|
|
console.log('logout');
|
2025-09-10 20:05:37 +01:00
|
|
|
snackbar.error('You are logged in with a different email');
|
|
|
|
|
shouldLogout = true;
|
2025-09-10 18:27:49 +01:00
|
|
|
}
|
|
|
|
|
}
|
2025-09-10 20:39:16 +01:00
|
|
|
|
|
|
|
|
$: isLoading = loading || $user.fetchingUser;
|
2025-09-10 20:45:50 +01:00
|
|
|
$: console.log('$profile', $profile);
|
|
|
|
|
$: console.log('data.invite.profile', data.invite.profile);
|
2023-09-01 15:28:47 +01:00
|
|
|
</script>
|
|
|
|
|
|
|
|
|
|
<svelte:head>
|
|
|
|
|
<title>Join ClassroomIO</title>
|
|
|
|
|
</svelte:head>
|
|
|
|
|
|
2025-09-10 18:49:50 +01:00
|
|
|
<AuthUI
|
|
|
|
|
{supabase}
|
|
|
|
|
redirectPathname={$page.url.pathname}
|
|
|
|
|
isLogin={false}
|
|
|
|
|
{handleSubmit}
|
2025-09-10 20:39:16 +01:00
|
|
|
{isLoading}
|
2025-09-10 18:49:50 +01:00
|
|
|
showLogo={true}
|
|
|
|
|
bind:formRef
|
|
|
|
|
>
|
2025-09-10 20:05:37 +01:00
|
|
|
<div class="mt-4 w-full {shouldLogout ? 'hidden' : ''}">
|
2024-12-28 23:11:55 +01:00
|
|
|
<p class="mb-6 text-lg font-semibold dark:text-white">
|
2024-09-17 14:24:37 +01:00
|
|
|
{#if data.invite.profile}
|
2024-12-28 23:11:55 +01:00
|
|
|
{$t('login.login_to_join')}
|
2023-09-14 06:42:16 +01:00
|
|
|
{:else}
|
2024-12-28 23:11:55 +01:00
|
|
|
{$t('login.create_to_join')}
|
2023-09-14 06:42:16 +01:00
|
|
|
{/if}
|
|
|
|
|
</p>
|
2023-09-01 15:28:47 +01:00
|
|
|
<TextField
|
2024-12-28 23:11:55 +01:00
|
|
|
label={$t('login.fields.email')}
|
2024-09-17 14:24:37 +01:00
|
|
|
value={data.invite.email}
|
2023-09-01 15:28:47 +01:00
|
|
|
type="email"
|
|
|
|
|
placeholder="you@domain.com"
|
|
|
|
|
className="mb-6"
|
|
|
|
|
inputClassName="w-full"
|
|
|
|
|
isDisabled={true}
|
|
|
|
|
/>
|
2024-09-17 14:24:37 +01:00
|
|
|
{#if !data.invite.profile}
|
2023-09-14 06:42:16 +01:00
|
|
|
<TextField
|
2024-12-28 23:11:55 +01:00
|
|
|
label={$t('login.fields.full_name')}
|
2023-09-14 06:42:16 +01:00
|
|
|
bind:value={fields.name}
|
|
|
|
|
type="text"
|
|
|
|
|
autoFocus={true}
|
|
|
|
|
placeholder="e.g Joke Silva"
|
|
|
|
|
className="mb-6"
|
|
|
|
|
inputClassName="w-full"
|
|
|
|
|
isDisabled={loading}
|
|
|
|
|
errorMessage={errors.name}
|
|
|
|
|
isRequired
|
|
|
|
|
/>
|
|
|
|
|
{/if}
|
2025-09-10 20:05:37 +01:00
|
|
|
{#if $profile?.email !== data.invite.email}
|
|
|
|
|
<TextField
|
|
|
|
|
label={$t('login.fields.password')}
|
|
|
|
|
bind:value={fields.password}
|
|
|
|
|
type="password"
|
|
|
|
|
placeholder="************"
|
|
|
|
|
className="mb-6"
|
|
|
|
|
inputClassName="w-full"
|
|
|
|
|
isDisabled={loading}
|
|
|
|
|
errorMessage={errors.password}
|
|
|
|
|
helperMessage={$t('login.fields.password_helper_message')}
|
|
|
|
|
isRequired
|
|
|
|
|
/>
|
|
|
|
|
{/if}
|
2024-09-17 14:24:37 +01:00
|
|
|
{#if !data.invite.profile}
|
2023-09-14 06:42:16 +01:00
|
|
|
<TextField
|
2024-12-28 23:11:55 +01:00
|
|
|
label={$t('login.fields.confirm_password')}
|
2023-09-14 06:42:16 +01:00
|
|
|
bind:value={fields.confirmPassword}
|
|
|
|
|
type="password"
|
|
|
|
|
placeholder="************"
|
|
|
|
|
className="mb-6"
|
|
|
|
|
inputClassName="w-full"
|
|
|
|
|
isDisabled={loading}
|
|
|
|
|
errorMessage={errors.confirmPassword}
|
|
|
|
|
isRequired
|
|
|
|
|
/>
|
|
|
|
|
{/if}
|
2023-09-01 15:28:47 +01:00
|
|
|
{#if submitError}
|
|
|
|
|
<p class="text-sm text-red-500">{submitError}</p>
|
|
|
|
|
{/if}
|
|
|
|
|
</div>
|
|
|
|
|
|
2024-12-28 23:11:55 +01:00
|
|
|
<div class="my-4 flex w-full items-center justify-end">
|
2025-09-10 20:05:37 +01:00
|
|
|
{#if shouldLogout}
|
|
|
|
|
<PrimaryButton
|
|
|
|
|
label="Logout"
|
|
|
|
|
type="button"
|
|
|
|
|
className="sm:w-full w-full"
|
|
|
|
|
isLoading={isLoggingOut}
|
|
|
|
|
variant={VARIANTS.CONTAINED_DANGER}
|
|
|
|
|
onClick={async () => {
|
|
|
|
|
isLoggingOut = true;
|
|
|
|
|
|
|
|
|
|
await logout(false);
|
|
|
|
|
|
|
|
|
|
isLoggingOut = false;
|
|
|
|
|
shouldLogout = false;
|
|
|
|
|
}}
|
|
|
|
|
/>
|
|
|
|
|
{:else}
|
|
|
|
|
<PrimaryButton
|
|
|
|
|
label="Accept Invite"
|
|
|
|
|
type="submit"
|
|
|
|
|
className="sm:w-full w-full"
|
2025-09-10 20:39:16 +01:00
|
|
|
isDisabled={disableSubmit}
|
|
|
|
|
{isLoading}
|
2025-09-10 20:05:37 +01:00
|
|
|
/>
|
|
|
|
|
{/if}
|
2023-09-01 15:28:47 +01:00
|
|
|
</div>
|
|
|
|
|
</AuthUI>
|