2023-09-14 18:46:24 +01:00
|
|
|
<script lang="ts">
|
2023-08-02 19:35:27 +01:00
|
|
|
import { goto } from '$app/navigation';
|
2023-09-14 06:42:16 +01:00
|
|
|
import { page } from '$app/stores';
|
2023-08-02 19:35:27 +01:00
|
|
|
import TextField from '$lib/components/Form/TextField.svelte';
|
|
|
|
|
import PrimaryButton from '$lib/components/PrimaryButton/index.svelte';
|
|
|
|
|
import { getSupabase } from '$lib/utils/functions/supabase';
|
2022-11-30 09:18:41 +01:00
|
|
|
import {
|
2022-12-03 22:45:01 +01:00
|
|
|
authValidation,
|
2022-11-30 09:18:41 +01:00
|
|
|
getConfirmPasswordError,
|
2023-08-04 12:49:34 +01:00
|
|
|
getDisableSubmit
|
2023-08-02 19:35:27 +01:00
|
|
|
} from '$lib/utils/functions/validator';
|
|
|
|
|
import { SIGNUP_FIELDS } from '$lib/utils/constants/authentication';
|
|
|
|
|
import AuthUI from '$lib/components/AuthUI/index.svelte';
|
2023-09-14 18:46:24 +01:00
|
|
|
import { profile } from '$lib/utils/store/user';
|
2023-09-15 00:11:23 +01:00
|
|
|
import { currentOrg } from '$lib/utils/store/org';
|
2022-11-28 11:23:46 +01:00
|
|
|
|
2023-08-02 19:35:27 +01:00
|
|
|
let supabase = getSupabase();
|
2022-11-30 09:18:41 +01:00
|
|
|
let fields = Object.assign({}, SIGNUP_FIELDS);
|
2022-11-28 11:23:46 +01:00
|
|
|
let loading = false;
|
|
|
|
|
let success = false;
|
|
|
|
|
let errors = {};
|
2023-09-14 18:46:24 +01:00
|
|
|
let submitError: string;
|
2022-11-30 09:18:41 +01:00
|
|
|
let disableSubmit = false;
|
2023-09-14 18:46:24 +01:00
|
|
|
let formRef: HTMLFormElement;
|
|
|
|
|
|
|
|
|
|
let query = new URLSearchParams($page.url.search);
|
|
|
|
|
let redirect = query.get('redirect');
|
2021-07-26 02:31:00 +03:00
|
|
|
|
2022-12-03 22:45:01 +01:00
|
|
|
async function handleSubmit(e) {
|
|
|
|
|
const validationRes = authValidation(fields);
|
2023-08-02 19:35:27 +01:00
|
|
|
console.log('validationRes', validationRes);
|
2022-11-30 09:18:41 +01:00
|
|
|
|
|
|
|
|
if (Object.keys(validationRes).length) {
|
|
|
|
|
errors = Object.assign(errors, validationRes);
|
2022-11-28 11:23:46 +01:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-26 07:49:05 +03:00
|
|
|
try {
|
|
|
|
|
loading = true;
|
2023-09-14 18:46:24 +01:00
|
|
|
|
|
|
|
|
const {
|
|
|
|
|
data: { session },
|
|
|
|
|
error
|
|
|
|
|
} = await supabase.auth.signUp({
|
2022-11-30 09:18:41 +01:00
|
|
|
email: fields.email,
|
2023-08-04 12:49:34 +01:00
|
|
|
password: fields.password
|
2022-11-30 09:18:41 +01:00
|
|
|
});
|
2023-09-14 18:46:24 +01:00
|
|
|
console.log('session', session);
|
|
|
|
|
|
2021-07-26 07:49:05 +03:00
|
|
|
if (error) throw error;
|
2022-11-28 11:23:46 +01:00
|
|
|
|
2023-09-14 18:46:24 +01:00
|
|
|
const { user: authUser } = session || {};
|
|
|
|
|
if (!authUser) {
|
|
|
|
|
throw 'Error creating user';
|
|
|
|
|
}
|
|
|
|
|
|
2023-09-15 00:11:23 +01:00
|
|
|
if (!$currentOrg.id) return;
|
|
|
|
|
|
2023-09-14 18:46:24 +01:00
|
|
|
const [regexUsernameMatch] = [...(authUser.email?.matchAll(/(.*)@/g) || [])];
|
2023-09-15 00:11:23 +01:00
|
|
|
const response = await fetch('https://api.ipregistry.co/?key=tryout');
|
|
|
|
|
const metadata = await response.json();
|
2023-09-14 18:46:24 +01:00
|
|
|
|
|
|
|
|
const profileRes = await supabase
|
|
|
|
|
.from('profile')
|
|
|
|
|
.insert({
|
|
|
|
|
id: authUser.id,
|
|
|
|
|
username: regexUsernameMatch[1] + `${new Date().getTime()}`,
|
|
|
|
|
fullname: regexUsernameMatch[1],
|
2023-09-15 00:11:23 +01:00
|
|
|
email: authUser.email,
|
|
|
|
|
metadata
|
2023-09-14 18:46:24 +01:00
|
|
|
})
|
|
|
|
|
.select();
|
|
|
|
|
console.log('profileRes', profileRes);
|
|
|
|
|
|
|
|
|
|
if (profileRes.error) {
|
|
|
|
|
throw profileRes.error;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Setting profile
|
|
|
|
|
console.log('setting profile', profileRes.data[0]);
|
|
|
|
|
profile.set(profileRes.data[0]);
|
|
|
|
|
|
|
|
|
|
if (redirect) {
|
|
|
|
|
goto(redirect);
|
|
|
|
|
} else {
|
2023-09-15 00:11:23 +01:00
|
|
|
goto('/login');
|
2023-09-14 18:46:24 +01:00
|
|
|
}
|
|
|
|
|
|
2022-12-03 22:45:01 +01:00
|
|
|
formRef?.reset();
|
2023-09-14 18:46:24 +01:00
|
|
|
success = true;
|
|
|
|
|
fields = Object.assign({}, SIGNUP_FIELDS);
|
2021-07-26 07:49:05 +03:00
|
|
|
} catch (error) {
|
2022-11-30 09:18:41 +01:00
|
|
|
submitError = error.error_description || error.message;
|
2021-07-26 07:49:05 +03:00
|
|
|
} finally {
|
|
|
|
|
loading = false;
|
|
|
|
|
}
|
2021-07-26 02:31:00 +03:00
|
|
|
}
|
2022-11-30 09:18:41 +01:00
|
|
|
|
|
|
|
|
$: errors.confirmPassword = getConfirmPasswordError(fields);
|
|
|
|
|
$: disableSubmit = getDisableSubmit(fields);
|
2021-07-26 02:31:00 +03:00
|
|
|
</script>
|
|
|
|
|
|
|
|
|
|
<svelte:head>
|
2022-12-03 22:45:01 +01:00
|
|
|
<title>Join ClassroomIO</title>
|
2021-07-26 02:31:00 +03:00
|
|
|
</svelte:head>
|
|
|
|
|
|
2023-08-04 12:49:34 +01:00
|
|
|
<AuthUI {supabase} isLogin={false} {handleSubmit} isLoading={loading} bind:formRef>
|
2022-11-28 11:23:46 +01:00
|
|
|
<div class="mt-4 w-full">
|
2023-08-04 12:49:34 +01:00
|
|
|
<p class="dark:text-white text-lg font-semibold mb-6">Create a free account</p>
|
2022-12-03 22:45:01 +01:00
|
|
|
<!-- <TextField
|
|
|
|
|
label="Full Name"
|
|
|
|
|
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
|
|
|
|
|
/> -->
|
|
|
|
|
<TextField
|
|
|
|
|
label="Your Email"
|
|
|
|
|
bind:value={fields.email}
|
|
|
|
|
type="email"
|
|
|
|
|
placeholder="you@domain.com"
|
|
|
|
|
className="mb-6"
|
|
|
|
|
inputClassName="w-full"
|
|
|
|
|
isDisabled={loading}
|
|
|
|
|
errorMessage={errors.email}
|
|
|
|
|
isRequired
|
|
|
|
|
/>
|
|
|
|
|
<TextField
|
|
|
|
|
label="Your Password"
|
|
|
|
|
bind:value={fields.password}
|
|
|
|
|
type="password"
|
|
|
|
|
placeholder="************"
|
|
|
|
|
className="mb-6"
|
|
|
|
|
inputClassName="w-full"
|
|
|
|
|
isDisabled={loading}
|
|
|
|
|
errorMessage={errors.password}
|
|
|
|
|
helperMessage="Password must be more than 6 characters"
|
|
|
|
|
isRequired
|
|
|
|
|
/>
|
|
|
|
|
<TextField
|
|
|
|
|
label="Confirm Password"
|
|
|
|
|
bind:value={fields.confirmPassword}
|
|
|
|
|
type="password"
|
|
|
|
|
placeholder="************"
|
|
|
|
|
className="mb-6"
|
|
|
|
|
inputClassName="w-full"
|
|
|
|
|
isDisabled={loading}
|
|
|
|
|
errorMessage={errors.confirmPassword}
|
|
|
|
|
isRequired
|
|
|
|
|
/>
|
|
|
|
|
{#if submitError}
|
2023-05-11 15:15:18 +00:00
|
|
|
<p class="text-sm text-red-500">{submitError}</p>
|
2022-11-28 11:23:46 +01:00
|
|
|
{/if}
|
2021-07-26 02:31:00 +03:00
|
|
|
</div>
|
|
|
|
|
|
2022-12-03 22:45:01 +01:00
|
|
|
<div class="my-4 w-full flex justify-end items-center">
|
|
|
|
|
<PrimaryButton
|
|
|
|
|
label="Create Account"
|
|
|
|
|
type="submit"
|
|
|
|
|
className="sm:w-full w-full"
|
|
|
|
|
isDisabled={disableSubmit || loading}
|
|
|
|
|
isLoading={loading}
|
|
|
|
|
/>
|
|
|
|
|
</div>
|
2022-11-28 11:23:46 +01:00
|
|
|
</AuthUI>
|