Consent engine

Content from another party, like an embedded video, sends the IP address of the visitor to that party and can set cookies. The Consent engine remembers per party whether the visitor allows it.

What it does
Remembers per party whether the visitor allows content from that party, and loads it only after a clear yes.
Exported from the package?
Yes. Import hasConsent, getConsents, giveConsent, revokeConsent and CONSENT_PROVIDERS from @davidnet-net/svelte-ui.
Where the answer is kept
Always in the consent_cache cookie on .davidnet.net (365 days), which every Davidnet app reads. That is all there is when no one is signed in. Signed in, the answer is also saved in the account, so it follows the person to every device, and when the session starts the account wins over the cookie. Never in local storage.

Which one do I use?

What I needUse
Check whether I may load contenthasConsent
List what is allowedgetConsents
Record a yesgiveConsent
Record a norevokeConsent

hasConsent

typescript
typescript
const CONSENT_PROVIDERS = ["youtube"] as const;
type ConsentProvider = (typeof CONSENT_PROVIDERS)[number];

function getConsents(): ConsentProvider[]
function hasConsent(provider: ConsentProvider): boolean

hasConsent(provider) says whether content of that party may be loaded. It is reactive: a template that reads it updates when the answer changes. The only party now is "youtube".

When to use

Use it before you load anything from another party. Show a place to ask first, and load the content when hasConsent is true. The YoutubeEmbed component does exactly this.

When not to use, and what to use instead

Content from our own servers needs no consent. Do not use it to remember other choices: it is for the loading of content from other parties only.

Live example

CallResult
hasConsent("youtube")false
getConsents()none
svelte
svelte
import { giveConsent, hasConsent } from "@davidnet-net/svelte-ui";

{#if hasConsent("youtube")}
	<iframe src={videoUrl} title={title}></iframe>
{:else}
	<button onclick={() => giveConsent("youtube")}>Load the video</button>
{/if}

giveConsent

typescript
typescript
function giveConsent(provider: ConsentProvider): Promise<void>
function revokeConsent(provider: ConsentProvider): Promise<void>

giveConsent(provider) allows the party, now and the next time. revokeConsent(provider) takes it back, and the content asks again. Both update the state and the cookie at once, and when someone is signed in they also save the full list in the account. If saving fails, the local answer stays, and the account decides again at the next sync. getConsents() lists the allowed parties.

When to use

Call giveConsent only from a clear action of the visitor, like a button that says what will happen. revokeConsent is already offered in the Privacy tab of the settings in every app that uses AppShell.

When not to use, and what to use instead

Never call giveConsent yourself on load, or because someone kept scrolling. That is not consent.

Live example

Checking the session…
Result Consent for youtube: not allowed Cookie: consent_cache= You are signed out: the answer is only in the cookie of this browser.
These buttons really change the consent for youtube: in the cookie, and in the account when you are signed in. A YoutubeEmbed on this site will load or ask again.
Nothing to do in an app that uses AppShell: the engine loads the account answer when the session starts, and the Privacy tab in the settings lets people change it. Only call the functions yourself when you load content from another party in your own component.
To add a party, add its id to CONSENT_PROVIDERS in the engine and give it a label in the Privacy tab (the type check points to the place). The backend needs the id in its own list of allowed ids too.
svelte
svelte
<Button onclick={() => giveConsent("youtube")}>Allow YouTube</Button>
<Button onclick={() => revokeConsent("youtube")}>Take it back</Button>