メインコンテンツへ移動

Next.js Authentication with Better Auth and PostgreSQL

Better Auth による、Next.js 16 向けのメール アドレスとパスワードでのサインアップ、サインイン、サインアウト。Docker 上の PostgreSQL、起動時の設定チェック、テスト付き。

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

スクリーンショット

スクリーンショット 1 / 10:Next.js Authentication with Better Auth and PostgreSQL のタイトル カード: キャッチ コピー、技術スタックのチップ、サインアップに成功した実際の npm run dev のログ、スマートフォンに表示したサインイン後のホーム ページ。

Next.js Authentication with Better Auth and PostgreSQL のタイトル カード: キャッチ コピー、技術スタックのチップ、サインアップに成功した実際の npm run dev のログ、スマートフォンに表示したサインイン後のホーム ページ。

買い切り価格

  • ₹199+GST(合計 ₹234.82)
  • $3
  • お支払いが確認されると、すぐにライブラリからダウンロードできます。
  • このプロジェクトの今後のリリースもすべて、追加費用なしで含まれます。
最新リリース
1.0.0 · 2026/11/19
ライセンス
Source Code Licence

概要

Better Auth 1.7 でメール アドレスとパスワードのアカウントを扱う、Next.js 16 アプリの完全なソース コードです。わかりやすいエラー メッセージを表示するサインアップとサインインのページ、サインイン中のユーザーを示すヘッダー、サインアウトを備えています。アカウントは Docker 上の PostgreSQL 18 に保存されます。アプリは起動時に設定をチェックし、そのチェックを 5 つのテストでカバーしています。macOS と Docker Desktop でテスト済みです。

サインインは小さなフォーム 2 つに見えますが、実際にはパスワードのハッシュ化、安全なセッション Cookie、わかりやすいエラー メッセージ、何かが壊れる前にチェックされる設定も必要です。Next.js Authentication with Better Auth and PostgreSQL は、TypeScript 向けのオープンソースの認証ライブラリである Better Auth で、そのすべてを実現します。自分のコンピューターで実行でき、土台として拡張できる小さなアプリです。macOS と Docker Desktop での実際の実行でテストしています。

できること

  • メール アドレスとパスワードでサインアップ。 サインアップ ページでアカウントを作成すると、そのまますぐにサインインした状態になります。
  • サインインとサインアウト。 サインイン ページで再びサインインでき、Sign out ボタンでそのブラウザーのセッションを終了します。
  • サインイン中のユーザーを表示。 すべてのページのヘッダーに、サインイン中のユーザーの名前と Sign out ボタン、または Sign in と Sign up のリンクが表示され、ページを再読み込みせずに切り替わります。
  • フォーム上にわかりやすいメッセージ。 User already exists. Use another email. と Invalid email or password が、フォーム上に直接表示されます。

Better Auth による安全な既定の設定

  • 平文のパスワードは保存しません。 Better Auth は、各パスワードの scrypt ハッシュだけを PostgreSQL に保存します。
  • HttpOnly Cookie によるセッション。 ページ上のスクリプトからは読み取れず、セッションの有効期間は既定で 7 日間です。
  • パスワードの長さをサーバーでチェック。 8~128 文字で、フォームにも同じ最小文字数を設定しています。

ミスを早く見つける設計

  • 起動時の設定チェック。 BETTER_AUTH_SECRET、BETTER_AUTH_URL、DATABASE_URL のいずれかがないか無効な場合、アプリは修正すべき項目をわかりやすく一覧にして停止します。
  • 5 つのテストで、このチェックを確認します。Node.js に組み込まれたテスト ランナーで実行するため、テスト フレームワークをインストールする必要はありません。
  • クリーンなチェック結果。 TypeScript の型チェックと Biome のリンターはエラーなしで通り、npm run build も成功します。

拡張しやすいコード

  • App Router を使った Next.js 16、TypeScript 7、Tailwind CSS 4、Biome。
  • PostgreSQL と組み合わせた Better Auth 1.7。 pg の接続プールで接続し、短い lib/auth.ts 1 つ、ルート ハンドラー、React クライアントで構成しています。
  • Docker Compose 上の PostgreSQL 18。 自分のコンピューターからのみ、ポート 5435 でアクセスできます。
  • すべての設定を 1 つのファイルに。 .env.example を .env にコピーします。各値は README で説明しています。
  • 再現可能なインストール。 パッケージの正確なバージョンを記録した package-lock.json と、Node.js 26 用の .nvmrc があります。

