settlekit/apps/dashboard/middleware.ts
caelum0x c14d0ab735 feat: team roles with invitations and enforced API key scopes (G9)
Security fix plus team access. Before this change any valid API key,
including access keys the delivery system hands to BUYERS, authenticated
as the merchant on the whole management API, and GET /v1/api-keys listed
every organization's keys. Now only platform keys (issued at sign-up or
via POST /v1/api-keys/platform, marked with the platform product id) can
call the API, and each request needs its resource scope
(<resource>:read|write, write implies read; platform:admin or * for
full access), answered with 403 and details.requiredScope otherwise.
Keys cannot mint broader keys, customer keys cannot carry management
scopes, key listing is tenant-scoped and keys can be revoked by id.

Dashboard sessions get team roles (owner, admin, developer, support,
viewer) mapped to scopes. /v1/team manages members and email
invitations (packages/invitations, token hash only, 7-day expiry);
/v1/auth/invitations/accept joins the org and creates the account. The
last owner is protected and only owners grant owner. Dashboard: Team
page, invite accept page, and an API keys page that creates restricted
keys and shows the secret once.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-09-30 06:09:06 +03:00

52 lines
1.8 KiB
TypeScript

// Dashboard login gate.
//
// The merchant dashboard is a first-party app: every page needs a signed-in
// merchant whose session scopes the data it loads. This middleware redirects
// anyone without the `sk_session` cookie to /login before a protected page
// renders, so logged-out users land on sign-in instead of empty 401 pages.
//
// This is only a UX gate on cookie PRESENCE — the API independently verifies
// the session's validity and binds the tenant on every call, so a forged or
// expired cookie still yields no data.
import { NextResponse, type NextRequest } from "next/server";
const SESSION_COOKIE = "sk_session";
/** Paths reachable without a session (auth screens + their assets). */
const PUBLIC_PREFIXES = ["/login", "/signup", "/invite"];
export function middleware(req: NextRequest): NextResponse {
const { pathname } = req.nextUrl;
const isPublic = PUBLIC_PREFIXES.some(
(p) => pathname === p || pathname.startsWith(`${p}/`),
);
const hasSession = req.cookies.has(SESSION_COOKIE);
// Unauthenticated → send to login, preserving where they were headed.
if (!isPublic && !hasSession) {
const url = req.nextUrl.clone();
url.pathname = "/login";
url.searchParams.set("next", pathname);
return NextResponse.redirect(url);
}
// Already authenticated but on an auth screen → send to the dashboard home.
// (An invitation link still opens: a signed-in person may accept it.)
if (isPublic && hasSession && !pathname.startsWith("/invite")) {
const url = req.nextUrl.clone();
url.pathname = "/";
url.search = "";
return NextResponse.redirect(url);
}
return NextResponse.next();
}
// Run on everything except Next internals, the route handlers under /api, and
// static asset files (anything with a file extension).
export const config = {
matcher: ["/((?!_next/|api/|.*\\.[^/]+$).*)"],
};