Files
classroomio/src/routes/login.svelte
T

102 lines
2.6 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-04-05 07:12:51 +03:00
<script>
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-12-03 22:45:01 +01:00
import { authValidation } from '../utils/functions/validator';
import { LOGIN_FIELDS } from '../utils/constants/authentication';
2022-11-28 11:23:46 +01:00
import AuthUI from '../components/AuthUI/index.svelte';
2021-07-26 02:31:00 +03:00
2021-07-31 23:06:14 +03:00
export let config;
2022-12-03 22:45:01 +01:00
let formRef;
2021-07-31 23:06:14 +03:00
let supabase = getSupabase(config);
2022-12-03 22:45:01 +01:00
let fields = Object.assign({}, LOGIN_FIELDS);
let submitError;
2021-07-31 23:06:14 +03:00
let loading = false;
let errors = {};
2021-07-26 02:31:00 +03:00
2021-07-31 23:06:14 +03:00
async function handleSubmit() {
2022-12-03 22:45:01 +01:00
const validationRes = authValidation(fields);
console.log('validationRes', validationRes);
if (Object.keys(validationRes).length) {
errors = Object.assign(errors, validationRes);
2021-07-31 23:06:14 +03:00
return;
}
try {
loading = true;
2022-12-03 22:45:01 +01:00
const { data, error } = await supabase.auth.signIn({
email: fields.email,
password: fields.password,
});
console.log('data', data);
2021-07-31 23:06:14 +03:00
if (error) throw error;
} catch (error) {
2022-12-03 22:45:01 +01:00
submitError = error.error_description || error.message;
2021-07-31 23:06:14 +03:00
} finally {
loading = false;
}
2021-04-05 07:12:51 +03:00
}
</script>
2021-01-21 22:26:32 +02:00
<svelte:head>
2022-12-03 22:45:01 +01:00
<title>Welcome back to ClassroomIO</title>
2021-01-21 22:26:32 +02:00
</svelte:head>
2022-12-03 22:45:01 +01:00
<AuthUI
{supabase}
isLogin={true}
{handleSubmit}
showOnlyContent={loading}
bind:formRef
>
2022-11-28 11:23:46 +01:00
<div class="mt-4 w-full">
<p class="dark:text-white text-lg font-semibold mb-6">Welcome back</p>
2022-12-03 22:45:01 +01:00
<TextField
label="Your email"
bind:value={fields.email}
type="email"
autoFocus={true}
placeholder="you@domain.com"
className="mb-6"
inputClassName="w-full"
isDisabled={loading}
errorMessage={errors.email}
/>
<TextField
label="Your password"
bind:value={fields.password}
type="password"
placeholder="************"
className="mb-6"
inputClassName="w-full"
isDisabled={loading}
errorMessage={errors.password}
/>
{#if submitError}
<p class="dark:text-white text-sm text-red-500">{submitError}</p>
2022-11-28 11:23:46 +01:00
{/if}
2022-12-03 22:45:01 +01:00
<div class="w-full text-right">
<a class="text-md text-blue-700" href="/forgot"> Forgot password? </a>
</div>
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">
<!-- <a href="/login" class="text-blue-700 text-sm">Create an account</a> -->
<PrimaryButton
label="Log In"
type="submit"
className="sm:w-full w-full"
isDisabled={loading}
isLoading={loading}
/>
</div>
2022-11-28 11:23:46 +01:00
</AuthUI>