feat(#60): Clear basket on sign out

This commit is contained in:
Josh Creek
2024-10-30 18:48:11 +00:00
parent b83b02fd12
commit c23de036c7
+15
View File
@@ -1,5 +1,7 @@
<script lang="ts">
import { createEventDispatcher } from 'svelte';
import { basket, type Basket, type Item } from '$lib/stores/basket.js';
const dispatch = createEventDispatcher();
function emitSignedOutEvent() {
@@ -9,10 +11,23 @@
// Access the supabase client from the layout data
export let supabase: any;
let localBasket: Basket;
const unsubscribe = basket.subscribe((value) => {
localBasket = value;
});
async function signOut() {
// TODO use the error from the response
const { error } = await supabase.auth.signOut().then(() => {
emitSignedOutEvent();
// Clear the basket
localBasket = { items: [], subtotal: 0 };
basket.set(localBasket);
unsubscribe();
if (typeof localStorage !== 'undefined') {
localStorage.removeItem('basket');
}
});
}
</script>