Passing event object to api function like auth.api.signInEmail() for sveltekit cookies helper #851

Closed
opened 2026-03-13 08:07:12 -05:00 by GiteaMirror · 2 comments
Owner

Originally created by @islamzaoui on GitHub (Mar 14, 2025).

Hi, I want to create a sveltekit cookie plugin similar to nextCookies

I need to pass the sveltekit event object t to the API function to access the cookies API event.cookies.set(...) like this:

export const actions = {
    default: async (event) => {
        const form = superValidate(event, zod(schema))
        if(!form.valid) return fail(402, {form})

        const result = auth.api.signInEmail({
           body: form.data,
           event
        })

        ....
    }
}

Then in the plugin side update the nextjs one to use the event cookies object instead of the cookies of next/headers

Originally created by @islamzaoui on GitHub (Mar 14, 2025). Hi, I want to create a sveltekit cookie plugin similar to [nextCookies](https://github.com/better-auth/better-auth/blob/main/packages/better-auth/src/integrations/next-js.ts) I need to pass the sveltekit event object t to the API function to access the cookies API `event.cookies.set(...)` like this: ```ts export const actions = { default: async (event) => { const form = superValidate(event, zod(schema)) if(!form.valid) return fail(402, {form}) const result = auth.api.signInEmail({ body: form.data, event }) .... } } ``` Then in the plugin side update the nextjs one to use the event cookies object instead of the cookies of `next/headers`
Author
Owner

@islamzaoui commented on GitHub (Mar 14, 2025):

Soon, we can use getRequestEvent() "I just saw the pull request XD" an upcoming feature in sveltekit to make the plugin just like the Next.js one:

const sveltekitCookies = (): BetterAuthPlugin => ({
	id: 'sveltekit-cookies',
	hooks: {
		after: [
			{
				matcher(ctx) {
					return true;
				},
				handler: createAuthMiddleware(async (ctx) => {
					const returned = ctx.context.responseHeaders;
					if ('_flag' in ctx && ctx._flag === 'router') {
						return;
					}
					if (returned instanceof Headers) {
						const setCookies = returned?.get('set-cookie');
						if (!setCookies) return;
						const parsed = parseSetCookieHeader(setCookies);
						const { getRequestEvent } = await import('$app/server');
						const event = await getRequestEvent();
						parsed.forEach((value, key) => {
							if (!key) return;
							const opts = {
								sameSite: value.samesite,
								secure: value.secure,
								maxAge: value['max-age'],
								httpOnly: value.httponly,
								domain: value.domain,
								path: value.path
							} as const;
							try {
								event.cookies.set(key, decodeURIComponent(value.value), opts);
							} catch (e) {
								// this will fail if the cookie is being set on server component
							}
						});
						return;
					}
				})
			}
		]
	}
});
@islamzaoui commented on GitHub (Mar 14, 2025): Soon, we can use [`getRequestEvent()`](https://github.com/sveltejs/kit/pull/13582) _"I just saw the pull request XD"_ an upcoming feature in sveltekit to make the plugin just like the Next.js one: ```ts const sveltekitCookies = (): BetterAuthPlugin => ({ id: 'sveltekit-cookies', hooks: { after: [ { matcher(ctx) { return true; }, handler: createAuthMiddleware(async (ctx) => { const returned = ctx.context.responseHeaders; if ('_flag' in ctx && ctx._flag === 'router') { return; } if (returned instanceof Headers) { const setCookies = returned?.get('set-cookie'); if (!setCookies) return; const parsed = parseSetCookieHeader(setCookies); const { getRequestEvent } = await import('$app/server'); const event = await getRequestEvent(); parsed.forEach((value, key) => { if (!key) return; const opts = { sameSite: value.samesite, secure: value.secure, maxAge: value['max-age'], httpOnly: value.httponly, domain: value.domain, path: value.path } as const; try { event.cookies.set(key, decodeURIComponent(value.value), opts); } catch (e) { // this will fail if the cookie is being set on server component } }); return; } }) } ] } }); ```
Author
Owner

@dosubot[bot] commented on GitHub (Jun 13, 2025):

Hi, @IslamZaoui. I'm Dosu, and I'm helping the better-auth team manage their backlog. I'm marking this issue as stale.

Issue Summary:

  • You are developing a SvelteKit cookie plugin inspired by the Next.js plugin, nextCookies.
  • The plugin aims to utilize the SvelteKit event object for cookie management, moving away from Next.js headers cookies.
  • You mentioned the upcoming SvelteKit feature getRequestEvent() as a key component for this development.
  • A code snippet was provided to illustrate the potential implementation using getRequestEvent().
  • The issue remains unresolved as of now.

Next Steps:

  • Please confirm if this issue is still relevant to the latest version of the better-auth repository by commenting on the issue.
  • If no updates are provided, the issue will be automatically closed in 7 days.

Thank you for your understanding and contribution!

@dosubot[bot] commented on GitHub (Jun 13, 2025): Hi, @IslamZaoui. I'm [Dosu](https://dosu.dev), and I'm helping the better-auth team manage their backlog. I'm marking this issue as stale. **Issue Summary:** - You are developing a SvelteKit cookie plugin inspired by the Next.js plugin, nextCookies. - The plugin aims to utilize the SvelteKit event object for cookie management, moving away from Next.js headers cookies. - You mentioned the upcoming SvelteKit feature `getRequestEvent()` as a key component for this development. - A code snippet was provided to illustrate the potential implementation using `getRequestEvent()`. - The issue remains unresolved as of now. **Next Steps:** - Please confirm if this issue is still relevant to the latest version of the better-auth repository by commenting on the issue. - If no updates are provided, the issue will be automatically closed in 7 days. Thank you for your understanding and contribution!
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/better-auth#851