Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 | 1x 1x 1x 1x 6x 6x 1x 5x 5x 5x 5x 5x 5x 5x 1x 1x 4x 3x 1x 1x | /**
* Admin Users API - List and Search B2C Users
* @see JCN-4 Phase 7: Wire E2E Tests to Real Backend
* @see JCN-23 Authorization fix
*/
import { NextRequest, NextResponse } from "next/server";
import {
listB2CUsers,
listUsers,
searchUsersByEmail,
} from "@/lib/cognito-admin";
import { requireSuperAdmin, forbiddenResponse } from "@/lib/amplify-server-utils";
/**
* GET /api/admin/users
* List B2C users with optional search
* Requires: super_admin role
*
* Query params:
* - search: Filter by email (partial match)
* - limit: Max users to return (default 60)
* - token: Pagination token
* - all: If true, return all users (not just B2C)
*/
export async function GET(request: NextRequest) {
// Authorization check
const auth = await requireSuperAdmin();
if (!auth.authorized) {
return forbiddenResponse(auth.error);
}
try {
const searchParams = request.nextUrl.searchParams;
const search = searchParams.get("search");
const limit = parseInt(searchParams.get("limit") || "60", 10);
const token = searchParams.get("token") || undefined;
const all = searchParams.get("all") === "true";
// If search query provided, search by email
if (search) {
const users = await searchUsersByEmail(search);
return NextResponse.json({
users,
total: users.length,
});
}
// List users (B2C only or all)
const result = all
? await listUsers({ limit, paginationToken: token })
: await listB2CUsers({ limit, paginationToken: token });
return NextResponse.json({
users: result.users,
total: result.users.length,
nextToken: result.paginationToken,
});
} catch (error) {
console.error("Error listing users:", error);
return NextResponse.json(
{ error: "Failed to list users", details: (error as Error).message },
{ status: 500 }
);
}
}
|