[GH-ISSUE #1840] Session doesn't work with SvelteKit #8940

Closed
opened 2026-04-13 04:11:19 -05:00 by GiteaMirror · 2 comments
Owner

Originally created by @v4iv on GitHub (Mar 16, 2025).
Original GitHub issue: https://github.com/better-auth/better-auth/issues/1840

Is this suited for github?

  • Yes, this is suited for github

To Reproduce

  1. Create a Sveltekit project with Drizzle and Tailwind
  2. Set up better auth according to the documents: https://www.better-auth.com/docs/integrations/svelte-kit
  3. Sign Up (this works) then Sign In (This does create a session entry in the Database)
  4. Try to get Session on client side or server side

Current vs. Expected behavior

Trying to get session on client side with authClient.useSession(); returns: { data: null error: null isPending: true isRefetching: false refetch: function() }

Trying to get session on server side await auth.api.getSession({ headers: request.headers }); returns: null

Not even error is returned.

What version of Better Auth are you using?

1.2.4

Provide environment information

- OS: Mac OS v15.3.2
- Browser: Chrome
- Svelte: 5
- Svelte Kit: 2.16.0
- Drizzle-kit: 0.30.2
- Drizzle-orm: 0.30.2

Which area(s) are affected? (Select all that apply)

Backend, Client

Auth config (if applicable)

import { betterAuth } from 'better-auth'
import { drizzleAdapter } from 'better-auth/adapters/drizzle'
import { username } from 'better-auth/plugins'

import { db } from '../db'

export const auth = betterAuth({
	database: drizzleAdapter(db, {
		provider: 'pg' // or "pg" or "mysql"
	}),
	emailAndPassword: {
		enabled: true,
		sendResetPassword: async ({ url }) => {
			console.log('Reset Password Link: ', url)
		}
	},
	plugins: [username()]
})

Additional context

Auth Client:

import { createAuthClient } from 'better-auth/svelte' // make sure to import from better-auth/svelte
import { usernameClient } from 'better-auth/client/plugins'
import { env } from '$env/dynamic/public'

export const authClient = createAuthClient({
	// you can pass client configuration here
	baseURL: env.PUBLIC_BETTER_AUTH_URL, // the base url of your auth server
	plugins: [usernameClient()]
})

export const { signIn, signUp, useSession } = authClient

hooks.server.ts:

import type { Handle } from '@sveltejs/kit'
import { svelteKitHandler } from 'better-auth/svelte-kit'

import { auth } from '$lib/server/auth'

export const handle: Handle = async ({ event, resolve }) => {
	return svelteKitHandler({ event, resolve, auth })
}
Originally created by @v4iv on GitHub (Mar 16, 2025). Original GitHub issue: https://github.com/better-auth/better-auth/issues/1840 ### Is this suited for github? - [x] Yes, this is suited for github ### To Reproduce 1. Create a Sveltekit project with Drizzle and Tailwind 2. Set up better auth according to the documents: https://www.better-auth.com/docs/integrations/svelte-kit 3. Sign Up (this works) then Sign In (This does create a session entry in the Database) 4. Try to get Session on client side or server side ### Current vs. Expected behavior Trying to get session on client side with `authClient.useSession();` returns: ```{ data: null error: null isPending: true isRefetching: false refetch: function() }``` Trying to get session on server side `await auth.api.getSession({ headers: request.headers });` returns: `null` Not even error is returned. ### What version of Better Auth are you using? 1.2.4 ### Provide environment information ```bash - OS: Mac OS v15.3.2 - Browser: Chrome - Svelte: 5 - Svelte Kit: 2.16.0 - Drizzle-kit: 0.30.2 - Drizzle-orm: 0.30.2 ``` ### Which area(s) are affected? (Select all that apply) Backend, Client ### Auth config (if applicable) ```typescript import { betterAuth } from 'better-auth' import { drizzleAdapter } from 'better-auth/adapters/drizzle' import { username } from 'better-auth/plugins' import { db } from '../db' export const auth = betterAuth({ database: drizzleAdapter(db, { provider: 'pg' // or "pg" or "mysql" }), emailAndPassword: { enabled: true, sendResetPassword: async ({ url }) => { console.log('Reset Password Link: ', url) } }, plugins: [username()] }) ``` ### Additional context Auth Client: ``` import { createAuthClient } from 'better-auth/svelte' // make sure to import from better-auth/svelte import { usernameClient } from 'better-auth/client/plugins' import { env } from '$env/dynamic/public' export const authClient = createAuthClient({ // you can pass client configuration here baseURL: env.PUBLIC_BETTER_AUTH_URL, // the base url of your auth server plugins: [usernameClient()] }) export const { signIn, signUp, useSession } = authClient ``` `hooks.server.ts`: ``` import type { Handle } from '@sveltejs/kit' import { svelteKitHandler } from 'better-auth/svelte-kit' import { auth } from '$lib/server/auth' export const handle: Handle = async ({ event, resolve }) => { return svelteKitHandler({ event, resolve, auth }) } ```
GiteaMirror added the lockedbug labels 2026-04-13 04:11:19 -05:00
Author
Owner

@mvdve commented on GitHub (Apr 7, 2025):

I'm using sessions in sveltekit without issues, running better-auth 1.2.5 and SqLite, but the database should not be an issue in this case.

I did provide the Drizzle schema within the betterAuth config, as it would not load it automatically.

<!-- gh-comment-id:2782643773 --> @mvdve commented on GitHub (Apr 7, 2025): I'm using sessions in sveltekit without issues, running better-auth 1.2.5 and SqLite, but the database should not be an issue in this case. I did provide the Drizzle schema within the betterAuth config, as it would not load it automatically.
Author
Owner

@v4iv commented on GitHub (Apr 7, 2025):

ok then I guess I'll close the issue

<!-- gh-comment-id:2783365290 --> @v4iv commented on GitHub (Apr 7, 2025): ok then I guess I'll close the issue
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/better-auth#8940