unit(B1): invite -> pending -> approve/deny with console outbox

Admin-invite lifecycle (BUILD-PLAN Phase B, depends A3):
- src/auth/invites.ts: pure nextUserStatus() rule (invite->pending_approval,
  approve->active, deny->denied) + isSignInEligible() mirroring the Auth.js
  authorize guard; thin DB wrappers inviteUser/approveUser/denyUser;
  enqueueOutbox() console transport (launch, ARCHITECTURE 3.4).
- Routes: /api/dev/invite, /api/dev/approve, /api/dev/deny.
- src/auth/invites.test.ts: 7 tests named after the rules.
- magic-link.ts exports APP_ORIGIN.
No migration: users.status already carries the enum (A2).
Verify: typecheck, lint, 24 unit tests (+7), build, 4 e2e all green; runtime
smoke test confirms invite->pending, approve->active signs in, deny->denied
gets no session.
This commit is contained in:
Tiel Coder
2026-11-17 23:46:07 +00:00
parent 3451bced82
commit 5f0b94af6a
7 changed files with 418 additions and 1 deletions
+48
View File
@@ -0,0 +1,48 @@
import { NextRequest } from "next/server";
import { z } from "zod";
import { approveUser } from "@/auth/invites";
const JSON_HEADERS = { "content-type": "application/json" };
const bodySchema = z.object({
userId: z.string().min(1),
approvedBy: z.string().min(1).optional(),
});
/**
* Approve a pending user -> active. POST { userId, approvedBy? }.
*/
export const POST = async (req: NextRequest) => {
let userId: string | undefined;
let approvedBy: string | undefined;
try {
const body = JSON.parse(await req.text()) as { userId?: string; approvedBy?: string };
userId = body.userId;
approvedBy = body.approvedBy;
} catch {
// fall through to validation
}
const parsed = bodySchema.safeParse({ userId, approvedBy });
if (!parsed.success) {
return new Response(JSON.stringify({ error: "a userId is required" }), {
status: 400,
headers: JSON_HEADERS,
});
}
try {
const user = await approveUser({
userId: parsed.data.userId,
approvedBy: parsed.data.approvedBy ?? "admin",
});
return new Response(JSON.stringify({ ok: true, id: user.id, status: user.status }), {
status: 200,
headers: JSON_HEADERS,
});
} catch {
return new Response(JSON.stringify({ error: "failed to approve user" }), {
status: 500,
headers: JSON_HEADERS,
});
}
};
+48
View File
@@ -0,0 +1,48 @@
import { NextRequest } from "next/server";
import { z } from "zod";
import { denyUser } from "@/auth/invites";
const JSON_HEADERS = { "content-type": "application/json" };
const bodySchema = z.object({
userId: z.string().min(1),
deniedBy: z.string().min(1).optional(),
});
/**
* Deny a pending user -> denied (cannot sign in). POST { userId, deniedBy? }.
*/
export const POST = async (req: NextRequest) => {
let userId: string | undefined;
let deniedBy: string | undefined;
try {
const body = JSON.parse(await req.text()) as { userId?: string; deniedBy?: string };
userId = body.userId;
deniedBy = body.deniedBy;
} catch {
// fall through to validation
}
const parsed = bodySchema.safeParse({ userId, deniedBy });
if (!parsed.success) {
return new Response(JSON.stringify({ error: "a userId is required" }), {
status: 400,
headers: JSON_HEADERS,
});
}
try {
const user = await denyUser({
userId: parsed.data.userId,
deniedBy: parsed.data.deniedBy ?? "admin",
});
return new Response(JSON.stringify({ ok: true, id: user.id, status: user.status }), {
status: 200,
headers: JSON_HEADERS,
});
} catch {
return new Response(JSON.stringify({ error: "failed to deny user" }), {
status: 500,
headers: JSON_HEADERS,
});
}
};
+50
View File
@@ -0,0 +1,50 @@
import { NextRequest } from "next/server";
import { z } from "zod";
import { inviteUser } from "@/auth/invites";
const JSON_HEADERS = { "content-type": "application/json" };
const bodySchema = z.object({
email: z.string().email(),
// The admin performing the invite; recorded on the outbox row.
invitedBy: z.string().min(1).optional(),
});
/**
* Admin invite. POST { email, invitedBy? } -> creates a pending_approval user,
* signs a magic-link token, and enqueues an invite to the console outbox.
*/
export const POST = async (req: NextRequest) => {
let email: string | undefined;
let invitedBy: string | undefined;
try {
const body = JSON.parse(await req.text()) as { email?: string; invitedBy?: string };
email = body.email;
invitedBy = body.invitedBy;
} catch {
// fall through to validation
}
const parsed = bodySchema.safeParse({ email, invitedBy });
if (!parsed.success) {
return new Response(JSON.stringify({ error: "a valid email is required" }), {
status: 400,
headers: JSON_HEADERS,
});
}
try {
const result = await inviteUser({
email: parsed.data.email,
invitedBy: parsed.data.invitedBy ?? "admin",
});
return new Response(JSON.stringify(result), {
status: 200,
headers: JSON_HEADERS,
});
} catch {
return new Response(JSON.stringify({ error: "failed to invite user" }), {
status: 500,
headers: JSON_HEADERS,
});
}
};