[GH-ISSUE #843] Server Cookies for Svelte #8462

Closed
opened 2026-04-13 03:31:56 -05:00 by GiteaMirror · 4 comments
Owner

Originally created by @johnsutor on GitHub (Dec 10, 2024).
Original GitHub issue: https://github.com/better-auth/better-auth/issues/843

Is your feature request related to a problem? Please describe.
I want to use Svelte Actions to sign in and sign up users. However, this isn't possible without setting cookies on the server.

Describe the solution you'd like
Something akin to the NextJS Server Action Cookies plugin for signing in a user

Describe alternatives you've considered
I tried setting cookies on a successful sign-up, but that did not work out (session isn't returned by the response so session is null).

import { redirect } from '@sveltejs/kit';
import { db } from '$lib/server/db';
import { signupSchema } from '$lib/validators/auth';
import { superValidate, setError, fail } from 'sveltekit-superforms';
import { zod } from 'sveltekit-superforms/adapters';
import { auth } from '$lib/auth';


import type { PageServerLoad, Actions } from './$types';

export const load: PageServerLoad = async ({ request }) => {
	const session = await auth.api.getSession({
		headers: request.headers
	});

	if (session) {
		return redirect(302, '/dashboard');
	}
	return session;
};

export const actions: Actions = {
	default: async (event) => {
		const form = await superValidate(event, zod(signupSchema));

		if (!form.valid) {
			return fail(400, {
				form
			});
		}

		const { name, email, password } = form.data;

		try {
			const result = await auth.api.signUpEmail({
				headers: event.request.headers,
				body: {
					email,
					password,
					name
				},
			})

			if (!result) {
				return fail(400, {
					form
				});
			}

			else {
				event.cookies.set("better-auth.session_token", result?.session?.token ?? '', { path: "/" });
			}
		}

		catch (error) {
			return fail(400, {
				form,
			});
		}

		return {
			form
		};
	}
};
Originally created by @johnsutor on GitHub (Dec 10, 2024). Original GitHub issue: https://github.com/better-auth/better-auth/issues/843 **Is your feature request related to a problem? Please describe.** I want to use Svelte Actions to sign in and sign up users. However, this isn't possible without setting cookies on the server. **Describe the solution you'd like** Something akin to the [NextJS Server Action Cookies plugin](https://www.better-auth.com/docs/integrations/next#server-action-cookies) for signing in a user **Describe alternatives you've considered** I tried [setting cookies](https://svelte.dev/tutorial/kit/cookies) on a successful sign-up, but that did not work out (session isn't returned by the response so session is null). ```js import { redirect } from '@sveltejs/kit'; import { db } from '$lib/server/db'; import { signupSchema } from '$lib/validators/auth'; import { superValidate, setError, fail } from 'sveltekit-superforms'; import { zod } from 'sveltekit-superforms/adapters'; import { auth } from '$lib/auth'; import type { PageServerLoad, Actions } from './$types'; export const load: PageServerLoad = async ({ request }) => { const session = await auth.api.getSession({ headers: request.headers }); if (session) { return redirect(302, '/dashboard'); } return session; }; export const actions: Actions = { default: async (event) => { const form = await superValidate(event, zod(signupSchema)); if (!form.valid) { return fail(400, { form }); } const { name, email, password } = form.data; try { const result = await auth.api.signUpEmail({ headers: event.request.headers, body: { email, password, name }, }) if (!result) { return fail(400, { form }); } else { event.cookies.set("better-auth.session_token", result?.session?.token ?? '', { path: "/" }); } } catch (error) { return fail(400, { form, }); } return { form }; } }; ```
GiteaMirror added the locked label 2026-04-13 03:31:56 -05:00
Author
Owner

@octet-stream commented on GitHub (Dec 12, 2024):

I am not familiar with Svelte, nor SvelteKit, but this is not how you are supposed to set set-cookie header on your own, because better auth is signing session's token. Instead, add asResponse option to signUpEmail method - this way it will return a standard Response object, then access your cookies via response.headers.get("set-cookie"). After you get access to the set-cookie header you can add it to the response whatever it's done in SvelteKit.

<!-- gh-comment-id:2538846936 --> @octet-stream commented on GitHub (Dec 12, 2024): I am not familiar with Svelte, nor SvelteKit, but this is not how you are supposed to set `set-cookie` header on your own, because better auth is signing session's token. Instead, add `asResponse` option to `signUpEmail` method - this way it will return a standard Response object, then access your cookies via `response.headers.get("set-cookie")`. After you get access to the `set-cookie` header you can add it to the response whatever it's done in SvelteKit.
Author
Owner

@johnsutor commented on GitHub (Dec 12, 2024):

Ah OK, that makes sense. Thank you!

<!-- gh-comment-id:2539170606 --> @johnsutor commented on GitHub (Dec 12, 2024): Ah OK, that makes sense. Thank you!
Author
Owner

@arkmech commented on GitHub (Jan 25, 2025):

Will setting cookie on server on SvelteKit ever be handled by better-auth, or will it stay manual?

<!-- gh-comment-id:2614021111 --> @arkmech commented on GitHub (Jan 25, 2025): Will setting cookie on server on SvelteKit ever be handled by better-auth, or will it stay manual?
Author
Owner

@arkmech commented on GitHub (Jan 25, 2025):

Never mind… https://github.com/better-auth/better-auth/issues/600

<!-- gh-comment-id:2614021451 --> @arkmech commented on GitHub (Jan 25, 2025): Never mind… https://github.com/better-auth/better-auth/issues/600
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/better-auth#8462