nuxt-auth icon indicating copy to clipboard operation
nuxt-auth copied to clipboard

Migration to BetterAuth from authjs

Open FireMasterK opened this issue 2 months ago • 1 comments

Describe the feature

Since auth.js is now part of better auth, it would make sense to migrate to it.

https://www.better-auth.com/blog/authjs-joins-better-auth https://github.com/nextauthjs/next-auth/discussions/13252

How would you implement this?

No response

Additional information

  • [ ] Would you be willing to help implement this feature?

Provider

  • [x] AuthJS
  • [ ] Local
  • [ ] Refresh
  • [ ] New Provider

FireMasterK avatar Sep 27 '25 08:09 FireMasterK

Hi @FireMasterK, it's unfortunately not as simple as just replacing a dependency. The two libraries are cardinally different in their approaches. While AuthJs supports database-less approach, BetterAuth does not. This would be a roadblock for our users relying on JWTs to keep sessions. On top of that, the APIs exposed by the two are so different up to a point that they would not be compatible even if NuxtAuth tried to abstract the implementations away.

One of such differences is that BetterAuth provides both the client and the server and it expects you to figure out how (and what, and when) to call. For example, this is what NuxtAuth currently provides you:

const { signIn } = useAuth()

// This works on both the server and the client
signIn(...)

Here's what BetterAuth asks you to do:

// On client
import { createAuthClient } from "better-auth/vue"
export const authClient = createAuthClient({
    baseURL: "http://localhost:3000" // The base URL of your auth server
})

authClient.signIn.email(...)

// On server
export const auth = betterAuth({
    //...
})
await auth.api.signInEmail({
    body: {
        email: "[email protected]",
        password: "password"
    },
    headers: await headers() // optional but would be useful to get the user IP, user agent, etc.
})

To get this working, NuxtAuth would have to abstract lots of BetterAuth things away from you:

  • auth client creation - because NuxtAuth needs client for its own needs;
  • auth server creation - same, NuxtAuth needs it as well;
  • useAuth to support both the flows;
  • one may say it's easier to just use BetterAuth without any abstractions on top;

In contrast to this, AuthJs only asks for a server handler. The client is provided by NuxtAuth using the full power of Nuxt.

It is of course possible, but it would require lots of coordinated efforts to implement a new provider and we want to be sure it is good enough.

phoenix-ru avatar Oct 09 '25 08:10 phoenix-ru