Files
Arnaud 4c3880e07f feat: moderator-scoped admin access + fix push notifications not displaying (v0.66.0)
Moderator role existed in the schema and was already respected by
comment deletion, but every admin page/route treated moderator
identically to a regular user (403/redirect). Wires it up narrowly:
admin/layout.tsx now lets admin+moderator through and filters the
nav by role, while every admin-only page (users, tiers, settings,
webhooks, insights, etc.) explicitly redirects moderators away via a
new requireFullAdminPage() helper -- the nav filter is UX, this is
the actual gate. Moderators land on Reports and Recipes: reports
GET/PATCH now accept requireAdmin({allowModerator: true}), and a new
PATCH /api/v1/admin/recipes/[id] lets admin+moderator unpublish a
public recipe (flip to private) as a takedown action, audit-logged.

Also found and fixed a real bug while auditing the PWA push pipeline
for a "push click-through" gap: public/sw.js had no `push` event
listener at all, so incoming push messages never displayed anything
-- push was silently non-functional end-to-end despite the
subscribe/send plumbing all working. Added the push listener
(showNotification) and a notificationclick listener that focuses an
existing tab or opens one at the payload's url.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-07-21 23:31:47 +02:00

149 lines
4.8 KiB
TypeScript

import crypto from "node:crypto";
import { headers } from "next/headers";
import { NextRequest, NextResponse } from "next/server";
import { auth } from "@/lib/auth/server";
import { db, apiKeys, users, eq } from "@epicure/db";
import { applyRateLimit } from "@/lib/rate-limit";
export async function requireSession() {
const session = await auth.api.getSession({ headers: await headers() });
if (!session) {
return { session: null, response: NextResponse.json({ error: "Unauthorized" }, { status: 401 }) };
}
return { session, response: null };
}
/** Like requireSession, but never 401s — for endpoints that also accept anonymous
* access via a resource-scoped capability (e.g. a public-editable share link). */
export async function getOptionalSession() {
return auth.api.getSession({ headers: await headers() });
}
export async function requireAdmin(opts?: { allowModerator?: boolean }) {
const { session, response } = await requireSession();
if (response) return { session: null, response };
// Don't trust session.user.role — it comes from a 5-minute cookieCache
// (see lib/auth/server.ts), so a just-demoted admin would keep access for
// up to 5 minutes. Query the current role directly.
const [dbUser] = await db
.select({ role: users.role })
.from(users)
.where(eq(users.id, session!.user.id))
.limit(1);
const allowed = dbUser?.role === "admin" || (opts?.allowModerator && dbUser?.role === "moderator");
if (!allowed) {
return { session: null, response: NextResponse.json({ error: "Forbidden" }, { status: 403 }) };
}
return { session, response: null };
}
type SessionLike = {
user: {
id: string;
email: string;
name: string;
tier: string;
role?: string;
image?: string | null;
};
};
type RateLimitOpts = { limit: number; windowSeconds: number };
export async function requireSessionOrApiKey(
req: NextRequest,
opts?: { rateLimit?: RateLimitOpts }
): Promise<{ session: SessionLike; response: null } | { session: null; response: NextResponse }> {
// 1. Try Bearer API key
const authHeader = req.headers.get("authorization");
if (authHeader?.startsWith("Bearer ")) {
const rawKey = authHeader.slice(7).trim();
if (rawKey.startsWith("ek_")) {
const keyHash = crypto.createHash("sha256").update(rawKey).digest("hex");
const [keyRow] = await db
.select({ id: apiKeys.id, userId: apiKeys.userId, scope: apiKeys.scope })
.from(apiKeys)
.where(eq(apiKeys.keyHash, keyHash))
.limit(1);
if (keyRow) {
// Read-scoped keys can't make any state-changing request — enforced
// once here rather than in every route, since a route can't tell
// whether it's being called by a "read" key without this check.
if (keyRow.scope === "read" && !["GET", "HEAD", "OPTIONS"].includes(req.method)) {
return {
session: null,
response: NextResponse.json({ error: "This API key is read-only" }, { status: 403 }),
};
}
// Update lastUsedAt asynchronously — don't block response
void db
.update(apiKeys)
.set({ lastUsedAt: new Date() })
.where(eq(apiKeys.id, keyRow.id))
.catch((err) => console.error("[api-auth] failed to update apiKeys.lastUsedAt", err));
const [user] = await db
.select({
id: users.id,
email: users.email,
name: users.name,
tier: users.tier,
role: users.role,
})
.from(users)
.where(eq(users.id, keyRow.userId))
.limit(1);
if (user) {
// Rate limit per API key (not per user — a user's other keys shouldn't
// share this bucket).
if (opts?.rateLimit) {
const { limit, windowSeconds } = opts.rateLimit;
const rateLimitResponse = await applyRateLimit(
`rl:api:key:${keyRow.id}`,
limit,
windowSeconds
);
if (rateLimitResponse) {
return { session: null, response: rateLimitResponse };
}
}
return {
session: { user: { ...user, image: null } },
response: null,
};
}
}
return {
session: null,
response: NextResponse.json({ error: "Unauthorized" }, { status: 401 }),
};
}
}
// 2. Fall back to session cookie
const result = await requireSession();
if (result.response) return result;
if (opts?.rateLimit) {
const { limit, windowSeconds } = opts.rateLimit;
const rateLimitResponse = await applyRateLimit(
`rl:api:session:${result.session!.user.id}`,
limit,
windowSeconds
);
if (rateLimitResponse) {
return { session: null, response: rateLimitResponse };
}
}
return result;
}