DEV Community

Alex Spinov
Alex Spinov

Posted on

Better Auth Has a Free Authentication Library — Auth.js Alternative With More Features

Auth.js (NextAuth) is great until you need sessions, organization management, or two-factor auth. Better Auth includes all of that out of the box.

What is Better Auth?

Better Auth is a TypeScript-first authentication library that handles email/password, OAuth, sessions, 2FA, organizations, and more — with a clean API and zero vendor lock-in.

Why Better Auth

1. Simple Setup

import { betterAuth } from "better-auth";

export const auth = betterAuth({
  database: {
    provider: "postgresql",
    url: process.env.DATABASE_URL,
  },
  emailAndPassword: {
    enabled: true,
  },
  socialProviders: {
    google: {
      clientId: process.env.GOOGLE_CLIENT_ID,
      clientSecret: process.env.GOOGLE_CLIENT_SECRET,
    },
    github: {
      clientId: process.env.GITHUB_CLIENT_ID,
      clientSecret: process.env.GITHUB_CLIENT_SECRET,
    },
  },
});
Enter fullscreen mode Exit fullscreen mode

2. Client-Side API

import { createAuthClient } from "better-auth/react";

export const authClient = createAuthClient();

// Sign up
await authClient.signUp.email({
  email: "user@example.com",
  password: "password",
  name: "Alice",
});

// Sign in
await authClient.signIn.email({
  email: "user@example.com",
  password: "password",
});

// OAuth
await authClient.signIn.social({ provider: "google" });

// Get session
const session = await authClient.useSession();
Enter fullscreen mode Exit fullscreen mode

3. Two-Factor Authentication

const auth = betterAuth({
  plugins: [
    twoFactor({
      issuer: "MyApp",
      // TOTP, SMS, or email
    }),
  ],
});

// Client
await authClient.twoFactor.enable({ password: "current-password" });
await authClient.twoFactor.verifyTOTP({ code: "123456" });
Enter fullscreen mode Exit fullscreen mode

4. Organization & Team Management

const auth = betterAuth({
  plugins: [
    organization({
      roles: ["owner", "admin", "member"],
    }),
  ],
});

// Create organization
await authClient.organization.create({ name: "Acme Inc" });

// Invite member
await authClient.organization.inviteMember({
  email: "bob@example.com",
  role: "member",
});
Enter fullscreen mode Exit fullscreen mode

5. Session Management

// List active sessions
const sessions = await authClient.listSessions();

// Revoke specific session
await authClient.revokeSession({ sessionId: "abc123" });

// Revoke all other sessions
await authClient.revokeOtherSessions();
Enter fullscreen mode Exit fullscreen mode

6. Framework Support

// Next.js
import { toNextJsHandler } from "better-auth/next-js";
export const { GET, POST } = toNextJsHandler(auth);

// Hono
import { toHonoHandler } from "better-auth/hono";
app.all("/api/auth/*", toHonoHandler(auth));

// Express
app.all("/api/auth/*", toExpressHandler(auth));

// SvelteKit, Nuxt, Remix — all supported
Enter fullscreen mode Exit fullscreen mode

Better Auth vs Auth.js vs Clerk

Better Auth Auth.js Clerk
Type Library Library Service
2FA Built-in No Yes
Organizations Built-in No Yes ($)
Session management Full Basic Full
Database Any SQL Any adapter Managed
Price Free Free Freemium
Self-hosted Yes Yes No

Getting Started

npm install better-auth
npx better-auth generate  # Generate database tables
npx better-auth migrate   # Run migrations
Enter fullscreen mode Exit fullscreen mode

The Bottom Line

Better Auth fills the gap between simple auth libraries and expensive auth services. Full-featured, self-hosted, and TypeScript-first.


Need data tools? I build web scraping solutions. Check my Apify actors or email spinov001@gmail.com.

Top comments (0)