Skip to main content

Next.js Authentication with Better Auth and PostgreSQL

Email and password sign-up, sign-in and sign-out for Next.js 16 with Better Auth, PostgreSQL in Docker, a startup settings check and tests.

  • Next.js
  • React
  • Better Auth
  • PostgreSQL
  • pg (node-postgres)

Screenshots

Screenshot 1 of 10: Title card of Next.js Authentication with Better Auth and PostgreSQL: the promise, the stack chips, a real npm run dev log with a successful sign-up, and the signed-in home page on a phone.

Title card of Next.js Authentication with Better Auth and PostgreSQL: the promise, the stack chips, a real npm run dev log with a successful sign-up, and the signed-in home page on a phone.

One-off price

  • ₹199+ GST, ₹234.82 in total
  • $3
  • Download it from your library as soon as your payment is confirmed.
  • Every later release of this project is included, at no extra cost.
Latest release
1.0.0 · Nov 19, 2026

Overview

The complete source code of a Next.js 16 app with email and password accounts by Better Auth 1.7: sign-up and sign-in pages with clear error messages, a header that shows who is signed in, and sign-out. Accounts are stored in PostgreSQL 18 in Docker. The app checks its settings when it starts, and five tests cover that check. Tested on macOS with Docker Desktop.

Sign-in looks like two small forms, but it also means hashed passwords, a secure session cookie, clear error messages and settings that are checked before anything breaks. Next.js Authentication with Better Auth and PostgreSQL does all of that with Better Auth, the open-source authentication library for TypeScript, in a small app you can run on your computer and build on. It was tested in real runs on macOS with Docker Desktop.

What it does

  • Sign up with an email address and a password. The sign-up page creates the account and signs the person in straight away.
  • Sign in and sign out. The sign-in page signs a person back in, and the Sign out button ends the session in that browser.
  • Shows who is signed in. The header on every page shows the signed-in person's name with a Sign out button, or Sign in and Sign up links, and switches without a page reload.
  • Clear messages on the forms. User already exists. Use another email. and Invalid email or password appear right on the form.

Secure defaults from Better Auth

  • No plain-text passwords. Better Auth stores only a scrypt hash of each password in PostgreSQL.
  • Sessions in an HttpOnly cookie. Scripts on the page can't read it, and a session lasts 7 days by default.
  • Password length checked on the server. 8 to 128 characters, with the same minimum in the form.

Built so mistakes show up early

  • A settings check at startup. If BETTER_AUTH_SECRET, BETTER_AUTH_URL or DATABASE_URL is missing or invalid, the app stops with a clear list of what to fix.
  • Five tests for that check, run by the test runner built into Node.js, so no test framework to install.
  • Clean checks. The TypeScript type check and the Biome linter pass, and npm run build succeeds.

Code you can build on

  • Next.js 16 with the App Router, TypeScript 7, Tailwind CSS 4 and Biome.
  • Better Auth 1.7 with PostgreSQL through a pg connection pool: one short lib/auth.ts, the route handler and the React client.
  • PostgreSQL 18 in Docker Compose, reachable only from your own computer, on port 5435.
  • Every setting in one file. Copy .env.example to .env; the README explains each value.
  • Repeatable installs. A package-lock.json with the exact package versions, and an .nvmrc for Node.js 26.

What it doesn't include

  • No emails. There's no email verification or password reset.
  • No social sign-in, two-factor authentication or passkeys.
  • No protected pages. Every page is public; the header shows the session.
  • No hosting. It runs on your computer. Deploying it is up to you.

Run it in six steps

  1. Install Node.js 26 LTS and start Docker Desktop.
  2. Unzip the download and open a terminal in its folder.
  3. Run npm install.
  4. Copy .env.example to .env. Run npx auth@latest secret and paste the line it prints into .env.
  5. Run docker compose up -d, then npx auth@latest migrate, and answer y.
  6. Run npm run dev, open http://localhost:3000 and click Sign up.

Every step, with screenshots: How to Set Up and Run Next.js Authentication with Better Auth and PostgreSQL (opens in a new tab).

Learn the ideas behind the code in the free Better Auth Tutorial for Beginners (opens in a new tab).

Better Auth, Next.js, PostgreSQL, Docker and other names mentioned are trademarks of their respective owners. This project isn't affiliated with or endorsed by Better Auth, Inc. or Vercel.

Free tutorial series

Better Auth Tutorial for Beginners

Prefer to learn by building it? This free, step-by-step guide on our blog shows how the project is made.

Read the series (opens in a new tab)

What you get

Everything in the archive, at a glance.

  • Version 1.0.0 of the complete source code of the Next.js app
  • Sign-up and sign-in pages with clear error messages
  • A header that shows who is signed in, with a Sign out button
  • Better Auth 1.7 set up with PostgreSQL, its route handler and its React client
  • A Docker Compose file that runs PostgreSQL 18 on your computer
  • A settings check that stops the app with a clear message when a setting is missing or invalid
  • Five tests for the settings check
  • An example settings file, a package lock file and an .nvmrc file
  • A README with the requirements, setup, every setting, the scripts and troubleshooting

Built with

The exact runtimes and versions it was built and tested against.

  • Next.js16.4.6
  • React19.3.1
  • Better Auth1.7.26
  • PostgreSQL18.7
  • pg (node-postgres)8.23.1
  • TypeScript7.0.2
  • Tailwind CSS4.3.5
  • Biome2.6.8
  • Node.js26.13.1
  • Docker Desktop4.99.0

