More screenshots

    Works with the AI Agents you already use

    Claude CodeClaude CodeCursorCursorCodex CLICodex CLIMuseMuseDotsDots+21 more

    Better Auth: Type-Safe Accounts for Next.js

    1

    Add type-safe authentication to a Next.js app with Better Auth: session strategy, email/password plus OAuth, database wiring, route protection, and a hardening checklist before launch.

    $5

    Secure checkout via Stripe

    Money-back guarantee

    Converts to your local currency at checkout

    Security scanned
    Better Auth: Type-Safe Accounts for Next.js

    Better Auth: Type-Safe Accounts for Next.js

    Example session with this skill installed

    Add accounts to a small Next.js project tracker: currently anyone who opens it sees everyone's projects. I want email/password plus Google sign-in, database sessions, and the dashboard behind a login. Prove the flows work before launch.

    • Read your context and instructions
    • Compiled the better auth

    AUTH LIVE — sign up, Google sign-in, protected dashboard, and hardening all verified before launch.

    Decision 1 — session strategy: database sessions. The user wants to revoke a session if a laptop is lost, and the app has no scale problem that would justify JWT.

    Decision 2 — providers: email/password plus Google. The users are a small team and all have Google accounts. No other OAuth provider — each one is failure surface, and nobody asked for GitHub.

    Setup: lib/auth.ts with the drizzle adapter, email/password enabled (minimum length 8), Google provider with the client ID and secret from the environment. Handler route and client created. BETTER_AUTH_URL set to the production URL; the Google console redirect URI set to that URL plus /api/auth/callback/google, character-for-character, including the trailing-slash check.

    Database: schema generated by the CLI and applied as a migration. The tables are read-only from here on — no hand edits.

    Sessions: the dashboard server component reads the session at the top and redirects to /login when there is none. The settings page does the same. Client components use useSession.

    Route protection: middleware with a public list (/login, /signup, /api/auth/*, marketing pages) and sessions required everywhere else. Admin settings get a role check on top of the session check.

    Test pass, all green: sign up with email/password → session created → dashboard loads → sign out → dashboard redirects to login. Google sign-in → session created → dashboard loads. An unauthenticated request to a protected API route returns 401. A logged-in non-admin user requesting /admin is denied.

    Hardening: rate limiting on /api/auth/*, password reset tokens single-use with short expiry, OAuth state verified, session cookie httpOnly + secure in production + sameSite=lax, sign-out revokes server-side, error messages don't distinguish "user not found" from "wrong password," admin routes behind a role check. Every box ticked before launch.

    Connects securely to your tools. The creator never sees your data.

    About this skill

    Auth libraries fail in production for decision reasons, not code reasons: the wrong session strategy, OAuth misconfigured, or the database schema drifting from the library's expectations. This skill makes the decisions first; the code is then straightforward.

    Decision 1 is the session strategy: database sessions (revocable instantly — pick this unless you have a reason not to) vs JWT sessions (stateless, faster, but revocation needs a denylist). Decision 2 is providers: email/password first as the baseline, then only the OAuth providers your users actually have accounts with — each provider is configuration surface and failure surface, with the exact redirect URI set in the provider's console.

    Then the setup path: a single auth instance, the API handler route, and the client. The database is owned by the library's CLI — generate the schema and apply it as a migration; never hand-write the auth tables, and treat generated auth migrations as read-only. Sessions are used directly in server components and via the useSession hook on the client, with auth checks as the first lines of server actions and API routes. Route protection is centralized in middleware: one public-path list, sessions required everywhere else, admin routes behind a role check. The skill closes with a hardening checklist (rate limiting, single-use reset tokens, OAuth state verification, cookie flags, server-side sign-out revocation, no account enumeration) and a common-failures table for redirect URI mismatches, sessions that die in production, and CSRF errors on OAuth callbacks.

    • It covers Next.js with Better Auth; other frameworks or auth libraries need adaptation.
    • A leaked BETTER_AUTH_SECRET requires rotating the secret and invalidating all sessions — there is no softer fix.
    • Email/password plus OAuth account linking (same address, two providers) must be decided up front; an unplanned collision strands users.
    • The hardening checklist is the same size for small apps — "it's a small app" is not a reason to skip it.
    • A domain move requires BETTER_AUTH_URL and every OAuth redirect URI to move on the same deploy.

    How to use

    1. 1

      Connect your agent to the Agensi MCP

      Copy the prompt and paste it into Claude, ChatGPT, Cursor or any agent that supports MCP. It sets up the connection for you. Manual setup

    2. 2

      Sign in to Agensi

      Your agent shows a sign-in link. Sign in and click Allow.

    3. 3

      Ask for what you need

      Your agent loads this skill and uses it on your task. It always asks before any payment.

    Reviews

    No reviews yet

    Be one of the first to try it. Every listed skill goes through the trust checks below.

    Security scanned

    Checked by our 8-point scan before listing

    Fresh listing

    Recently published to Agensi

    Money-back guarantee

    Doesn't do what its page promises? You get your money back.

    Trust & safety

    Security scan

    Security scan passed 3 days ago

    Permissions

    Environment Variables
    Listed3 days ago

    What's inside

    Frequently Asked Questions

    Database sessions, unless you have a real scale reason for JWT. Instant revocation ("log this user out everywhere right now") is a feature you'll want, and JWT revocation needs a denylist — which is a database again.

    The provider console URI must match `BETTER_AUTH_URL` plus the callback path character-for-character, including trailing slashes. One character off and the whole flow fails silently.

    Almost always the cookie `secure` flag, or a `BETTER_AUTH_URL` still pointing at localhost in the deploy environment. Check both before anything else.

    New in Security