From f825ae54fb589075e2323eb08382c5c2663832ab Mon Sep 17 00:00:00 2001 From: notmd Date: Mon, 30 Jan 2023 02:15:34 +0700 Subject: [PATCH] try to fix test --- website/src/pages/api/auth/[...nextauth].ts | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/website/src/pages/api/auth/[...nextauth].ts b/website/src/pages/api/auth/[...nextauth].ts index bc876182..5a316256 100644 --- a/website/src/pages/api/auth/[...nextauth].ts +++ b/website/src/pages/api/auth/[...nextauth].ts @@ -166,11 +166,8 @@ export default function auth(req: NextApiRequest, res: NextApiResponse) { } const captcha = req.body.captcha; - // https://stackoverflow.com/questions/66111742/get-the-client-ip-on-nextjs-and-use-ssr - const forwarded = req.headers["x-forwarded-for"]; - const ip = typeof forwarded === "string" ? forwarded.split(/, /)[0] : req.socket.remoteAddress; - const res = await checkCaptcha(captcha, ip); + const res = await checkCaptcha(captcha, getIp(req)); if (res.success) { return true; @@ -181,3 +178,13 @@ export default function auth(req: NextApiRequest, res: NextApiResponse) { }, }); } + +const getIp = (req: NextApiRequest) => { + try { + // https://stackoverflow.com/questions/66111742/get-the-client-ip-on-nextjs-and-use-ssr + const forwarded = req.headers["x-forwarded-for"]; + return typeof forwarded === "string" ? forwarded.split(/, /)[0] : req.socket.remoteAddress; + } catch { + return ""; + } +};