# Unidy Web SDK — Produktempfehlung abbestellen

The landing page behind the unsubscribe link in the Produktempfehlung mailing. The visitor arrives from an email rather than a login: the mailing appends a preference token to the link, and u-newsletter-root takes that token as identification, so the page works without an account. It shows one newsletter and one action: leaving it. The newsletter's topics are set by the club, so the recipient is not offered a say over them here.

## Setup

Every example below assumes the SDK is loaded and configured once per page. The
import map lets the headless examples use the bare `@unidy.io/sdk` specifier —
the same import a bundler/npm setup uses; drop the import map if you install the
package instead.

```html
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@unidy.io/sdk@1.13.1/dist/sdk/sdk.css">
<script type="importmap">
  { "imports": { "@unidy.io/sdk": "https://cdn.jsdelivr.net/npm/@unidy.io/sdk@1.13.1/dist/sdk/index.esm.js" } }
</script>
<script type="module" src="https://cdn.jsdelivr.net/npm/@unidy.io/sdk@1.13.1/dist/sdk/sdk.esm.js"></script>

<u-config
  base-url="https://hannover96.staging.unidy.de"
  api-key="your-api-key"
  locale="en"
  check-signed-in="true"></u-config>
```

## Unsubscribe from an email link

Everything is nested in u-conditional-render when="newsletter.loggedIn", which is true for a preference-token visitor as much as for a signed-in member — that is what makes a passwordless unsubscribe page possible. Inside it, newsletter.subscribed decides between the card and the receipt: unsubscribing drops the subscription from the store, so the confirmation replaces the card on the spot. The newsletter carries two preferences, but neither is rendered — they belong to the club, so the page shows the subscription and the way out of it, nothing else. A visitor without any token sees nothing at all — there is no second entry point to explain. A visitor whose token no longer leads to a Produktempfehlung subscription (the link in an old mailing, clicked a second time) gets a short notice instead: the snippet asks the API first, because u-newsletter-root answers an unauthorized token with a "session has expired" toast and an empty page. The 96-Newsletter is deliberately absent — someone who came to leave one list should not be handed another.

- **No login required** — the preference token from the email link identifies the visitor
- **One action only** — the preferences exist on the newsletter but are not offered here
- **Receipt in place** — newsletter.subscribed flips and the confirmation replaces the card
- **Nothing without a token** — the page is reached from the mailing and renders only for its visitor
- **Notice for a used-up link** — a token whose subscription is gone shows a short note, not a session-expired toast

