Merge pull request #71 from jcreek/60-if-you-sign-out-the-basket-doesnt-get-cleared

60 if you sign out the basket doesnt get cleared
This commit is contained in:
Josh Creek
2024-11-10 18:53:27 +00:00
committed by GitHub
2 changed files with 33 additions and 12 deletions
+33 -5
View File
@@ -1,5 +1,8 @@
<script lang="ts">
import { createEventDispatcher } from 'svelte';
import { createEventDispatcher, onDestroy } from 'svelte';
import type { SupabaseClient } from '@supabase/supabase-js';
import { basket, type Basket } from '$lib/stores/basket.js';
const dispatch = createEventDispatcher();
function emitSignedOutEvent() {
@@ -7,13 +10,38 @@
}
// Access the supabase client from the layout data
export let supabase: any;
export let supabase: SupabaseClient;
let localBasket: Basket;
const unsubscribe = basket.subscribe((value) => {
localBasket = value;
});
onDestroy(unsubscribe);
async function signOut() {
// TODO use the error from the response
const { error } = await supabase.auth.signOut().then(() => {
const { error } = await supabase.auth.signOut();
if (error) {
console.error('Error signing out:', error.message);
alert('Error signing out');
return;
}
try {
emitSignedOutEvent();
});
// Clear the basket
localBasket = { items: [], subtotal: 0 };
basket.set(localBasket);
unsubscribe();
if (typeof localStorage !== 'undefined') {
localStorage.removeItem('basket');
}
} catch (err) {
console.error('Error clearing basket:', err);
alert('Error clearing basket');
}
}
</script>
-7
View File
@@ -78,12 +78,5 @@ export const actions: Actions = {
return {
name
};
},
signout: async ({ locals: { supabase, safeGetSession } }) => {
const { session } = await safeGetSession();
if (session) {
await supabase.auth.signOut();
redirect(303, '/');
}
}
};