Skip to content
TesseraDocs
MenuSvelteKit with Auth.js

SvelteKit with Auth.js

Sign users in to a SvelteKit app with Tessera. Two small files.

View as Markdown

On this page

Sign users in to a SvelteKit app with Tessera. Two small files.

Requires: Node.js 20 or later and a SvelteKit 2 project on Svelte 5.

1. Create the app in Tessera

Sign in to the console at https://platform.kurthlabs.com/tessera with your invitation (see your first sign-in), open your realm and add an app. Choose SvelteKit and where it runs: this computer for now.

The console then shows what you need, each with a copy button:

  • the issuer, https://login.kurthlabs.com/realms/YOUR_REALM
  • the client ID
  • the redirect URI, http://localhost:5173/auth/callback/tessera
  • the client secret, once. Copy it now, or download the .env file. Tessera cannot show it again.

Put the values in .env.local as shown under Environment below, and keep that file out of git.

If you have the tessera CLI (during early access it is by invitation), tessera init does this step: it registers the app, writes .env.local and prints the code for your stack. The console path above needs no download.

2. Add the provider

npm install --save-exact @auth/sveltekit@1.11.3

src/hooks.server.ts

import { SvelteKitAuth } from "@auth/sveltekit";
import { env } from "$env/dynamic/private";

export const { handle, signIn, signOut } = SvelteKitAuth({
  trustHost: true,
  providers: [{
    id: "tessera",
    name: "Tessera",
    type: "oidc",
    issuer: env.TESSERA_ISSUER,
    clientId: env.TESSERA_CLIENT_ID,
    clientSecret: env.TESSERA_CLIENT_SECRET,
  }],
});

3. Add a sign-in button

src/routes/+layout.server.ts

export const load = async (event) => ({ session: await event.locals.auth() });

src/routes/+page.svelte

<script>
  import { page } from "$app/state";
  import { signIn } from "@auth/sveltekit/client";
</script>

{#if page.data.session?.user}
  <p>Signed in as {page.data.session.user.email}</p>
{:else}
  <button onclick={() => signIn("tessera")}>Sign in</button>
{/if}

Run npm run dev and open the page. Click Sign in, approve on Tessera, and you come back with "Signed in as" and your email.

Environment

The console gives you the first three values (the downloaded .env file has them, and so does tessera init). Put them in .env.local:

TESSERA_ISSUER=https://login.kurthlabs.com/realms/YOUR_REALM
TESSERA_CLIENT_ID=YOUR_CLIENT_ID
TESSERA_CLIENT_SECRET=YOUR_CLIENT_SECRET
AUTH_SECRET=YOUR_SESSION_SECRET

AUTH_SECRET is yours, not Tessera's: it signs your app's own session cookie. Generate one with openssl rand -base64 32.

The redirect URI registered for this app is http://localhost:5173/auth/callback/tessera. For production, add your production address to the app's redirect URIs in the console (the same path on https://your-app.example); the local one can stay.