brackt/app/routes/api/webhooks/clerk.ts
Chris Parsons e2b178221a
Add oxlint linting setup with zero errors (#194)
* Add oxlint and fix all lint errors

- Install oxlint, add .oxlintrc.json with rules for TypeScript/React
- Add npm run lint / lint:fix scripts
- Add Claude PostToolUse hook to run oxlint on every edited file
- Fix 101 errors: unused vars/imports, eqeqeq, prefer-const, no-new-array
- Fix no-array-index-key (use stable keys or suppress positional cases)
- Fix exhaustive-deps missing dependency in useEffect
- Promote exhaustive-deps and no-array-index-key to errors
- Fix Map.get() !== null bug in $leagueId.server.ts (should be !== undefined)

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>

* Fix no-explicit-any warnings and upgrade tsconfig to ES2023

- Replace all `any` types with proper types or `unknown` across ~20 files
- Add typed socket payload interfaces in draft route and useDraftSocket
- Use any[] with eslint-disable for socket.io callbacks (legitimate escape hatch)
- Bump all tsconfigs from ES2022 → ES2023 to support toSorted/toReversed
- Fix cascading type errors uncovered by removing any: Map.get narrowing,
  participant relation types, ChartDataPoint, Partial<NewSeason> indexing
- Add ParticipantResultWithParticipant type to participant-result model
- Fix test fixtures to match updated interfaces (DraftCell, ParticipantResult)
- Fix duplicate getQPStandings import in sportsSeasonId.server.ts

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>

* Promote no-explicit-any to error

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>

---------

Co-authored-by: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-03-21 09:44:05 -07:00

96 lines
3.1 KiB
TypeScript

import { Webhook } from "svix";
import type { Route } from "./+types/clerk";
import { findOrCreateUser, getUserDisplayName } from "~/models/user";
export async function action({ request }: Route.ActionArgs) {
// Get the webhook secret from environment
const WEBHOOK_SECRET = process.env.CLERK_WEBHOOK_SECRET;
if (!WEBHOOK_SECRET) {
throw new Error("Please add CLERK_WEBHOOK_SECRET to your .env file");
}
// Get the headers
const svix_id = request.headers.get("svix-id");
const svix_timestamp = request.headers.get("svix-timestamp");
const svix_signature = request.headers.get("svix-signature");
// If there are no headers, error out
if (!svix_id || !svix_timestamp || !svix_signature) {
return new Response("Error: Missing svix headers", { status: 400 });
}
// Get the body
const payload = await request.text();
// Create a new Svix instance with your webhook secret
const wh = new Webhook(WEBHOOK_SECRET);
let evt: { type: string; data: { id: string; email_addresses?: Array<{ email_address: string }>; username?: string; first_name?: string; last_name?: string; image_url?: string } };
// Verify the webhook
try {
evt = wh.verify(payload, {
"svix-id": svix_id,
"svix-timestamp": svix_timestamp,
"svix-signature": svix_signature,
}) as typeof evt;
} catch (err) {
console.error("Error verifying webhook:", err);
return new Response("Error: Verification failed", { status: 400 });
}
// Handle the webhook
const eventType = evt.type;
console.log(`Webhook received: ${eventType}`);
if (eventType === "user.created" || eventType === "user.updated") {
const { id, email_addresses, username, first_name, last_name, image_url } = evt.data;
try {
if (eventType === "user.created") {
// Create new user
const user = await findOrCreateUser({
id,
emailAddresses:
email_addresses?.map((e: { email_address: string }) => ({
emailAddress: e.email_address,
})) || [],
username,
firstName: first_name,
lastName: last_name,
imageUrl: image_url,
});
console.log(
`User created in database: ${user.id} (${getUserDisplayName(user)})`
);
} else {
// Update existing user (or create if doesn't exist)
const user = await findOrCreateUser({
id,
emailAddresses:
email_addresses?.map((e: { email_address: string }) => ({
emailAddress: e.email_address,
})) || [],
username,
firstName: first_name,
lastName: last_name,
imageUrl: image_url,
});
console.log(
`User updated in database: ${user.id} (${getUserDisplayName(user)})`
);
}
} catch (error) {
console.error(`Error handling ${eventType}:`, error);
return new Response(
`Error: Failed to ${eventType === "user.created" ? "create" : "update"} user`,
{
status: 500,
}
);
}
}
return new Response("Webhook processed successfully", { status: 200 });
}