[GH-ISSUE #3550] sveltekitCookies() expects async factory, docs show sync example #18272

Closed
opened 2026-04-15 16:41:40 -05:00 by GiteaMirror · 8 comments
Owner

Originally created by @screenfluent on GitHub (Jul 22, 2025).
Original GitHub issue: https://github.com/better-auth/better-auth/issues/3550

Is this suited for github?

  • Yes, this is suited for github

To Reproduce

  1. Install better-auth@1.3.2 in a fresh SvelteKit project.
  2. Paste the snippet from docs.
  3. npm run check ➝ TS2345.

Current vs. Expected behavior

Current behaviour

The official docs & README show this example:

import { BetterAuth } from "better-auth";
import { sveltekitCookies } from "better-auth/svelte-kit";
import { getRequestEvent } from "$app/server";

export const auth = betterAuth({
  // ... your config
  plugins: [sveltekitCookies(getRequestEvent)],
});

But TypeScript throws:

TS2345: Argument of type '() => RequestEvent …' is not assignable to parameter of type '() => Promise<RequestEvent …>'

because the published types declare:

export declare function sveltekitCookies(
  getEvent: () => Promise<RequestEvent | undefined>
): BetterAuthPlugin;

Work‑around

Wrap the sync call in an async arrow:

plugins: [ sveltekitCookies(async () => getRequestEvent()) 

This silences TS and works at runtime.

Expected behaviour

Either:

  1. Types accept a synchronous factory (() => RequestEvent | undefined), or
  2. Docs mention the need to wrap getRequestEvent in async () => ….

What version of Better Auth are you using?

1.3.2

Provide environment information

- Framework: SvelteKit 2.25.1
- Adapter: Node
- TS version: 5.8.3

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

Backend

Auth config (if applicable)

import { betterAuth } from 'better-auth';
import { drizzleAdapter } from 'better-auth/adapters/drizzle';
import { db } from './db';
import { sveltekitCookies } from 'better-auth/svelte-kit';
import { getRequestEvent } from '$app/server';

export const auth = betterAuth({
	database: drizzleAdapter(db, {
		provider: 'sqlite'
	}),
	emailAndPassword: {
		enabled: true
	},
	plugins: [sveltekitCookies(async () => getRequestEvent())],
	rateLimit: {
		enabled: true,
		window: 60,
		max: 100,
		customRules: {
			'/sign-in/email': { window: 10, max: 3 }
		}
	}
});

Additional context

Proposed fix

Add an overload to sveltekitCookies types:

export declare function sveltekitCookies(
  getEvent: () => RequestEvent | undefined
): BetterAuthPlugin;

No runtime change required — only typings.

Happy to submit a PR if you’d prefer, but filing as issue first for visibility.

Thanks for the awesome library!

Originally created by @screenfluent on GitHub (Jul 22, 2025). Original GitHub issue: https://github.com/better-auth/better-auth/issues/3550 ### Is this suited for github? - [x] Yes, this is suited for github ### To Reproduce 1. Install better-auth@1.3.2 in a fresh SvelteKit project. 2. Paste the snippet from docs. 3. npm run check ➝ TS2345. ### Current vs. Expected behavior ### Current behaviour The official docs & README show this example: ```ts title="lib/auth.ts" import { BetterAuth } from "better-auth"; import { sveltekitCookies } from "better-auth/svelte-kit"; import { getRequestEvent } from "$app/server"; export const auth = betterAuth({ // ... your config plugins: [sveltekitCookies(getRequestEvent)], }); ``` But TypeScript throws: ``` TS2345: Argument of type '() => RequestEvent …' is not assignable to parameter of type '() => Promise<RequestEvent …>' ``` because the published types declare: ```ts export declare function sveltekitCookies( getEvent: () => Promise<RequestEvent | undefined> ): BetterAuthPlugin; ``` ### Work‑around Wrap the sync call in an async arrow: ```ts plugins: [ sveltekitCookies(async () => getRequestEvent()) ``` This silences TS and works at runtime. ### Expected behaviour Either: 1. Types accept a synchronous factory (`() => RequestEvent | undefined`), or 2. Docs mention the need to wrap `getRequestEvent in async () => …`. ### What version of Better Auth are you using? 1.3.2 ### Provide environment information ```bash - Framework: SvelteKit 2.25.1 - Adapter: Node - TS version: 5.8.3 ``` ### Which area(s) are affected? (Select all that apply) Backend ### Auth config (if applicable) ```typescript import { betterAuth } from 'better-auth'; import { drizzleAdapter } from 'better-auth/adapters/drizzle'; import { db } from './db'; import { sveltekitCookies } from 'better-auth/svelte-kit'; import { getRequestEvent } from '$app/server'; export const auth = betterAuth({ database: drizzleAdapter(db, { provider: 'sqlite' }), emailAndPassword: { enabled: true }, plugins: [sveltekitCookies(async () => getRequestEvent())], rateLimit: { enabled: true, window: 60, max: 100, customRules: { '/sign-in/email': { window: 10, max: 3 } } } }); ``` ### Additional context ### Proposed fix Add an overload to sveltekitCookies types: ```ts export declare function sveltekitCookies( getEvent: () => RequestEvent | undefined ): BetterAuthPlugin; ``` No runtime change required — only typings. Happy to submit a PR if you’d prefer, but filing as issue first for visibility. Thanks for the awesome library!
GiteaMirror added the locked label 2026-04-15 16:41:40 -05:00
Author
Owner

@Kinfe123 commented on GitHub (Jul 22, 2025):

there is already pr for that #3533

<!-- gh-comment-id:3104946392 --> @Kinfe123 commented on GitHub (Jul 22, 2025): there is already pr for that #3533
Author
Owner

@rebasecase commented on GitHub (Jul 31, 2025):

@Kinfe123

With factory:

Type 'Promise<RequestEvent<LayoutParams<"/">, any>>' is missing the following properties from type 'RequestEvent<Partial<Record<string, string>>, string | null>': cookies, fetch, getClientAddress, locals, and 8 more.ts(2740)
svelte-kit.d.ts(33, 51): The expected type comes from the return type of this signature.

Without factory:

Argument of type '() => RequestEvent<LayoutParams<"/">, any>' is not assignable to parameter of type '() => RequestEvent<Partial<Record<string, string>>, string | null>'.
  Type 'RequestEvent<LayoutParams<"/">, any>' is not assignable to type 'RequestEvent<Partial<Record<string, string>>, string | null>'.
    Type 'LayoutParams<"/">' is not assignable to type 'Partial<Record<string, string>>'.
      Type 'undefined' is not assignable to type 'Partial<Record<string, string>>'.ts(2345)

"better-auth": "^1.3.4",
"svelte": "^5.37.1",
"@sveltejs/kit": "^2.26.1",

<!-- gh-comment-id:3140021328 --> @rebasecase commented on GitHub (Jul 31, 2025): @Kinfe123 With factory: ``` Type 'Promise<RequestEvent<LayoutParams<"/">, any>>' is missing the following properties from type 'RequestEvent<Partial<Record<string, string>>, string | null>': cookies, fetch, getClientAddress, locals, and 8 more.ts(2740) svelte-kit.d.ts(33, 51): The expected type comes from the return type of this signature. ``` Without factory: ``` Argument of type '() => RequestEvent<LayoutParams<"/">, any>' is not assignable to parameter of type '() => RequestEvent<Partial<Record<string, string>>, string | null>'. Type 'RequestEvent<LayoutParams<"/">, any>' is not assignable to type 'RequestEvent<Partial<Record<string, string>>, string | null>'. Type 'LayoutParams<"/">' is not assignable to type 'Partial<Record<string, string>>'. Type 'undefined' is not assignable to type 'Partial<Record<string, string>>'.ts(2345) ``` `"better-auth": "^1.3.4",` `"svelte": "^5.37.1",` `"@sveltejs/kit": "^2.26.1",`
Author
Owner

@rebasecase commented on GitHub (Aug 3, 2025):

Same behaviour happens for

"better-auth": "^1.3.4",
"svelte": "^5.37.3",
"@sveltejs/kit": "^2.27.0",
<!-- gh-comment-id:3148599348 --> @rebasecase commented on GitHub (Aug 3, 2025): Same behaviour happens for ``` "better-auth": "^1.3.4", "svelte": "^5.37.3", "@sveltejs/kit": "^2.27.0", ```
Author
Owner

@DarthGigi commented on GitHub (Aug 7, 2025):

Having the same type error:

Argument of type '() => RequestEvent<LayoutParams<"/">, any>' is not assignable to parameter of type '() => RequestEvent<Partial<Record<string, string>>, string | null>'.
  Type 'RequestEvent<LayoutParams<"/">, any>' is not assignable to type 'RequestEvent<Partial<Record<string, string>>, string | null>'.
    Type 'LayoutParams<"/">' is not assignable to type 'Partial<Record<string, string>>'.
      Type 'undefined' is not assignable to type 'Partial<Record<string, string>>'.ts(2345)
<!-- gh-comment-id:3165685650 --> @DarthGigi commented on GitHub (Aug 7, 2025): Having the same type error: ```ts Argument of type '() => RequestEvent<LayoutParams<"/">, any>' is not assignable to parameter of type '() => RequestEvent<Partial<Record<string, string>>, string | null>'. Type 'RequestEvent<LayoutParams<"/">, any>' is not assignable to type 'RequestEvent<Partial<Record<string, string>>, string | null>'. Type 'LayoutParams<"/">' is not assignable to type 'Partial<Record<string, string>>'. Type 'undefined' is not assignable to type 'Partial<Record<string, string>>'.ts(2345) ```
Author
Owner

@rebasecase commented on GitHub (Aug 8, 2025):

Strangely it started working (well, stopped complaining) today.

Can you try purging node modules and any language server cache - maybe that was why?

<!-- gh-comment-id:3166770736 --> @rebasecase commented on GitHub (Aug 8, 2025): Strangely it started working (well, stopped complaining) today. Can you try purging node modules and any language server cache - maybe that was why?
Author
Owner

@DarthGigi commented on GitHub (Aug 8, 2025):

@rebasecase That did not work for me, unfortunately.

Here are my package versions:

"@sveltejs/adapter-node": "^5.2.14",
"@sveltejs/kit": "^2.27.3",
"@sveltejs/vite-plugin-svelte": "^6.1.1",
"svelte": "^5.38.0",
"svelte-check": "^4.3.1",
"better-auth": "^1.3.4",
"@types/node": "^24.2.0",

At the time of writing, these versions are the latest for these packages

I think this issue should be re-opened, no?

<!-- gh-comment-id:3168012830 --> @DarthGigi commented on GitHub (Aug 8, 2025): @rebasecase That did not work for me, unfortunately. Here are my package versions: ```json "@sveltejs/adapter-node": "^5.2.14", "@sveltejs/kit": "^2.27.3", "@sveltejs/vite-plugin-svelte": "^6.1.1", "svelte": "^5.38.0", "svelte-check": "^4.3.1", "better-auth": "^1.3.4", "@types/node": "^24.2.0", ``` At the time of writing, these versions are the latest for these packages I think this issue should be re-opened, no?
Author
Owner

@rebasecase commented on GitHub (Aug 10, 2025):

@DarthGigi no you're right it doesn't work

<!-- gh-comment-id:3172724116 --> @rebasecase commented on GitHub (Aug 10, 2025): @DarthGigi no you're right it doesn't work
Author
Owner

@rebasecase commented on GitHub (Aug 27, 2025):

Fixed here

https://github.com/better-auth/better-auth/pull/3994

<!-- gh-comment-id:3226888611 --> @rebasecase commented on GitHub (Aug 27, 2025): Fixed here https://github.com/better-auth/better-auth/pull/3994
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/better-auth#18272