agentleFS
blyatiful1blyatiful1/ultraweb/skills/auth/SKILL.md

auth

Skill1 starsChanged 3 months agoMITView on GitHub

Authentication with Better Auth 1.6, the locked stack default (Auth.js/NextAuth is maintenance-mode legacy) — server config with the Drizzle adapter, CLI-generated schema, client hooks, optimistic route protection in proxy.ts (Next 16, never middleware.ts), authoritative session checks in RSC and in every mutation, and a sign-in page styled to the design system, never the default unstyled form. Invoke during the backend phase whenever the brief needs accounts, protected pages, or roles, or the user says "auth", "login", "sign in", "sign up", "sessions", "protected routes", or "user accounts".

  • Reads credentials
  • Installs packages

17 other projects use a version of this.

Adopted by

Most recently taken up by

Used with

Projects with it also use test and deploy.

Other files people use alongside this one.

Files found in the same public projects as a version of this one, more often than they turn up anywhere else. Each project counts once, large collections count for less, and this file's own repository is left out.
  • Microsoft AzureSkill

    test

    Also in 3 projects that use this

  • VercelSkill

    deploy

    Deploy applications to Vercel. Use when the user says "deploy", "deploy to Vercel", "push to production", "deploy my app", or "go live".

    Also in 3 projects that use this

The file

We split this skill into the rules it gives an agent

