Where can I extend the input type for trpc router?

Hey, I wrote a tRPC recaptcha v3 middleware for my public procedures, I am making use of the input object to pass the recaptchaToken, how can I extend the input object to include this as an optional attribute? Also on a sidenote, is there an alternative other than input that I could use, my requests will be from client components...
const recaptchaMiddleware = t.middleware(async ({ next, ctx, input }) => {
if (Object.keys(input ?? {}).includes("recaptchaToken")) {
const res = await fetch("https://www.google.com/recaptcha/api/siteverify", {
method: "POST",
headers: {
"Content-Type": "application/x-www-form-urlencoded",
},
body: new URLSearchParams({
secret: process.env.RECAPTCHA_SECRET_KEY ?? "",
response: (input as { [key: string]: unknown; recaptchaToken: string })
.recaptchaToken,
}),
});
if (!res.ok) {
throw new Error("Failed to verify reCAPTCHA token");
}
const data = (await res.json()) as {
success: true | false; // whether this request was a valid reCAPTCHA token for your site
score: number; // the score for this request (0.0 - 1.0)
action: string; // the action name for this request (important to verify)
challenge_ts: string; // timestamp of the challenge load (ISO format yyyy-MM-dd'T'HH:mm:ssZZ)
hostname: string; // the hostname of the site where the reCAPTCHA was solved
"error-codes": string[] | undefined; // error codes if the request failed
};
if (!data.success) {
throw new Error("reCAPTCHA verification failed");
}
if (data.score < 0.5) {
throw new Error("reCAPTCHA score too low");
}
}
return next({
ctx: {
...ctx,
recaptcha: {
verified: true,
},
},
});
});
const recaptchaMiddleware = t.middleware(async ({ next, ctx, input }) => {
if (Object.keys(input ?? {}).includes("recaptchaToken")) {
const res = await fetch("https://www.google.com/recaptcha/api/siteverify", {
method: "POST",
headers: {
"Content-Type": "application/x-www-form-urlencoded",
},
body: new URLSearchParams({
secret: process.env.RECAPTCHA_SECRET_KEY ?? "",
response: (input as { [key: string]: unknown; recaptchaToken: string })
.recaptchaToken,
}),
});
if (!res.ok) {
throw new Error("Failed to verify reCAPTCHA token");
}
const data = (await res.json()) as {
success: true | false; // whether this request was a valid reCAPTCHA token for your site
score: number; // the score for this request (0.0 - 1.0)
action: string; // the action name for this request (important to verify)
challenge_ts: string; // timestamp of the challenge load (ISO format yyyy-MM-dd'T'HH:mm:ssZZ)
hostname: string; // the hostname of the site where the reCAPTCHA was solved
"error-codes": string[] | undefined; // error codes if the request failed
};
if (!data.success) {
throw new Error("reCAPTCHA verification failed");
}
if (data.score < 0.5) {
throw new Error("reCAPTCHA score too low");
}
}
return next({
ctx: {
...ctx,
recaptcha: {
verified: true,
},
},
});
});
0 Replies
No replies yetBe the first to reply to this messageJoin

Did you find this page helpful?