mirror of
https://github.com/jcreek/SvelteKitSaasBoilerplate.git
synced 2026-07-13 19:13:48 +00:00
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:
@@ -1,5 +1,8 @@
|
|||||||
<script lang="ts">
|
<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();
|
const dispatch = createEventDispatcher();
|
||||||
|
|
||||||
function emitSignedOutEvent() {
|
function emitSignedOutEvent() {
|
||||||
@@ -7,13 +10,38 @@
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Access the supabase client from the layout data
|
// 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() {
|
async function signOut() {
|
||||||
// TODO use the error from the response
|
const { error } = await supabase.auth.signOut();
|
||||||
const { error } = await supabase.auth.signOut().then(() => {
|
|
||||||
|
if (error) {
|
||||||
|
console.error('Error signing out:', error.message);
|
||||||
|
alert('Error signing out');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
emitSignedOutEvent();
|
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>
|
</script>
|
||||||
|
|
||||||
|
|||||||
@@ -78,12 +78,5 @@ export const actions: Actions = {
|
|||||||
return {
|
return {
|
||||||
name
|
name
|
||||||
};
|
};
|
||||||
},
|
|
||||||
signout: async ({ locals: { supabase, safeGetSession } }) => {
|
|
||||||
const { session } = await safeGetSession();
|
|
||||||
if (session) {
|
|
||||||
await supabase.auth.signOut();
|
|
||||||
redirect(303, '/');
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user