Files
CatoandClaude Opus 5.5 344ddd4c77 feat(auth): every way in on AccountKit's own screens (#167)
* feat(auth): every way in on AccountKit's own screens

Petvity has answered sign-in from AccountKit since this morning, through the
Auth.js compat layer, with AccountKit's sign-up, recovery, passkey and
two-factor endpoints closed. This finishes the move:

- /login, /register and /forgot-password render AccountKit's SignInForm,
  SignUpForm and recovery form: email and password, an email sign-in link,
  passkeys, two-factor, recovery, Continue with OrangeCat and the shared
  bitbaum sign-in, each shown when its env is set.
  /portal/settings/security renders AccountPanel (password, passkeys, second
  factor, linked sign-ins, deleting the account).
- The compat layer passes screens: "accountkit" (AccountKit 0.20.0): every
  AccountKit endpoint is open, and useSession and signOut keep Petvity's
  session shape.
- AccountKit is the only credential writer. Petvity's registration route,
  reset routes, settings password change and its own deletion are removed,
  and so are Auth.js, its edge config, the Drizzle adapter and the
  credential-less Google provider.
- Migration 0009 (additive) makes the panel's rename and deletion reach
  users. The import attaches the new trigger too.
- A Petvity plugin keeps the shared demo account explorable (no sign-in
  changes, no deletion, no mailed links) and keeps ADMIN_EMAILS promotion
  at a password or email-link sign-in.
- Every imported account keeps working: same ids, bcrypt hashes through
  legacyBcrypt, OrangeCat links, roles on users.

browser-smoke.yml now runs every way in against the production build and a
migrated, imported PostgreSQL: the method set at 320px and 390px, sign up,
sign out, a wrong password, sign in, an Auth.js-era bcrypt account, a reset
through the mailed link and an email sign-in link. AccountKit's real mail is
caught by scripts/ci/mail-sink.cjs. Long button labels now wrap (.btn-wrap):
the shared sign-in's label overflowed a 320px screen.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_019MDtmuJhiHPASaR5k8raL9

* fix(ci): the mail sink path needs no runner context

Job-level env cannot read the runner context, so the workflow did not
parse. The sink sits at a fixed path outside the uploaded artifacts.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_019MDtmuJhiHPASaR5k8raL9

---------

Co-authored-by: Claude <noreply@anthropic.com>
2026-11-01 23:38:49 +01:00

114 lines
4.9 KiB
TypeScript

import createIntlMiddleware from "next-intl/middleware";
import { routing } from "./i18n/routing";
import { NextResponse, type NextRequest } from "next/server";
import { POST_LOGIN_PATH } from "@/lib/config/auth";
import { withoutSessionCookie } from "@/lib/auth/session-cookie";
import { compat } from "@/lib/auth/shared/compat";
import type { Session } from "next-auth";
const intlMiddleware = createIntlMiddleware(routing);
// All portal routes live under /portal/ so this stays a single prefix entry.
// IMPORTANT: New portal routes must be nested under /portal/ — do not add
// individual paths here.
const PORTAL_PREFIXES = ["/portal", "/admin"];
// API routes that require authentication (pet/health data, etc.)
// /api/auth/* is handled separately (always public)
// /api/account (registration) and /api/public/* are intentionally public
const PRIVATE_API_PREFIXES = [
"/api/pets",
// Health *data*. The liveness probe is /api/healthz, deliberately outside
// this prefix so it stays reachable without a session.
"/api/health",
"/api/vaccinations",
"/api/medications",
"/api/vets",
"/api/sitters",
"/api/groomers",
"/api/sellers",
"/api/bookings",
"/api/availability",
// Writes files to disk — never reachable without a session.
"/api/uploads",
"/api/reviews",
// Account orders. /api/shop/* is the guest counterpart and is deliberately
// NOT listed: requiring a session there would delete guest checkout, which
// is the whole point of it. It self-guards with a rate limit and, for
// payment, an unguessable order token.
"/api/orders",
// /api/cron/* is intentionally NOT gated — cron routes self-authenticate via
// the CRON_SECRET bearer (box systemd timers; loki install-app-crons.sh).
"/api/admin",
];
// Non-localized auth pages — bypass intl routing entirely
// (/demo signs visitors into the shared demo account; logged-in users are
// bounced to their dashboard like on any auth page)
const AUTH_PATHS = ["/login", "/register", "/forgot-password", "/reset-password", "/demo"];
// Non-localized public pages — bypass intl routing AND don't redirect logged-in users.
// /unsubscribe is here so the email link works whether the recipient is logged in or not.
const PUBLIC_BYPASS_PATHS = ["/unsubscribe"];
/** Petvity's routing for one request, given who (if anyone) is signed in. */
function route(req: NextRequest, session: Session | null) {
const { pathname } = req.nextUrl;
const dest = session?.user?.role === "admin" ? "/admin/users" : POST_LOGIN_PATH;
// ── Auth API routes — always public ──────────────────────────────────
if (pathname.startsWith("/api/auth")) {
return NextResponse.next();
}
// ── Portal / admin / private API ─────────────────────────────────────────
const isPrivate =
PORTAL_PREFIXES.some((p) => pathname === p || pathname.startsWith(p + "/")) ||
PRIVATE_API_PREFIXES.some((p) => pathname === p || pathname.startsWith(p + "/"));
if (isPrivate) {
if (!session) {
const loginUrl = new URL("/login", req.url);
loginUrl.searchParams.set("returnTo", pathname);
return NextResponse.redirect(loginUrl);
}
if (pathname.startsWith("/admin") && session.user.role !== "admin") {
return NextResponse.redirect(new URL(POST_LOGIN_PATH, req.url));
}
return NextResponse.next();
}
// ── Non-localized auth pages ─────────────────────────────────────────────
// These live at /login, /register etc. — serve directly, skip intl routing.
// Redirect already-logged-in users to their dashboard.
if (AUTH_PATHS.some((p) => pathname === p || pathname.startsWith(p + "/"))) {
if (session) return NextResponse.redirect(new URL(dest, req.url));
return NextResponse.next();
}
// ── Non-localized public pages — pass through regardless of auth state ──
if (PUBLIC_BYPASS_PATHS.some((p) => pathname === p || pathname.startsWith(p + "/"))) {
return NextResponse.next();
}
// ── API routes not already handled above — pass through, no locale routing
if (pathname.startsWith("/api/")) {
return NextResponse.next();
}
// ── Marketing site: locale routing ───────────────────────────────────────
return intlMiddleware(req);
}
// Reads the session, never re-issues it: see lib/auth/session-cookie.ts. The
// session is a database row read through AccountKit (docs/ACCOUNTKIT-MIGRATION.md).
export default async function proxy(req: NextRequest) {
return withoutSessionCookie(await route(req, await compat.sessionFor(req.headers)));
}
export const config = {
matcher: [
"/((?!_next/static|_next/image|favicon|icon|opengraph-image|robots\\.txt|sitemap\\.xml|.*\\..*).*)",
],
};