How to avoid singleton database when setting up better auth
from what I am seeing in the installation steps, the way to set this up is by making a specific file in an specific folder and exporting specifically an
auth variable:
I don't have a singleton database, so this is impossible for me. my database is setup a main function on startup, not exported.
someone else offered this solution in a different post, but it doesn't work:
I get this error:
nowhere in the docs seem to be explained why better auth needs to be initialised in this very specific way or what the consequences are for not doing so, either way I still like to inject my dependencies, is there any way to achieve this pattern?\6 Replies
@Better Auth can you help me with the above
Hi there! I'd be happy to help with Better Auth. To give you a quick overview, Better Auth is a framework-agnostic authentication solution for TypeScript offering built‑in support for email/password logins, social sign-on providers, two-factor authentication, and a robust plugin ecosystem for advanced features [1], [2].
Some common questions from the community include:
• Configuration & Integration:
– How to set up email/password or social sign-on authentication?
– How to integrate Better Auth into various frameworks (Next.js, Hono, Convex, etc.)?
These topics are covered in our installation guides and framework-specific documentation [1], [18].
• Type Safety & Customization:
– How to leverage TypeScript with strict mode and infer custom additional fields for user and session objects?
– How to extend or customize the default authentication flows using plugins?
We provide detailed instructions on setting up type-safe configurations and extending types with additional fields [4].
• Advanced Features:
– Enabling and managing two-factor authentication (2FA), including handling TOTP code verifications.
– Configuring passkey registration with custom options using the Passkey plugin.
Our 2FA documentation explains enabling 2FA for credential accounts, while our passkey plugin updates cover client-side options and global
Are you looking for guidance on one of these specific areas, or is there another aspect of Better Auth you'd like to explore further? Let me know, and I'll
– How to set up email/password or social sign-on authentication?
– How to integrate Better Auth into various frameworks (Next.js, Hono, Convex, etc.)?
These topics are covered in our installation guides and framework-specific documentation [1], [18].
• Type Safety & Customization:
– How to leverage TypeScript with strict mode and infer custom additional fields for user and session objects?
– How to extend or customize the default authentication flows using plugins?
We provide detailed instructions on setting up type-safe configurations and extending types with additional fields [4].
• Advanced Features:
– Enabling and managing two-factor authentication (2FA), including handling TOTP code verifications.
– Configuring passkey registration with custom options using the Passkey plugin.
Our 2FA documentation explains enabling 2FA for credential accounts, while our passkey plugin updates cover client-side options and global
authenticatorSelection settings [5], [15].Are you looking for guidance on one of these specific areas, or is there another aspect of Better Auth you'd like to explore further? Let me know, and I'll
provide more targeted help!
Sources:
[1] Introduction | Better Auth
[2] Better Auth
[4] TypeScript | Better Auth
[5] Two-Factor Authentication (2FA) | Better Auth
[15] feat: passkey plugin with client-side
authenticatorAttachment and global authenticatorSelection configuration
[18] Installation | Better AuthIntroduction | Better Auth
Introduction to Better Auth.
TypeScript | Better Auth
Better Auth TypeScript integration.
Two-Factor Authentication (2FA) | Better Auth
Enhance your app's security with two-factor authentication.
GitHub
feat: passkey plugin with client-side
authenticatorAttachment an...Description
This PR addresses Issue #1311 by enhancing the Passkey plugin in better-auth to allow clients to specify the authenticatorAttachment (platform or cross-platform) during passkey registra...
@Better Auth from what I am seeing in the installation steps, the way to set this up is by making a specific file in an specific folder and exporting specifically an
auth variable:
I don't have a singleton database, so this is impossible for me. my database is setup a main function on startup, not exported.
someone else offered this solution in a different post, but it doesn't work:
I get this error:
nowhere in the docs seem to be explained why better auth needs to be initialised in this very specific way or what the consequences are for not doing so, either way I still like to inject my dependencies, is there any way to achieve this pattern?\It's most likely coming from
getMigrations, since the provided DB is the drizzle adapter it won't work, you must pass an actual database connection for pg I recommed just using the pg package and creating a new Pool("connection-string-here") and then overwriting the database with that pool you created right before you pass it to the getMigrations.
Also with initAuth it's returning better-auth itself, you need to do initAuth(...).options to grab the BetterAuthOptionsGot it, thanks! I'll try that.