
Lucia-auth is Deprecated: Meet the Better Alternative – Better Auth
Posted by
kamlesh paulon
Feb 10, 2025| 5 min read
Last updated on : Feb 10, 2025
As of 2025, Lucia-auth
, a popular authentication library, is reaching the end of its support lifecycle. With that change, developers are seeking alternatives that provide robust, flexible, and easy-to-use authentication solutions. Enter Better Auth
—a new, comprehensive, and framework-agnostic authentication (and authorization) library for TypeScript.
Better Auth brings powerful features, a growing plugin ecosystem, and the ability to handle complex authentication scenarios like 2FA and multi-tenant support. Though still in beta, it is already proving to be an excellent option for developers looking to future-proof their TypeScript applications.
In this article, we’ll guide you through setting up Better Auth in your Next.js project using Drizzle ORM and PostgreSQL.
Table of contents
- Pre-requisites
- Installation
- Setting Environment Variables
- Creating a Better Auth Instance
- Database Configuration
- Creating Database Tables
- Authentication Methods
- Setting Up the API Route
- Creating a Client Instance
- Conclusion
Pre-requisites
Before you begin, ensure you have the following in place:
- Node.js installed. You can download it from nodejs.org.
- A PostgreSQL database set up. Better Auth requires a database to store user data.
- Drizzle ORM installed if you’re planning to use the Drizzle adapter.
- Basic knowledge of environment variables and API routes in your framework.
Installation
Let’s start by adding Better Auth to your Next.js project:
pnpm install better-auth
Setting Environment Variables
- Create a
.env
file in the root of your project and add the following environment variables:
- Secret Key: This is a random value used by the library for encryption and generating hashes. You can generate a secret key manually using a tool like OpenSSL or by using the built-in key generator in the Better Auth docs.
BETTER_AUTH_SECRET=<Your generated secret key>
- Base URL: Set the base URL of your app.
BETTER_AUTH_URL=http://localhost:3000
- Trusted Origins (optional): If your app accepts requests from multiple domains, list them under trusted origins.
BETTER_AUTH_TRUSTED_ORIGINS="http://localhost:3000,https://example.com"
Creating a Better Auth Instance
Create a file named auth.ts
in root of your application
import { betterAuth } from "better-auth";
export const auth = betterAuth({
// Add your configuration here
})
Database Configuration
Better Auth requires a database to store user data. By default, it uses Kysely for database queries, which supports PostgreSQL, MySQL, and SQLite.
Since we are using PostgreSQL with the Drizzle ORM adapter, the configuration would look like this:
import { betterAuth } from "better-auth";
import { drizzleAdapter } from "better-auth/adapters/drizzle";
import { db } from "@/db"; // your drizzle instance
export const auth = betterAuth({
database: drizzleAdapter(db, {
provider: "pg",
})
});
For performance optimization, the built-in Kysely adapter is recommended when supported. However, Drizzle works well for custom setups.
Creating Database Tables
Better Auth provides a CLI tool to help generate the required database schema. Run the following command to generate an ORM schema or SQL migration file:
npx @better-auth/cli generate
Next, run the migration command to create the necessary tables in your PostgreSQL database:
npx @better-auth/cli migrate
If you need to manually create the schema, refer to the Better Auth documentation.
Authentication Methods
Better Auth supports several authentication methods out of the box. Here’s an example of how to configure email/password authentication along with GitHub
as a social sign-on provider:
import { betterAuth } from "better-auth"
import { github } from "better-auth/social-providers"
export const auth = betterAuth({
emailAndPassword: {
enabled: true
},
socialProviders: {
github: {
clientId: process.env.GITHUB_CLIENT_ID,
clientSecret: process.env.GITHUB_CLIENT_SECRET,
}
}
});
Better Auth also supports additional methods such as passkeys, magic links, and more, which can be added through its plugin ecosystem.
Setting Up the API Route
To handle authentication requests, create a new API route for /api/auth/*.
In Next.js, you would typically create a catch-all route handler as follows:
Create a new file under app/api/auth/[...all]/route.ts
:
import { auth } from "@/lib/auth";
import { toNextJsHandler } from "better-auth/next-js";
export const { POST, GET } = toNextJsHandler(auth);
This file will manage all incoming authentication-related requests.
Creating a Client Instance
On the client side, Better Auth provides helper functions to interact with the authentication server. Here’s how to set it up:
Create a lib/auth-client.ts
file and add the following:
import { createAuthClient } from "better-auth/react";
export const authClient = createAuthClient({
baseURL: "http://localhost:3000", // Your auth server base URL
});
You can also export specific methods from authClient as needed:
export const { signIn, signUp, useSession } = authClient();
Conclusion
That’s it! You now have a fully functional Better Auth setup in your Next.js project, using Drizzle ORM and PostgreSQL for database management. With Better Auth’s framework-agnostic approach and plugin-friendly architecture, it provides a modern solution for all your authentication needs.
If you’re looking to extend functionality, be sure to explore Better Auth’s comprehensive set of plugins for advanced use cases like 2FA, magic links, and more.
For more information and detailed guides, visit the official Better Auth documentation.
Get updates directly to your inbox.
Join 500+ developers getting updates on Laravel & Next.js tips. No spam,
unsubscribe anytime.