Setting the file. One moment.
Skill 12 · Clerk Next.js Patterns
Subchapter 12.1
references/api-routes.mdMarkdown1 KBView on GitHub
Also bundled
Evalsimport { auth } from '@clerk/nextjs/server';
export async function GET() {
const { isAuthenticated, userId } = await auth();
if (!isAuthenticated) {
return Response.json({ error: 'Unauthorized' }, { status: 401 });
}
const data = await db.data.findMany({ where: { userId } });
return Response.json(data);
}Core 2 ONLY (skip if current SDK):
isAuthenticatedis not available. Useif (!userId)instead.
export async function DELETE(req: Request) {
const { isAuthenticated, has } = await auth();
if (!isAuthenticated) return Response.json({ error: 'Unauthorized' }, { status: 401 });
const isAdmin = await has({ role: 'org:admin' });
if (!isAdmin) return Response.json({ error: 'Forbidden' }, { status: 403 });
return Response.json({ success: true });
}export async function GET(req: Request, { params }: { params: { orgId: string } }) {
const { userId, orgId } = await auth();
if (!userId) return Response.json({ error: 'Unauthorized' }, { status: 401 });
if (orgId !== params.orgId) return Response.json({ error: 'Forbidden' }, { status: 403 });
const orgData = await db.orgs.findUnique({ where: { id: orgId } });
return Response.json(orgData);
}