Using the Better Auth component in "use node" context
Coming from a different database, I'm in the process of migrating over to Convex (cloud, not self-hosted), and using Better Auth Convex component (local install) as auth solution.
Ideally I want to re-use the users's password hash, which are hashed using
argon2id, this requires the node runtime instead of the default v8 runtime Convex uses.
When adding "use node" at the top of the /convex/auth.ts where I the betterAuth instance with the password hash override like so:
this will causes the following errors (not only from node:crypto also node:util fs, path and os)
this essentially means I can't override the password hash?10 Replies
Thanks for posting in <#1088161997662724167>.
Reminder: If you have a Convex Pro account, use the Convex Dashboard to file support tickets.
- Provide context: What are you trying to achieve, what is the end-user interaction, what are you seeing? (full error message, command output, etc.)
- Use search.convex.dev to search Docs, Stack, and Discord all at once.
- Additionally, you can post your questions in the Convex Community's <#1228095053885476985> channel to receive a response from AI.
- Avoid tagging staff unless specifically instructed.
Thank you!
I don't think you can use
use node in /convex/auth.ts. That defines auth for your whole application, so it can't be isolated to the node-compatible runtime.
Try creating a separate file with node convex actions for hashing and verifying passwords, then calling them via ctx.runAction in your auth file.mmmmm, so like justin mentions, use node is made for you to use actions on, convex works with
crypto without needing to import it at the top, maybe you could use that?yeah that's what I figured, having
"use node" at the top of /convex/auth.ts would basically mean that most Convex functions (doing auth checks) will need to run in node environment. this also isn't how the node runtime works for Convex, in the docs they explain that only actions will run in the node environment, and is not for queries or mutations. This is intended to be extract/separated into its own file with "use node" at the top, so that only those actions are run in the node environment.
Making me come to the conclusion: I can't override the password hashing (at least in its current implementation) for Better Auth as Convex component.
Or is it possible that I can run an action from within the /convex/auth.ts inside the betterAuth({...}) constructor? Hmm I'll try that
it's not that I'm using/importing crypto myself, it's used by the package argon2 that I import into /convex/auth.tsYeah i understand that
Yeah scratch that
Yes, you can run actions from within the
auth.ts file. For example:
In this case, it's a scheduled action. But ActionCtx will also expose runAction.Type Utilities | Convex + Better Auth
Type utilities for Better Auth
I have created separate node functions for the
hash and verify in /convex/password.ts like so:
but during auto deployment of functions (after saving the file) I get this error:
I guess there's a mismatch / misconfiguration for the node version? 🤔
could it be related to my convex.json, where I set the version to 22, so it matches the version i have on vercel?
man, I feel i'm in the depths of Convex 😅
brb lunchI wonder if you need to mark argon2 as an external package? https://docs.convex.dev/functions/bundling#external-packages
Bundling | Convex Developer Hub
How Convex bundles and optimizes your function code
YES! That was the last missing piece! Thanks @Justin 🙌
Or well, at least the upload of the functions work, and no TS errors exist 😉
so for completeness, the resulting code for betterAuth config is as follows (see below), this is using the internalActions from
/convex/password.ts (see above), and the convex.json setting "externalPackages": ["argon2"]
Oh, this makes me feel great about this. There's a learning curve to Convex, but so far no hard blockers yet! Thanks again 😄