Skip to content

better-auth addon #658

Closed
Closed

Description

@azezsan

I find getting better auth setup and going through the sveltekit guide and creating a small demo takes the longest, I think better auth has reached a good stable point and it's double the size of lucia auth by now and growing making it a save bet to add

Activity

  1. changed the title [-]Adding better auth as an option[/-] [+]better-auth addon[/+] on Aug 2, 2025
  2. jycouet commented on Aug 2, 2025

    @jycouet
    Contributor

    I also did the switch from lucia-style to better-auth and I have to say that I'm pretty happy about it!
    But I don't know if sv want to have all options possible. For example there is drizzle and not prisma, there is prettier and not biome, ... Maybe community addon could answer this ?! I don't know :)

  3. jycouet commented on Aug 2, 2025

    @jycouet
    Contributor

    Side comment / Self promo
    If you want to spin up a better-auth demo, you can do: npm init remult@latest.
    Then you ca use sv to add stuff in it ;)

  4. benmccann commented on Aug 2, 2025

    @benmccann
    Member

    Yeah, I think we'd probably want a community add-on for it.

    I've been a bit annoyed at how many dependencies better-auth has. E.g. they include kysley as a dependency and we offer drizzle so you'd end up with two orms in your dependency tree.

    I've been hoping to improve our auth add-on a bit. I sent a PR to grant for SvelteKit support, which would let us support over 200 services via OAuth. I'm waiting for a review there, however.

  5. added
    enhancement
    pkg:create
    on Aug 2, 2025
  6. manuel3108 commented on Aug 2, 2025

    @manuel3108
    Member

    I do agree that our auth setup could use a rework with a different library under the hood. Leaving this open for visibility at the moment and to gather more opinions. Apart from that, I think everything has been said.

    Side note:
    Coming from the c# world, as soon as you add auth to a template, you are greeted with way more than what we currently provide. I'm not necessarily saying that we should provide all of this, but we could take some ideas:

    • MFA (and recovery codes)
    • password reset
    • changing emails
    • rbac (role based access control)
  7. added
    pkg:add
    and removed
    pkg:create
    on Aug 2, 2025
  8. sacrosanctic commented on Nov 22, 2025

    @sacrosanctic
    Contributor

    @benmccann I'd like to request a reconsideration on better-auth. They have removed @simplewebauthn and kysely is now optional.

    https://npmgraph.js.org/?q=better-auth@1.3.34
    https://npmgraph.js.org/?q=better-auth@1.4.0

  9. benmccann commented on Nov 22, 2025

    @benmccann
    Member

    Much better! kysely is still in the dependency graph unfortunately. I just filed an issue for that one

  10. sacrosanctic commented on Nov 22, 2025

    @sacrosanctic
    Contributor
  11. sacrosanctic commented on Nov 22, 2025

    @sacrosanctic
    Contributor

    Are there other blockers? If we can add it as an official addon, I'd be happy to contribute.

  12. benmccann commented on Nov 22, 2025

    @benmccann
    Member

    Chatting with the other maintainers about this and there's no clear consensus on whether we should make better-auth the officially recommended auth solution for Svelte. The main complaint that a couple of people have is that it requires client-side JS. I just filed an issue to see whether that's something that might be able to be addressed: better-auth/better-auth#6204

  13. sillvva commented on Nov 22, 2025

    @sillvva

    As far as I know, all of the client-side functions can be performed with the the server-side API via load/action/remote functions.

    https://www.better-auth.com/docs/concepts/api

    Any endpoint added to Better Auth, whether from plugins or the core, will be accessible through the api object.

    import { form } from "$app/server";
    import { auth } from "$lib/server/auth.ts";
    import { invalid, redirect } from "@sveltejs/kit";
    import { APIError } from "better-auth";
    
    export const signInSocial = form(
    	v.object({
    		provider: v.picklist(PROVIDERS.map((p) => p.id)),
    		callbackURL: v.pipe(v.string(), v.startsWith("/"))
    	}),
    	async function ({ provider, callbackURL }) {
    		const event = getRequestEvent();
    
    		const result = await auth.api
    			.signInSocial({
    				headers: event.request.headers,
    				body: {
    					provider
    				}
    			})
    			.catch((err) => {
    				if (err instanceof APIError) {
    					invalid(err.message);
    				}
    				return {};
    			});
    
    		if ("user" in result && result.user) {
    			redirect(302, callbackURL);
    		} else {
    			invalid("Failed to sign in");
    		}
    	}
    );
  14. sacrosanctic commented on Nov 22, 2025

    @sacrosanctic
    Contributor

    As @sillvva mentioned, every backend call has a corresponding endpoint. But some API's still require JavaScript, like passkeys.

    I understand if the official addon is not an option. I am content with a community addon.

  15. sillvva commented on Nov 22, 2025

    @sillvva

    I mean, if you use client-side authentication methods like passkeys, then it doesn't really matter which library you use or no library at all. Then sure, JS would be required in those cases.

  16. paoloricciuti commented on Nov 22, 2025

    @paoloricciuti
    Member

    @sillvva they do have a server side counterpart...very often however is not documented at all AND it's a bit annoying to use...for example the example you posted about social login will not work (at least for GitHub) because they need to set the cookie for the state parameter.

    They also do some silly thing, like you could technically do asResponse to get back an actual response but that doesn't include a Location header so you still need to manually redirect (and you would still need to manually set cookies).

    Here's what worked for me:

    async function login_social(provider: 'github' | 'google') {
    	const { cookies } = getRequestEvent();
    	const response = await auth.api.signInSocial({
    		returnHeaders: true,
    		body: {
    			provider,
    			callbackURL: BETTER_AUTH_URL + '/dashboard'
    		}
    	});
    
    	const set_cookie = response.headers.get('set-cookie');
    	if (set_cookie) {
    		const { name, value, ...options } = parseString(set_cookie);
    		cookies.set(name, value, options as never);
    	}
    	if (response.response.redirect && response.response.url) {
    		redirect(302, response.response.url!);
    	}
    }
  17. Bekacru commented on Nov 22, 2025

    @Bekacru

    Hey all, happy to help get this over the line. We’ve been talking about dropping Kysely as the default, and we’ll sync with the team on it

    requires client-side JS

    We’ll support form data

    @paoloricciuti you can use the sveltekitCookies plugin, it does exactly what you're doing here. It calls cookies.set with the Set-Cookie header https://www.better-auth.com/docs/integrations/svelte-kit#server-action-cookies

  18. benmccann commented on Nov 22, 2025

    @benmccann
    Member

    Thanks! This is very helpful

    I filed better-auth/better-auth#6219 with a suggestion to make the redirect portion easier

    I'm going to ask around the team on our side to understand why we don't just use the set-cookie header as it seems like a poor user experience. It'd be much easier if you didn't need the sveltekitCookies plugin. That part is on us and I can't say I know why it's required today, so I'll at least try to get an answer to that and see if it's something that we can change

  19. benmccann commented on Nov 23, 2025

    @benmccann
    Member

    I found sveltejs/kit#8409 regarding the cookies issue. I'll ask around for a bit more clarification though as I'm not understanding it yet

  20. sacrosanctic commented on Feb 6, 2026

    @sacrosanctic
    Contributor

    better-auth is now added to the cli via #898 so this issue is now mainly about improving the experience?

  21. jycouet commented on Feb 6, 2026

    @jycouet
    Contributor

    oOups, it's was not linked with the issue to auto close it!
    Thanks @benmccann

  22. azezsan commented on Feb 11, 2026

    @azezsan
    ContributorAuthor

    @jycouet I was reading the better auth docs and I would like you to check this section this sounds perfect for our case or am I miss understanding it

  23. sacrosanctic commented on Feb 11, 2026

    @sacrosanctic
    Contributor

    #658 (comment)

    That was referenced here. It doesn't actually matter since kysely will be downloaded either way and if you're not using it , rollup should treeshake it away.

  24. benmccann commented on Feb 11, 2026

    @benmccann
    Member

    I'd support switching. Faster install time, less disk usage, and reduced risk of supply chain attacks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementpkg:add

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions