[PR #403] [CLOSED] Update sveltekit integration docs #3274

Closed
opened 2026-03-13 10:46:27 -05:00 by GiteaMirror · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/better-auth/better-auth/pull/403
Author: @nalonix
Created: 11/3/2024
Status: Closed

Base: mainHead: main


📝 Commits (3)

📊 Changes

2 files changed (+181 additions, -83 deletions)

View changed files

📝 docs/content/docs/integrations/svelte-kit.mdx (+149 -62)
📝 packages/better-auth/src/integrations/svelte-kit.ts (+32 -21)

📄 Description

Update to sveltekit handler.
It's intended to attach the user session to events.locals so that it's available through out the app.

This will give access to the session (and user data) in Page / Layout load functions [ event.locals.session | event.locals.user ] and Form actions [ event.locals.session | event.locals.user ].

For Svelte files, we can grab the session in a load function and pass it down to the svelte page / layout file.

For TS: requires updating Locals interface in app.d.ts by adding session and user properties with types Session and User respectively [ from "better-auth/types" ].

IF THIS IS NOT A VIABLE OPTION. WE CAN JUST UPDATE THE DOCS. SEE BELOW.

The session and user can be attached to event.locals inside hooks.server.ts.

import { auth } from '$lib/auth'; // path to your auth file
import { svelteKitHandler } from 'better-auth/svelte-kit';

export async function handle({ event, resolve }) {
	const session = await auth.api.getSession({
		headers: event.request.headers
	});

	event.locals.session = session?.session;
	event.locals.user = session?.user;

	return svelteKitHandler({ event, resolve, auth });
}

and update app.d.ts

import type { User, Session } from 'better-auth/types';

declare global {
	namespace App {
		interface Locals {
			session: Session | undefined;
			user: User | undefined;
		}
	}
}

export {};

Acessing sessions / user and passing down as page data:

import { redirect } from '@sveltejs/kit';

export async function load({ locals }) {
	const session = locals.session;

	if (!session) {
		throw redirect(302, '/auth/signin');
	}

	return {
		session
	};
}

Now session is accessible inside the page like every other page data:

// Svelte 5 
	const { data } = $props();
	console.log(data.session);

// Previous verions 
    export let data;
    console.log(data.session)
    

🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/better-auth/better-auth/pull/403 **Author:** [@nalonix](https://github.com/nalonix) **Created:** 11/3/2024 **Status:** ❌ Closed **Base:** `main` ← **Head:** `main` --- ### 📝 Commits (3) - [`39b9120`](https://github.com/better-auth/better-auth/commit/39b9120eef9ea0595fd5c1323c4ecc0b09d2570f) Update svelte-kit.ts - [`1cfc971`](https://github.com/better-auth/better-auth/commit/1cfc971feb259986e03045ff74da742d90d4fe23) Update svelte-kit.ts - [`67032d2`](https://github.com/better-auth/better-auth/commit/67032d27646f88b415f63458cdd5c209a3aa5948) Update svelte-kit.mdx ### 📊 Changes **2 files changed** (+181 additions, -83 deletions) <details> <summary>View changed files</summary> 📝 `docs/content/docs/integrations/svelte-kit.mdx` (+149 -62) 📝 `packages/better-auth/src/integrations/svelte-kit.ts` (+32 -21) </details> ### 📄 Description Update to sveltekit handler. It's intended to attach the user session to **events.locals** so that it's available through out the app. This will give access to the session (and user data) in Page / Layout load functions [ _event.locals.session_ | _event.locals.user_ ] and Form actions [ _event.locals.session_ | _event.locals.user_ ]. For Svelte files, we can grab the session in a load function and pass it down to the svelte page / layout file. For TS: requires updating Locals interface in `app.d.ts` by adding session and user properties with types Session and User respectively [ from _"better-auth/types"_ ]. **IF THIS IS NOT A VIABLE OPTION. WE CAN JUST UPDATE THE DOCS. SEE BELOW.** The session and user can be attached to **event.locals** inside `hooks.server.ts`. ``` import { auth } from '$lib/auth'; // path to your auth file import { svelteKitHandler } from 'better-auth/svelte-kit'; export async function handle({ event, resolve }) { const session = await auth.api.getSession({ headers: event.request.headers }); event.locals.session = session?.session; event.locals.user = session?.user; return svelteKitHandler({ event, resolve, auth }); } ``` and update `app.d.ts` ``` import type { User, Session } from 'better-auth/types'; declare global { namespace App { interface Locals { session: Session | undefined; user: User | undefined; } } } export {}; ``` Acessing sessions / user and passing down as page data: ``` import { redirect } from '@sveltejs/kit'; export async function load({ locals }) { const session = locals.session; if (!session) { throw redirect(302, '/auth/signin'); } return { session }; } ``` Now session is accessible inside the page like every other page data: ``` // Svelte 5 const { data } = $props(); console.log(data.session); // Previous verions export let data; console.log(data.session) ``` --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
GiteaMirror added the pull-request label 2026-03-13 10:46:27 -05:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/better-auth#3274