icns-frontend/pages/api/icns-verification.ts

52 lines
1.5 KiB
TypeScript
Raw Normal View History

2022-12-23 13:30:35 +00:00
import { captureException } from "@sentry/nextjs";
import { withIronSessionApiRoute } from "iron-session/next";
import type { NextApiRequest, NextApiResponse } from "next";
import { ironOptions } from "../../iron.config";
export default withIronSessionApiRoute(async function handler(
req: NextApiRequest,
res: NextApiResponse,
) {
try {
2022-12-12 06:31:52 +00:00
if (!process.env.ICNS_VERIFIER_ORIGIN_LIST) {
2022-12-23 13:30:35 +00:00
const errorMessage = ".env is not set";
console.error(errorMessage);
captureException(errorMessage);
return res.status(500).json({ error: "Internal server error" });
}
2022-12-12 06:31:52 +00:00
const verifierOriginList = process.env.ICNS_VERIFIER_ORIGIN_LIST.split(",");
const verificationList = await Promise.allSettled(
2022-12-23 13:30:35 +00:00
verifierOriginList.map(async (verfierOrigin) =>
(
await fetch(`${verfierOrigin}/api/verify_twitter`, {
2022-12-12 06:31:52 +00:00
method: "post",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify(req.body),
2022-12-23 13:30:35 +00:00
})
).json(),
2022-12-12 06:31:52 +00:00
),
);
2022-12-23 13:30:35 +00:00
const errorTrimmedVerificationList = verificationList.map(
(verification) => ({
...verification,
...(verification.status === "rejected" && {
reason: verification.reason.message || verification.reason,
}),
}),
);
2022-12-12 06:31:52 +00:00
res.status(200).json({
2022-12-23 13:30:35 +00:00
verificationList: errorTrimmedVerificationList,
2022-12-12 06:31:52 +00:00
});
} catch (error) {
console.error(error);
2022-12-23 13:30:35 +00:00
captureException(error);
res.status(500).json({ error: "Internal server error" });
}
},
ironOptions);