3 rules. 0 are common, 1 is distinctive, and 2 are very specific to this project.

  • 0

    Common

    Most of the 14 edited versions we read keep these.

  • 1

    Distinctive

    This author's own choices. Worth a look.

  • 2

    Very specific

    Tied to their project's names, files or tools. Probably skip.

  • Common
  • Distinctive
  • Very specific
  1. Very specific:

    Read BRIEF.md: which pages are protected, which sign-in methods (email+password?

    line 22
  2. Very specific:

    Every mutating server action and route handler re-checks the session itself — a forged request never meets proxy.ts, and a page-level check …

    line 97
  3. Distinctive:

    Default unstyled sign-in form.

    line 113

“Common” is measured: kept by at least 6 in 10 of the 14 edited versions of this file we could read. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.

Keep what fits and drop the rest. Your version, ready for your agents.
Files
SKILL.md

auth — Better Auth, designed sign-in

Stage: Phase 7 (Backend) - Reads: design/BRIEF.md, design/SYSTEM.md, design/DIRECTION.md, db/schema.ts - Writes: lib/auth.ts, lib/auth-client.ts, app/api/auth/[...all]/route.ts, proxy.ts, app/(auth)/* pages

Standard

Invisible when it works, designed when it shows. Sessions are verified on the server where the data lives — proxy redirects are UX, not security. The sign-in page consumes SYSTEM.md tokens and gets hero-level craft: it is often the second impression the site makes. Better Auth 1.6 without discussion; NextAuth requires written justification.

Default and legacy

Better Auth 1.6 is the default for every new build. Auth.js/NextAuth entered maintenance mode 2025-09-22 — the Better Auth team maintains it for security patches only and recommends Better Auth for greenfield. NextAuth v5 never left beta (5.0.0-beta.31).

NextAuth v5 beta remains acceptable in exactly two cases: the project already runs it and this is an iteration, or the brief hard-requires a provider/adapter that exists only in the Auth.js ecosystem. Then use the legacy pattern — auth.ts exporting const { auth, handlers } = NextAuth({...}), app/api/auth/[...nextauth]/route.ts re-exporting handlers, proxy.ts re-exporting auth — and record the deviation plus reason in BRIEF.md.

Process

  1. Read BRIEF.md: which pages are protected, which sign-in methods (email+password? Google? magic link → ultraweb:email), whether roles exist. No auth requirement → stop. Never bolt accounts onto a brochure site.
  2. npm i better-auth@1.6 (the STACK.md-verified line). BETTER_AUTH_SECRET + BETTER_AUTH_URL into .env. Server config in lib/auth.ts (below).
  3. npx auth@1.6 generate — the CLI pinned to the SAME 1.6.x line as the installed better-auth (they ship in lockstep; a floating CLI can emit a schema the installed lib doesn't match). Emits the Drizzle auth tables (user, session, account, verification). Merge into db/schema.ts and migrate through ultraweb:database: one schema, one migration history.
  4. Mount the handler and create the client (below).
  5. Protect in layers: proxy.ts cookie check for redirect UX, server-side session check where protected data renders, and a session re-check inside every mutating server action and route handler. All three, always.
  6. Build sign-in/sign-up as designed pages (rule below). Then verify the loop empirically: sign up → sign out → hit a protected page (must redirect) → sign in (must land back).

Server config, handler, client

// lib/auth.ts
import { betterAuth } from "better-auth";
import { drizzleAdapter } from "better-auth/adapters/drizzle";
import { nextCookies } from "better-auth/next-js";
import { db } from "@/db";

export const auth = betterAuth({
  database: drizzleAdapter(db, { provider: "pg" }),
  emailAndPassword: { enabled: true },
  // socialProviders: only what BRIEF.md names — each one is env vars + a provider console to maintain
  plugins: [nextCookies()], // keep last: lets server actions set session cookies
});
// app/api/auth/[...all]/route.ts
import { auth } from "@/lib/auth";
import { toNextJsHandler } from "better-auth/next-js";

export const { GET, POST } = toNextJsHandler(auth);
// lib/auth-client.ts
import { createAuthClient } from "better-auth/react";
export const authClient = createAuthClient();
// client components: authClient.useSession(), authClient.signIn.email({ email, password }), authClient.signOut()

Better Auth's adapter and plugin import paths evolve — the shapes above are the 1.6 line; verify against current docs at build time.

Route protection: two layers, never one

middleware.ts is deprecated in Next 16 — the file is proxy.ts, Node runtime. The proxy layer is OPTIMISTIC: cookie presence only, no DB hit, pure redirect UX.

// proxy.ts
import { NextRequest, NextResponse } from "next/server";
import { getSessionCookie } from "better-auth/cookies";

export function proxy(request: NextRequest) {
  if (!getSessionCookie(request)) {
    return NextResponse.redirect(new URL("/login", request.url));
  }
  return NextResponse.next();
}

export const config = { matcher: ["/dashboard/:path*", "/settings/:path*"] };

The AUTHORITATIVE check lives where protected data renders:

// app/dashboard/page.tsx (RSC)
import { auth } from "@/lib/auth";
import { headers } from "next/headers";
import { redirect } from "next/navigation";

export default async function DashboardPage() {
  const session = await auth.api.getSession({ headers: await headers() });
  if (!session) redirect("/login");
  // session.user.id drives every query below
}

Every mutating server action and route handler re-checks the session itself — a forged request never meets proxy.ts, and a page-level check doesn't cover it.

The sign-in page rule

The default unstyled form is banned — same severity as lorem ipsum. The sign-in page:

  • Consumes SYSTEM.md tokens: type scale, accent, radius/shadow language, both themes designed.
  • Picks a layout variant to match DIRECTION.md: centered card (SaaS default), split-screen with a brand panel that carries the signature move, or minimal full-page type-led form (editorial directions).
  • Full states: focus-visible rings in the palette, inline zod validation on ultraweb:forms timing, submit button with loading state, error copy in the site voice ("That email and password don't match." — never "Invalid credentials error").
  • Social buttons follow provider brand rules but sit inside your radius and spacing system.

Anti-patterns

  • middleware.ts — deprecated; the file is proxy.ts
  • auth.api.getSession inside proxy.ts — a DB hit on every matched request; cookie check only there
  • Protection ONLY in proxy.ts — an optimistic redirect is not authorization; any mutation without its own check is an open door
  • Default unstyled sign-in form; untouched shadcn card presented as "the auth page"
  • npm i next-auth on greenfield without the written justification in BRIEF.md
  • Hand-rolled password hashing or session tokens
  • session?.user from a client hook trusted for anything security-relevant — client session state is display data

Worked example — Tidepool, gating the port-analytics app shell

Moved to references/example.md — read only when this build's case is genuinely ambiguous; the sections above are the decision material.

Composes with

Moved to references/composes.md — the handoff map; load it when orchestrating this skill against its neighbors.

Download now (ZIP)

Does it work?

Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.

No reviews yet

People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.

  • It worked
  • Partly
  • It didn't
What did you use it for? e.g. Reviewing pull requests in a Rails app

Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.

Your trail

Files you open will collect here, so you can find them again.

Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.

Now do this for your own.

Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.