What is in the archive

The file tree you get once you unzip it.

nextjs-authentication-better-auth-postgresql/

  • .env.example
  • .gitattributes
  • .gitignore
  • .nvmrc
  • AGENTS.md
  • README.md
  • app/
    • api/
      • auth/
        • [...all]/
          • route.ts
    • favicon.ico
    • globals.css
    • layout.tsx
    • page.tsx
    • sign-in/
      • page.tsx
    • sign-up/
      • page.tsx
  • biome.json
  • components/
    • user-menu.tsx
  • compose.yaml
  • instrumentation.ts
  • lib/
    • auth-client.ts
    • auth.ts
    • env.ts
  • next.config.ts
  • package-lock.json
  • package.json
  • tests/
    • env.test.ts
  • tsconfig.json

What it needs from you

The accounts and services it expects to find, so nothing is a surprise.

  • Node.js 26 LTS and npm
  • Docker with Docker Compose: Docker Desktop on macOS or Windows, or Docker Engine on Linux. Docker Desktop is free for personal use and small businesses; larger organizations need a paid subscription
  • Free ports 3000 (the app) and 5435 (the database) on your computer
  • No other accounts, API keys or paid services: everything runs on your computer

Releases

Every version so far, newest first.

  1. 1.0.0Latest

    First release of the complete source code.

    • Email and password sign-up, sign-in and sign-out with Better Auth 1.7.26 in Next.js 16.4.6.
    • PostgreSQL 18 in Docker Compose, with Better Auth's tables created by one command.
    • A settings check at startup, with five tests.

Source Code Licence

What you may do with it

What you may and may not do with the source code you buy.

Read the full licence
  1. What this licence covers

This licence governs every file included in a product you buy from Dewiride Store: source code, configuration, database scripts, sample data, documentation and any other file in the download. It takes effect when your payment is confirmed and applies to every release of that product that reaches your library. Files that reach your library through an annual updates membership are governed in exactly the same way.

It forms part of the Terms of Sale and Use. Where a product page states different or additional licence terms for that product, those terms prevail for that product. The membership page may likewise state different or additional licence terms for the files a membership carries, and those terms then prevail for those files.

  1. The licence we grant you

We grant you a perpetual, worldwide, non-exclusive, non-transferable licence to use, reproduce, adapt, translate and modify the files, and to include the result in your own work, on the conditions set out below.

Perpetual means it does not expire. Worldwide means it is not limited to any country. Non-exclusive means we license the same product to other customers. Non-transferable means you cannot pass it on to someone else.

The licence is granted to you — either you personally, or the organisation you identified when buying. Everyone working within that organisation may use the files for that organisation’s projects. Nobody outside it may.

  1. What you may do
  • Use and change the files in your own projects, commercial or otherwise, with no limit on the number of projects.
  • Build a product or a service on them and charge your own customers for it.
  • Deploy the result for a client, as part of work you are doing for that client.
  • Distribute the result in compiled, deployed or otherwise non-source form.
  • Keep as many back-up copies as you reasonably need.
  • Study, observe and test how the code works, and obtain the information you need to make it work with your own software.

The last two are yours by law under sections 52(1)(aa) to (ac) of the Indian Copyright Act, 1957, and nothing in this licence is intended to take them away.

  1. What you may not do
  • Resell, sublicense, redistribute or publish the files themselves, in whole or in substantial part, in source form.
  • Include them in a template, starter kit, course, tutorial repository or component library that you sell or give away, where the code is the thing being supplied.
  • Share your account, your download links or your invoices with anyone outside your organisation.
  • Publish the files in a public repository, or in any other place where they can be obtained without buying them.
  • Remove or obscure the copyright and attribution notices in the source files.
  • Use the files to build something whose main purpose is to compete with Dewiride Store as a supplier of the same code.

The distinction that matters is simple: you may sell what you build with the code; you may not sell the code.

  1. Third-party components

Our products depend on open-source libraries, frameworks and services that we did not write. Those remain under their own licences, which we do not and cannot change. Where a product includes or requires such a component, its licence is named in the product’s documentation, and complying with it is your responsibility.

  1. Releases

Every release of a product you own is covered by this same licence and appears in your library at no extra cost. A release you have already downloaded remains licensed to you even if we later stop publishing the product. A release that reached your library through an annual updates membership is licensed to you on these same terms, and stays licensed after the period that carried it has ended.

  1. Ownership

We keep ownership of the files and of the copyright in them. You own whatever you create with them: your changes, your additions and the product you build are yours.

  1. If this licence ends

This licence ends if you materially breach it and do not put the breach right within thirty days of us asking you to in writing. It also ends if your payment is reversed or refunded, because the purchase it depends on no longer stands. Where a membership period is refunded or reversed, the licence ends for the releases that period carried and for nothing else: anything you bought separately, or that another period carried, is unaffected.

If it ends, you must stop using the files and delete the copies you hold. Work you had already delivered to a client before that point may continue in use, so that ending this licence never breaks something your own customer relies on.

  1. No warranty beyond the description

The warranties and limits in section 19 and section 20 of the Terms of Sale and Use apply to this licence as well.

  1. Governing law

This licence is governed by the law of India, and section 24 of the Terms of Sale and Use applies to any dispute about it. If a translation and the English version differ in meaning, the English version governs.