DEV Community

Alex Spinov
Alex Spinov

Posted on

Better Auth Has a Free TypeScript Auth Library That's Actually Better

NextAuth is confusing. Passport.js is outdated. Lucia Auth is being deprecated. Better Auth is a new TypeScript authentication library that's framework-agnostic, fully typed, and handles everything from email/password to OAuth to two-factor.

What Better Auth Gives You for Free

  • Framework agnostic — works with Next.js, SvelteKit, Nuxt, Hono, Express
  • Full TypeScript — end-to-end type safety
  • Email/password — with verification and password reset
  • OAuth — Google, GitHub, Discord, and 20+ providers
  • Two-factor auth — TOTP, email OTP, backup codes
  • Session management — secure, configurable sessions
  • Organizations — teams, roles, invitations
  • Rate limiting — built-in protection
  • Database adapters — Prisma, Drizzle, Kysely, or any SQL

Quick Start

npm install better-auth
Enter fullscreen mode Exit fullscreen mode

Server Setup

// lib/auth.ts
import { betterAuth } from 'better-auth';
import { prismaAdapter } from 'better-auth/adapters/prisma';
import { PrismaClient } from '@prisma/client';

export const auth = betterAuth({
  database: prismaAdapter(new PrismaClient(), { provider: 'postgresql' }),
  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

Client Usage (React)

// lib/auth-client.ts
import { createAuthClient } from 'better-auth/react';

export const { signIn, signUp, signOut, useSession } = createAuthClient();
Enter fullscreen mode Exit fullscreen mode
// Sign up
await signUp.email({
  email: 'user@example.com',
  password: 'securepass123',
  name: 'Alice'
});

// Sign in with email
await signIn.email({ email: 'user@example.com', password: 'securepass123' });

// Sign in with OAuth
await signIn.social({ provider: 'google' });

// Use session
function Dashboard() {
  const { data: session, isPending } = useSession();
  if (isPending) return <div>Loading...</div>;
  return <h1>Welcome, {session?.user.name}!</h1>;
}
Enter fullscreen mode Exit fullscreen mode

Plugins (Extend Functionality)

import { betterAuth } from 'better-auth';
import { twoFactor } from 'better-auth/plugins';
import { organization } from 'better-auth/plugins';

export const auth = betterAuth({
  plugins: [
    twoFactor({ issuer: 'MyApp' }),
    organization()
  ]
});
Enter fullscreen mode Exit fullscreen mode

Better Auth vs NextAuth vs Clerk vs Lucia

Feature Better Auth NextAuth v5 Clerk Lucia
Framework Any Next.js focused Any Any
Type safety Full Partial Full Full
Self-hosted Yes Yes No (SaaS) Yes
2FA Plugin DIY Built-in DIY
Organizations Plugin DIY Built-in DIY
OAuth providers 20+ 50+ 20+ Manual
Cost Free (OSS) Free (OSS) Freemium Deprecated
Setup time 15 min 30 min 5 min 1 hour

The Verdict

Better Auth is the TypeScript auth library that gets the balance right. Self-hosted but not painful. Full-featured but not bloated. Framework-agnostic but with great React/Next.js support. If Lucia is deprecated and NextAuth is too Next.js-specific, Better Auth is your answer.


Need help building production web scrapers or data pipelines? I build custom solutions. Reach out: spinov001@gmail.com

Check out my awesome-web-scraping collection — 400+ tools for extracting web data.

Top comments (0)