mirror of
https://github.com/jcreek/LivingDexTracker.git
synced 2026-07-13 02:53:45 +00:00
feat(#9): Add sign in and sign out
This commit is contained in:
@@ -0,0 +1,30 @@
|
||||
<script lang="ts">
|
||||
import { createEventDispatcher } from 'svelte';
|
||||
const dispatch = createEventDispatcher();
|
||||
|
||||
function emitSignedInEvent() {
|
||||
dispatch('signedIn', {});
|
||||
}
|
||||
|
||||
let email = '';
|
||||
let password = '';
|
||||
|
||||
// Access the supabase client from the layout data
|
||||
export let supabase: any;
|
||||
|
||||
async function signInWithEmail() {
|
||||
// TODO use the data and error from the response
|
||||
const { data, error } = await supabase.auth
|
||||
.signInWithPassword({
|
||||
email: email,
|
||||
password: password
|
||||
})
|
||||
.then(() => {
|
||||
emitSignedInEvent();
|
||||
});
|
||||
}
|
||||
</script>
|
||||
|
||||
<input type="email" bind:value={email} placeholder="Email" />
|
||||
<input type="password" bind:value={password} placeholder="Password" />
|
||||
<button on:click={signInWithEmail}>Sign In</button>
|
||||
@@ -0,0 +1,20 @@
|
||||
<script lang="ts">
|
||||
import { createEventDispatcher } from 'svelte';
|
||||
const dispatch = createEventDispatcher();
|
||||
|
||||
function emitSignedOutEvent() {
|
||||
dispatch('signedOut', {});
|
||||
}
|
||||
|
||||
// Access the supabase client from the layout data
|
||||
export let supabase: any;
|
||||
|
||||
async function signOut() {
|
||||
// TODO use the error from the response
|
||||
const { error } = await supabase.auth.signOut().then(() => {
|
||||
emitSignedOutEvent();
|
||||
});
|
||||
}
|
||||
</script>
|
||||
|
||||
<button on:click={signOut}>Sign Out</button>
|
||||
@@ -1,9 +1,53 @@
|
||||
<script lang="ts">
|
||||
import { onMount } from 'svelte';
|
||||
import SignUp from '$lib/components/SignUp.svelte';
|
||||
import SignIn from '$lib/components/SignIn.svelte';
|
||||
import SignOut from '$lib/components/SignOut.svelte';
|
||||
|
||||
export let data;
|
||||
let { supabase } = data;
|
||||
$: ({ supabase } = data);
|
||||
|
||||
let userEmail = null as string | null | undefined;
|
||||
|
||||
onMount(async () => {
|
||||
await setUserEmail();
|
||||
});
|
||||
|
||||
async function getUser() {
|
||||
const {
|
||||
data: { session }
|
||||
} = await supabase.auth.getSession();
|
||||
|
||||
if (session) {
|
||||
console.log(session.user);
|
||||
return session.user;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
async function getUserEmail() {
|
||||
const user = await getUser();
|
||||
|
||||
if (user) {
|
||||
return user.email;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
async function setUserEmail() {
|
||||
userEmail = await getUserEmail();
|
||||
}
|
||||
</script>
|
||||
|
||||
{#if userEmail}
|
||||
<span>Welcome back {userEmail}</span>
|
||||
<br />
|
||||
{/if}
|
||||
<SignUp {supabase} />
|
||||
<br />
|
||||
<SignIn {supabase} on:signedIn={setUserEmail} />
|
||||
<br />
|
||||
<SignOut {supabase} on:signedOut={setUserEmail} />
|
||||
|
||||
Reference in New Issue
Block a user