Files
classroomio/src/routes/signup.svelte
T

157 lines
4.4 KiB
Svelte
Raw Normal View History

2021-07-31 23:06:14 +03:00
<script context="module">
export function preload(page, { config }) {
return { config };
}
</script>
2021-07-26 02:31:00 +03:00
<script>
2022-11-30 09:18:41 +01:00
import { goto } from '@sapper/app';
2022-11-28 11:23:46 +01:00
import { Chasing } from 'svelte-loading-spinners';
2021-07-26 02:31:00 +03:00
import TextField from '../components/Form/TextField.svelte';
import PrimaryButton from '../components/PrimaryButton/index.svelte';
2021-07-31 23:06:14 +03:00
import { getSupabase } from '../utils/functions/supabase';
2022-11-28 11:23:46 +01:00
import { user } from '../utils/store/user';
2022-11-30 09:18:41 +01:00
import {
signUpValidation,
getConfirmPasswordError,
getDisableSubmit,
} from '../utils/functions/validator';
import { SIGNUP_FIELDS } from '../utils/constants/authentication';
2022-11-28 11:23:46 +01:00
import AuthUI from '../components/AuthUI/index.svelte';
2022-11-30 09:18:41 +01:00
import EmailSentIcon from '../components/Icons/EmailSentIcon.svelte';
2022-11-28 11:23:46 +01:00
2021-07-31 23:06:14 +03:00
export let config;
let supabase = getSupabase(config);
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 = {};
2022-11-30 09:18:41 +01:00
let submitError;
let disableSubmit = false;
2021-07-26 02:31:00 +03:00
2021-07-26 07:49:05 +03:00
async function handleSubmit() {
2022-11-30 09:18:41 +01:00
const validationRes = signUpValidation(fields);
console.log('validationRes', validationRes);
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;
2022-11-30 09:18:41 +01:00
const { data, error } = await supabase.auth.signUp({
email: fields.email,
password: fields.password,
options: {
data: {
fullname: fields.name,
},
},
});
2021-07-26 07:49:05 +03:00
if (error) throw error;
2022-11-28 11:23:46 +01:00
2022-11-30 09:18:41 +01:00
return goto('/login');
// 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-11-28 11:23:46 +01:00
<title>Sign Up - ClassroomIO</title>
2021-07-26 02:31:00 +03:00
</svelte:head>
2022-11-30 09:18:41 +01:00
<AuthUI {supabase} isLogin={false} {handleSubmit} showOnlyContent={success}>
2022-11-28 11:23:46 +01:00
<div class="mt-4 w-full">
{#if $user.fetchingUser}
<div class="h-40 text-md flex items-center justify-center">
<Chasing size="60" color="#ff3e00" unit="px" duration="1s" />
</div>
{:else if success}
2022-11-30 09:18:41 +01:00
<div class="text-md flex items-center justify-center flex-col">
<EmailSentIcon />
<h2 class="text-xl">Email sent!</h2>
<p class="mt-4 text-center">
We have sent a confirmation email to {fields.email}. Please check both
your
2022-11-28 11:23:46 +01:00
<strong>spam </strong>and your<strong>&nbsp; inbox</strong>.
</p>
2021-07-26 02:31:00 +03:00
</div>
2022-11-28 11:23:46 +01:00
{:else}
<p class="text-lg font-semibold mb-6">Create a free account</p>
<TextField
label="Full Name"
2022-11-30 09:18:41 +01:00
bind:value={fields.name}
2022-11-28 11:23:46 +01:00
type="text"
autoFocus={true}
placeholder="e.g Joke Silva"
className="mb-6"
inputClassName="w-full"
isDisabled={loading}
errorMessage={errors.name}
2022-11-30 09:18:41 +01:00
isRequired
2022-11-28 11:23:46 +01:00
/>
<TextField
label="Your Email"
2022-11-30 09:18:41 +01:00
bind:value={fields.email}
2022-11-28 11:23:46 +01:00
type="email"
placeholder="you@domain.com"
className="mb-6"
inputClassName="w-full"
isDisabled={loading}
errorMessage={errors.email}
2022-11-30 09:18:41 +01:00
isRequired
2022-11-28 11:23:46 +01:00
/>
<TextField
label="Your Password"
2022-11-30 09:18:41 +01:00
bind:value={fields.password}
2022-11-28 11:23:46 +01:00
type="password"
placeholder="************"
className="mb-6"
inputClassName="w-full"
isDisabled={loading}
errorMessage={errors.password}
2022-11-30 09:18:41 +01:00
helperMessage="Password must be more than 6 characters"
isRequired
2022-11-28 11:23:46 +01:00
/>
<TextField
label="Confirm Password"
2022-11-30 09:18:41 +01:00
bind:value={fields.confirmPassword}
2022-11-28 11:23:46 +01:00
type="password"
placeholder="************"
className="mb-6"
inputClassName="w-full"
isDisabled={loading}
2022-11-30 09:18:41 +01:00
errorMessage={errors.confirmPassword}
isRequired
2022-11-28 11:23:46 +01:00
/>
2022-11-30 09:18:41 +01:00
{#if submitError}
<p class="text-sm text-red-500">{submitError}</p>
{/if}
2022-11-28 11:23:46 +01:00
<div class="w-full text-right">
<a class="text-md text-blue-700" href="/forgot"> Forgot password? </a>
2021-07-26 02:31:00 +03:00
</div>
2022-11-28 11:23:46 +01:00
{/if}
2021-07-26 02:31:00 +03:00
</div>
2022-11-28 11:23:46 +01:00
{#if !success}
<div class="my-4 w-full flex justify-end items-center">
<PrimaryButton
2022-11-30 09:18:41 +01:00
label={loading ? 'Creating...' : 'Create Account'}
2022-11-28 11:23:46 +01:00
type="submit"
className="py-3 sm:w-full w-full"
2022-11-30 09:18:41 +01:00
isDisabled={disableSubmit || loading}
2022-11-28 11:23:46 +01:00
/>
</div>
{/if}
</AuthUI>