fix: challenge

This commit is contained in:
2025-06-30 20:44:56 +01:00
parent 7545fdf6ac
commit 76bee93338
2 changed files with 27 additions and 4 deletions

View File

@ -21,6 +21,23 @@ type TwitchSubscribeBody = {
};
};
export const twitchChallengeEvent = z.object({
challenge: z.string(),
subscription: z.object({
id: z.string(),
status: z.enum(['webhook_callback_verification_pending']),
type: z.enum(['channel.subscribe']),
version: z.string(),
condition: z.object({ broadcaster_user_id: z.string() }),
transport: z.object({
method: z.enum(['webhook']),
callback: z.string(),
}),
created_at: z.string(),
cost: z.number(),
})
});
// Not the complete event, but the information we need.
export const twitchFollowEvent = z.object({
subscription: z.object({
@ -31,6 +48,8 @@ export const twitchFollowEvent = z.object({
}),
});
export const twitchEvent = z.union([twitchFollowEvent, twitchChallengeEvent]);
/*
*curl -X POST 'https://api.twitch.tv/helix/eventsub/subscriptions' \
-H 'Authorization: Bearer 2gbdx6oar67tqtcmt49t3wpcgycthx' \

View File

@ -1,7 +1,7 @@
import { createHmac, timingSafeEqual } from "node:crypto";
import type { RequestHandler } from "@sveltejs/kit";
import { HASH_SECRET } from "$env/static/private";
import { twitchFollowEvent } from "$lib/twitch";
import { twitchChallengeEvent, twitchFollowEvent } from "$lib/twitch";
import { client } from "$lib";
const getHmacMessage = (request: Request, body: string): string => {
return (
@ -42,9 +42,13 @@ export const fallback: RequestHandler = async ({ request }) => {
console.log(jsonBody);
const event = twitchFollowEvent.safeParse(jsonBody);
if (!event.success) {
console.log("Not a follow event");
console.log(event.error);
return new Response();
const stuff = twitchChallengeEvent.safeParse(jsonBody);
if (!stuff.success) {
console.log("unknown event!!");
return new Response();
}
return new Response(stuff.data.challenge);
}
const { user_name } = event.data.event;