```html
<!-- Landing page for the unsubscribe link in the Produktempfehlung mailing.

     The visitor arrives from an email, not from a login: the mailing appends a
     preference token to the link (…?preference_token=…&email=…), and
     u-newsletter-root accepts that token as identification. newsletter.loggedIn
     is therefore true for a token visitor as much as for a signed-in member —
     which is why this snippet keys off that and not off u-signed-in.

     Only Produktempfehlung appears here. The 96-Newsletter is deliberately
     absent: someone who came to leave one list should not be handed another.

     A visitor without any token renders nothing at all — the page is reached from
     the mailing and nowhere else. A visitor WITH a token that no longer leads to a
     Produktempfehlung subscription (typically: clicking the link in an old mailing
     a second time) gets a short notice instead — see the script below.

     The button label comes from the SDK's translations. Put
     custom-translations on <u-config> to relabel it, e.g.
     {"de":{"newsletter":{"buttons":{"unsubscribe":"Abmelden"}}}}. -->
<template id="pe-unsub-template">
  <u-newsletter-root>
    <u-conditional-render when="newsletter.loggedIn">
      <!-- Identified and still subscribed: one action, unsubscribe. -->
      <u-conditional-render when="newsletter.subscribed" is="ns_productrelated_with_prefs">
        <div class="rounded-xl border border-border p-4">
          <p class="font-semibold">Produktempfehlung</p>
          <p class="mt-1 text-sm text-text-light">
            Du erhältst Produktempfehlungen von Hannover 96.
          </p>

          <div class="mt-4 border-t border-border pt-4">
            <!-- The newsletter's preferences (salesandservices, kgaa) are
                 deliberately not rendered: the club sets the topics, the
                 recipient does not. Leaving the newsletter is the only action
                 this page offers.

                 The card only renders while the subscription exists, so the
                 button is always in its unsubscribe state here. There is no way
                 to subscribe from this page by design. -->
            <u-newsletter-toggle-subscription-button
              internal-name="ns_productrelated_with_prefs"
              class-name="text-sm font-medium rounded-lg px-3 py-1 cursor-pointer"
              subscribe-class-name="text-white bg-accent hover:bg-accent-hover"
              unsubscribe-class-name="text-danger border border-error-border bg-white hover:bg-error-bg"
            ></u-newsletter-toggle-subscription-button>
          </div>
        </div>
      </u-conditional-render>

      <!-- Identified, no subscription left: the receipt. Unsubscribing drops the
           entry from the store, so this replaces the card the moment it happens. -->
      <u-conditional-render when="newsletter.subscribed" is="ns_productrelated_with_prefs" not>
        <div class="rounded-xl border border-success-border bg-success-bg p-4">
          <p class="font-semibold text-success-text">Du bist abgemeldet.</p>
          <p class="mt-1 text-sm text-text-light">
            Du erhältst keine Produktempfehlungen mehr. Andere Newsletter von Hannover 96 sind davon
            nicht betroffen.
          </p>
        </div>
      </u-conditional-render>
    </u-conditional-render>
  </u-newsletter-root>
</template>
<!-- Shown instead of the card when the link's token leads nowhere. -->
<template id="pe-unsub-gone-template">
  <div class="rounded-xl border border-border p-4">
    <p class="font-semibold">Kein aktives Abo.</p>
    <p class="mt-1 text-sm text-text-light">
      Du erhältst keine Produktempfehlungen von Hannover 96. Möglicherweise hast du dich bereits
      abgemeldet.
    </p>
  </div>
</template>
<div id="pe-unsub-mount"></div>

<script type="module">
  // u-newsletter-root reads the auth state in componentWillLoad without waiting
  // for <u-config check-signed-in> to settle, so a signed-in visitor would get
  // an empty center on a cold load. auth.ready resolves either way — including
  // for a token visitor who is not signed in at all — and the preference token
  // stays in location.search, so deferring the mount costs nothing.
  import { Auth, getUnidyClient } from "@unidy.io/sdk";

  const auth = await Auth.getInstance();
  await auth.ready;

  // A preference token belongs to one subscription. Once that subscription is
  // gone (the visitor already unsubscribed and clicks the link again) the API
  // answers "unauthorized" — and u-newsletter-root reacts to that with a
  // "session has expired" toast and an empty page. So ask first, and only mount
  // the root when there is something to show. The root still finds the token in
  // location.search afterwards.
  async function tokenLeadsToSubscription() {
    const token = new URLSearchParams(location.search).get("preference_token");
    if (!token) return true; // no token: nothing to check, the root stays blank as before

    const [error, subscriptions] = await getUnidyClient().newsletters.list({
      options: { preferenceToken: token },
    });
    if (error === "unauthorized") return false;
    if (error !== null) return true; // network hiccup etc.: let the root try
    return subscriptions.some((s) => s.newsletter_internal_name === "ns_productrelated_with_prefs");
  }

  const templateId = (await tokenLeadsToSubscription())
    ? "pe-unsub-template"
    : "pe-unsub-gone-template";
  document
    .getElementById("pe-unsub-mount")
    .append(document.getElementById(templateId).content.cloneNode(true));
</script>
```

The mailing has to point at this page with the token attached, e.g. `https://<host>/pe-unsubscribe?preference_token=<token>&email=<email>`. Unidy substitutes `{preference_token}`, `{email}` and `{newsletter_internal_name}` placeholders in a configured redirect_uri and appends whatever is left over as query parameters. The button label is the SDK's own translation; relabel it with `custom-translations` on `<u-config>` (this demo sets `newsletter.buttons.unsubscribe` to `Abmelden` in `Layout.astro`) — the integrating site needs the same on its own `<u-config>`. When the SDK requests the login mail itself the redirect_uri is validated against the SDK client's allowed_hosts — an origin that is not allowlisted fails with `origin_not_allowed` and no mail is sent.

Live demo: /pe-unsubscribe#pe-unsubscribe

## Further reading

- Full component reference: https://github.com/UnidyID/unidy-sdk/blob/HEAD/packages/sdk/readme.md
- Official quick-start examples: https://github.com/UnidyID/unidy-sdk/blob/HEAD/packages/sdk/quick-start-examples.md
- Package: https://www.npmjs.com/package/@unidy.io/sdk
- All guides: /llms.txt
