Better Auth
A TypeScript sign-in library: users and sessions live in your own app database, not with a vendor.
Backend & APIsUsing since 2024 · Featured project: The Travelling Cows
- Category
- Backend & APIs
- Using since
- 2024
- Featured project
- The Travelling Cows
In practice
I pick Better Auth when sign-in should live in the app code and the users in the app database, not with an outside provider. At The Travelling Cows it handles Google sign-in on a trip booking platform built in Next.js.
What is Better Auth?
Better Auth is a framework-agnostic authentication and authorization library for TypeScript. Password sign-in, social accounts, sessions, rate limiting and database migrations come built in, and advanced features such as two-factor sign-in or multi-tenancy arrive as plugins.
It runs inside your app, so users, accounts and sessions land in a database connected through an adapter, such as PostgreSQL. A session is an ordinary cookie that expires after seven days by default.
- License
- MIT
- Language
- TypeScript
- User data
- In your own database
- Sessions
- A cookie, seven days by default
How I work with Better Auth
I start by asking who signs in and how. That decides whether a Google account is enough, or whether passwords and sessions on several devices are needed too.
Schema and adapter
I connect an adapter to the project database and generate the schema with the CLI, so the user and session tables join the other migrations of the project.
Sign-in providers
I configure only the providers the project needs. On The Travelling Cows that is a Google account. It needs OAuth credentials from the Google console, which go into the config.
Sessions and access
I set session lifetime and refresh to fit the project. I check access to routes on the server in Next.js, not only in the interface, which is easy to bypass.
When it fits, and when it doesn't
Better Auth fits where sign-in is part of the app, not a separate service.
A good fit when
- A Next.js app where the users should sit in your own database.
- Google sign-in and, when needed, a password as well.
- A project where user data has to stay under your control.
- A team that will keep the config in code and review it like any other change.
Not a fit when
- A mobile app that already runs entirely on Firebase services.
- A team where nobody will look after the database or library updates.
- An editor panel that is fine with the account built into the CMS.
- A company with a ready central sign-in system that has to stay.
For a mobile app on Firebase I stay with Firebase Auth, and for an editor panel the sign-in from Payload CMS is enough.
Better Auth or Firebase Auth?
Both handle sign-in, but in different places. They differ in who holds the users and how far the dependence on a vendor reaches.
| Criterion | Better Auth | Firebase |
|---|---|---|
| Where users live | In the app database | In a Google-hosted service |
| Sessions | A cookie, seven days by default | An ID token for an hour, refreshed |
| Provider setup | In the project code | In the Firebase console or CLI |
| Vendor dependence | Code and data are yours | Accounts and tokens stay in Firebase |
| Cost model | Free library, you pay for hosting | Free plan, then pay by usage |
| Platform coupling | Independent of framework and host | Tightly tied to Firebase services |
Choose Better Auth when
you want users in your own database and sign-in in code you maintain anyway. You pay with time for configuration and updates that nobody else will do for you.
Choose Firebase Auth when
the mobile app already runs on Firebase and should work without a server of its own. You get a ready SDK and a hosted service, but the user accounts stay with Google.
On The Travelling Cows I used Better Auth for Google sign-in.
Frequently asked questions
Want to straighten out sign-in in your app?
Describe who has to sign in and whether the user data should stay with you. I will answer whether Better Auth fits, or whether a hosted provider is the better pick.