DEV Community

Alex Spinov
Alex Spinov

Posted on

Better Auth Has a Free API That Most Developers Dont Know About

Better Auth is a modern authentication library for TypeScript that works with any framework. It provides email/password, OAuth, 2FA, and session management out of the box.

Setup

import { betterAuth } from "better-auth";

export const auth = betterAuth({
  database: { provider: "pg", url: process.env.DATABASE_URL },
  emailAndPassword: { enabled: true },
  socialProviders: {
    google: { clientId: process.env.GOOGLE_ID, clientSecret: process.env.GOOGLE_SECRET },
    github: { clientId: process.env.GITHUB_ID, clientSecret: process.env.GITHUB_SECRET }
  },
  session: { expiresIn: 60 * 60 * 24 * 7 } // 7 days
});
Enter fullscreen mode Exit fullscreen mode

Client

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

export const authClient = createAuthClient({ baseURL: "http://localhost:3000" });

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

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

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

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

Two-Factor Auth

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

export const auth = betterAuth({
  plugins: [twoFactor({ issuer: "MyApp" })]
});

// Client: enable 2FA
const { data } = await authClient.twoFactor.enable();
// data.totpURI — show as QR code
Enter fullscreen mode Exit fullscreen mode

Key Features

  • Email/password + OAuth providers
  • Two-factor authentication
  • Session management
  • Works with any framework
  • Plugin system (2FA, magic link, passkeys)

Need to scrape or monitor web data at scale? Check out my web scraping actors on Apify or email spinov001@gmail.com for custom solutions.

Top comments (0)