All files / components/ContentfulLoginLink ContentfulLoginLink.svelte

0% Statements 0/37
0% Branches 0/1
0% Functions 0/1
0% Lines 0/37

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38                                                                           
<script lang="ts">
  import type { ComponentProps } from "svelte";
  import {
    PUBLIC_CONTENTFUL_OAUTH_ENDPOINT,
    PUBLIC_CONTENTFUL_OAUTH_CLIENT_ID,
    PUBLIC_CONTENTFUL_OAUTH_CLIENT_REDIRECT_URI,
  } from "$env/static/public";
  import { page } from "$app/stores";
  import Link from "$lib/components/Link";

  type $$Props = Omit<ComponentProps<Link>, "href">;

  const baseURL = PUBLIC_CONTENTFUL_OAUTH_ENDPOINT;

  const defaultLoginParams = {
    response_type: "token",
    scope: "content_management_read",
    client_id: PUBLIC_CONTENTFUL_OAUTH_CLIENT_ID,
    redirect_uri: encodeURIComponent(PUBLIC_CONTENTFUL_OAUTH_CLIENT_REDIRECT_URI),
  };

  $: loginParams = {
    ...defaultLoginParams,
    state: encodeURIComponent($page.url.pathname + $page.url.search + $page.url.hash),
  };

  $: loginLink = `${baseURL}?${Object.entries(loginParams)
    .map(([key, value]) => `${key}=${value}`)
    .join("&")}`;

  let className: string | undefined = undefined;
  export { className as class };
</script>

<Link class={className} href={loginLink} {...$$restProps}>
  <slot>Login</slot>
</Link>