含まれないもの

  • メールの送信機能はありません。 メール アドレスの確認やパスワードのリセットには対応していません。
  • ソーシャル サインイン、2 要素認証、パスキーはありません。
  • 保護されたページはありません。 すべてのページが公開されており、ヘッダーにはセッションが表示されます。
  • ホスティングは含まれません。 アプリは自分のコンピューター上で動きます。デプロイはお任せします。

6 つの手順で実行

  1. Node.js 26 LTS をインストールし、Docker Desktop を起動します。
  2. ダウンロードしたファイルを展開し、そのフォルダーでターミナルを開きます。
  3. npm install を実行します。
  4. .env.example を .env にコピーします。npx auth@latest secret を実行し、表示された行を .env に貼り付けます。
  5. docker compose up -d、続いて npx auth@latest migrate を実行し、y と答えます。
  6. npm run dev を実行し、http://localhost:3000 を開いて Sign up をクリックします。

How to Set Up and Run Next.js Authentication with Better Auth and PostgreSQL (新しいタブで開きます)(英語)では、すべての手順をスクリーンショット付きで紹介しています。

コードの背景にある考え方は、無料の Better Auth Tutorial for Beginners (新しいタブで開きます)(英語)で学べます。

Better Auth、Next.js、PostgreSQL、Docker、その他の記載されている名称は、それぞれの所有者の商標です。このプロジェクトは、Better Auth, Inc. および Vercel と提携しておらず、両社から承認を受けたものでもありません。

無料チュートリアルシリーズ

Better Auth Tutorial for Beginners

作りながら学びたい方へ。ブログの無料ステップガイドで、このプロジェクトの作り方を順を追って紹介しています。

シリーズを読む (新しいタブで開きます)

手に入るもの

アーカイブの中身が一目でわかります。

  • Next.js アプリの完全なソース コードのバージョン 1.0.0
  • わかりやすいエラー メッセージを表示する、サインアップとサインインのページ
  • サインイン中のユーザーを示す、Sign out ボタン付きのヘッダー
  • PostgreSQL と組み合わせて設定した Better Auth 1.7 と、そのルート ハンドラー、React クライアント
  • 自分のコンピューターで PostgreSQL 18 を実行する Docker Compose ファイル
  • 設定がない、または無効なときに、わかりやすいメッセージでアプリを停止する設定チェック
  • 設定チェックの 5 つのテスト
  • 設定のサンプル ファイル、パッケージのロック ファイル、.nvmrc ファイル
  • 要件、セットアップ、すべての設定、スクリプト、トラブルシューティングをまとめた README

使用している技術

開発と動作確認に使った実行環境とバージョンです。

  • 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

アーカイブの中身

展開したあとに手元に残るファイル構成です。

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

必要になるもの

動かすために必要なアカウントやサービスです。あとから驚くことはありません。

  • Node.js 26 LTS と npm
  • Docker と Docker Compose: macOS または Windows では Docker Desktop、Linux では Docker Engine。Docker Desktop は個人利用と小規模企業では無料ですが、それより大きな組織では有料のサブスクリプションが必要です
  • コンピューター上の空きポート 3000(アプリ)と 5435(データベース)
  • ほかのアカウント、API キー、有料サービスは不要。すべて自分のコンピューターで動きます

リリース

これまでのすべてのバージョンを新しい順に並べています。

  1. 1.0.0最新

    完全なソース コードの最初のリリースです。

    • Next.js 16.4.6 で Better Auth 1.7.26 を使った、メール アドレスとパスワードによるサインアップ、サインイン、サインアウト。
    • Docker Compose 上の PostgreSQL 18。Better Auth のテーブルは 1 つのコマンドで作成します。
    • 起動時の設定チェックと、その 5 つのテスト。

Source Code Licence

利用できる範囲

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

ライセンスの全文を読む
  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.