mirror of
https://github.com/jcreek/SvelteKitSaasBoilerplate.git
synced 2026-07-13 02:53:50 +00:00
Merge pull request #9 from jcreek/5-add-proper-cookie-consent-that-is-gdpr-compliant
feat(#5): Add cookie consent banner
This commit is contained in:
@@ -0,0 +1,27 @@
|
|||||||
|
<script lang="ts">
|
||||||
|
import { isBrowser } from '@supabase/ssr';
|
||||||
|
|
||||||
|
export let cookiesAccepted;
|
||||||
|
let showBanner = !cookiesAccepted;
|
||||||
|
$: showBanner = !cookiesAccepted;
|
||||||
|
|
||||||
|
const acceptCookies = () => {
|
||||||
|
if (isBrowser()) {
|
||||||
|
localStorage.setItem('cookiesAccepted', 'true');
|
||||||
|
showBanner = false;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
</script>
|
||||||
|
|
||||||
|
{#if showBanner}
|
||||||
|
<div class="fixed bottom-0 left-0 w-full bg-gray-800 text-white p-4">
|
||||||
|
<p class="text-sm">
|
||||||
|
We use cookies to ensure you get the best experience on our website. By continuing, you agree
|
||||||
|
to our use of cookies.
|
||||||
|
</p>
|
||||||
|
<button class="bg-blue-500 hover:bg-blue-600 px-4 py-2 rounded" on:click={acceptCookies}
|
||||||
|
>Accept</button
|
||||||
|
>
|
||||||
|
</div>
|
||||||
|
{/if}
|
||||||
@@ -1,11 +1,13 @@
|
|||||||
<script lang="ts">
|
<script lang="ts">
|
||||||
import 'tailwindcss/tailwind.css';
|
import 'tailwindcss/tailwind.css';
|
||||||
import { onDestroy, onMount } from 'svelte';
|
import { onDestroy, onMount } from 'svelte';
|
||||||
|
import { isBrowser } from '@supabase/ssr';
|
||||||
import { basket, type Basket, type Item } from '$lib/stores/basket.js';
|
import { basket, type Basket, type Item } from '$lib/stores/basket.js';
|
||||||
import { general } from '$lib/stores/generalStore.js';
|
import { general } from '$lib/stores/generalStore.js';
|
||||||
import { invalidate } from '$app/navigation';
|
import { invalidate } from '$app/navigation';
|
||||||
import SignIn from '$lib/components/SignIn.svelte';
|
import SignIn from '$lib/components/SignIn.svelte';
|
||||||
import SignOut from '$lib/components/SignOut.svelte';
|
import SignOut from '$lib/components/SignOut.svelte';
|
||||||
|
import CookieConsent from '$lib/components/CookieConsent.svelte';
|
||||||
|
|
||||||
export let data;
|
export let data;
|
||||||
let { supabase, session } = data;
|
let { supabase, session } = data;
|
||||||
@@ -20,11 +22,14 @@
|
|||||||
const unsubscribeToGeneralStore = general.subscribe((value) => {
|
const unsubscribeToGeneralStore = general.subscribe((value) => {
|
||||||
localGeneral = value;
|
localGeneral = value;
|
||||||
});
|
});
|
||||||
|
|
||||||
onDestroy(() => {
|
onDestroy(() => {
|
||||||
unsubscribeToBasket();
|
unsubscribeToBasket();
|
||||||
unsubscribeToGeneralStore();
|
unsubscribeToGeneralStore();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
let cookiesAccepted = false;
|
||||||
|
|
||||||
onMount(() => {
|
onMount(() => {
|
||||||
const { data } = supabase.auth.onAuthStateChange((event, _session) => {
|
const { data } = supabase.auth.onAuthStateChange((event, _session) => {
|
||||||
if (_session?.expires_at !== session?.expires_at) {
|
if (_session?.expires_at !== session?.expires_at) {
|
||||||
@@ -33,6 +38,12 @@
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
if (isBrowser()) {
|
||||||
|
if (localStorage.getItem('cookiesAccepted')) {
|
||||||
|
cookiesAccepted = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return () => data.subscription.unsubscribe();
|
return () => data.subscription.unsubscribe();
|
||||||
});
|
});
|
||||||
</script>
|
</script>
|
||||||
@@ -44,6 +55,10 @@
|
|||||||
<meta property="og:description" content="" />
|
<meta property="og:description" content="" />
|
||||||
<link rel="canonical" href="" />
|
<link rel="canonical" href="" />
|
||||||
<link rel="apple-touch-icon" href="/apple-touch-icon.png" />
|
<link rel="apple-touch-icon" href="/apple-touch-icon.png" />
|
||||||
|
|
||||||
|
{#if cookiesAccepted}
|
||||||
|
<!-- Google Analytics script goes here -->
|
||||||
|
{/if}
|
||||||
</svelte:head>
|
</svelte:head>
|
||||||
|
|
||||||
<header>
|
<header>
|
||||||
@@ -162,6 +177,8 @@
|
|||||||
<slot />
|
<slot />
|
||||||
</main>
|
</main>
|
||||||
|
|
||||||
|
<CookieConsent {cookiesAccepted} />
|
||||||
|
|
||||||
<footer class="footer items-center p-4 bg-neutral text-neutral-content">
|
<footer class="footer items-center p-4 bg-neutral text-neutral-content">
|
||||||
<aside class="items-center grid-flow-col">
|
<aside class="items-center grid-flow-col">
|
||||||
<svg
|
<svg
|
||||||
@@ -218,6 +235,7 @@
|
|||||||
</a>
|
</a>
|
||||||
</nav>
|
</nav>
|
||||||
</footer>
|
</footer>
|
||||||
|
|
||||||
<div
|
<div
|
||||||
id="success-toast"
|
id="success-toast"
|
||||||
role="alert"
|
role="alert"
|
||||||
|
|||||||
Reference in New Issue
Block a user