DEV Community

Alex Spinov
Alex Spinov

Posted on

Better Auth Has a Free TypeScript Auth Framework — Here's How to Use It

Lucia showed us auth can be a library, not a service. Better Auth takes it further — a complete, framework-agnostic auth solution with plugins for everything from 2FA to organizations.

What Is Better Auth?

Better Auth is a comprehensive TypeScript authentication framework. It handles the entire auth flow — signup, login, sessions, OAuth, 2FA, organizations — out of the box.

Better Auth vs. Other Solutions

Feature Better Auth Lucia Auth Services
Setup time 5 minutes 30 minutes 5 minutes
Cost Free forever Free forever $25+/month
2FA built-in Yes (plugin) Manual Some plans
Organizations Yes (plugin) Manual Enterprise plan
Email verification Yes Manual Yes
Rate limiting Yes (plugin) Manual Yes
Data ownership Full Full Vendor

Quick Start

npm install better-auth
Enter fullscreen mode Exit fullscreen mode
// auth.ts
import { betterAuth } from "better-auth";
import { drizzleAdapter } from "better-auth/adapters/drizzle";

export const auth = betterAuth({
  database: drizzleAdapter(db, { provider: "pg" }),
  emailAndPassword: { enabled: true },
  socialProviders: {
    github: {
      clientId: process.env.GITHUB_CLIENT_ID!,
      clientSecret: process.env.GITHUB_CLIENT_SECRET!,
    },
    google: {
      clientId: process.env.GOOGLE_CLIENT_ID!,
      clientSecret: process.env.GOOGLE_CLIENT_SECRET!,
    },
  },
});
Enter fullscreen mode Exit fullscreen mode
// Client-side
import { createAuthClient } from "better-auth/client";

const authClient = createAuthClient();

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

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

// OAuth
await authClient.signIn.social({ provider: "github" });
Enter fullscreen mode Exit fullscreen mode

Plugin System

Better Auth's plugin architecture is its superpower:

Two-Factor Authentication

import { twoFactor } from "better-auth/plugins";

export const auth = betterAuth({
  plugins: [
    twoFactor({
      otpOptions: { period: 30, digits: 6 }
    })
  ]
});
Enter fullscreen mode Exit fullscreen mode

Organizations & Teams

import { organization } from "better-auth/plugins";

export const auth = betterAuth({
  plugins: [
    organization({
      roles: ["owner", "admin", "member"],
      invitations: true
    })
  ]
});
Enter fullscreen mode Exit fullscreen mode

Rate Limiting

import { rateLimit } from "better-auth/plugins";

export const auth = betterAuth({
  plugins: [
    rateLimit({ window: 60, max: 10 })
  ]
});
Enter fullscreen mode Exit fullscreen mode

Framework Integration

Works with any TypeScript framework:

  • Next.js — App Router middleware
  • SvelteKit — hooks integration
  • Nuxt — server middleware
  • Astro — middleware
  • Express/Hono — handler

Key Features

  • Auto-generated API routes/api/auth/* handled automatically
  • Database agnostic — Drizzle, Prisma, Mongoose adapters
  • Type-safe client — full TypeScript inference
  • CLI toolnpx better-auth generate for migrations
  • Session management — JWT or database sessions

Get Started


Building a SaaS with authentication? Check out my Apify web scrapers for market research and competitive data. Custom solutions: spinov001@gmail.com

Top comments (0)