From fb26a52c2cb51197c12b7daf9cd6a12cc688a72c Mon Sep 17 00:00:00 2001 From: notmd Date: Sun, 29 Jan 2023 23:28:26 +0700 Subject: [PATCH] allow to enable/disable email signin captcha via env variable --- docker-compose.yaml | 1 + website/.env | 1 + website/src/pages/api/auth/[...nextauth].ts | 2 +- website/src/pages/auth/signin.tsx | 22 +++++++++++++++------ website/types/env.d.ts | 1 + 5 files changed, 20 insertions(+), 7 deletions(-) diff --git a/docker-compose.yaml b/docker-compose.yaml index 4376e25d..edb33162 100644 --- a/docker-compose.yaml +++ b/docker-compose.yaml @@ -127,6 +127,7 @@ services: - DEBUG_LOGIN=true - NEXT_PUBLIC_CLOUDFARE_CAPTCHA_SITE_KEY=1x00000000000000000000AA - CLOUDFLARE_CAPTCHA_SERCERT_KEY=1x0000000000000000000000000000000AA + - NEXT_PUBLIC_ENABLE_EMAIL_SIGNIN_CAPTCHA=true depends_on: webdb: condition: service_healthy diff --git a/website/.env b/website/.env index a5bb7e0a..e71e2a3b 100644 --- a/website/.env +++ b/website/.env @@ -17,3 +17,4 @@ EMAIL_FROM=info@example.com NEXT_PUBLIC_CLOUDFLARE_CAPTCHA_SITE_KEY=1x00000000000000000000AA CLOUDFLARE_CAPTCHA_SERCERT_KEY=1x0000000000000000000000000000000AA +NEXT_PUBLIC_ENABLE_EMAIL_SIGNIN_CAPTCHA=true diff --git a/website/src/pages/api/auth/[...nextauth].ts b/website/src/pages/api/auth/[...nextauth].ts index 065d67f4..bc876182 100644 --- a/website/src/pages/api/auth/[...nextauth].ts +++ b/website/src/pages/api/auth/[...nextauth].ts @@ -161,7 +161,7 @@ export default function auth(req: NextApiRequest, res: NextApiResponse) { callbacks: { ...authOptions.callbacks, async signIn({ account }) { - if (account.provider !== "email") { + if (account.provider !== "email" || !boolean(process.env.NEXT_PUBLIC_ENABLE_EMAIL_SIGNIN_CAPTCHA)) { return true; } diff --git a/website/src/pages/auth/signin.tsx b/website/src/pages/auth/signin.tsx index 3a2f48ff..a0826b3c 100644 --- a/website/src/pages/auth/signin.tsx +++ b/website/src/pages/auth/signin.tsx @@ -1,6 +1,7 @@ import { Button, ButtonProps, Input, Stack, useColorModeValue } from "@chakra-ui/react"; import { useColorMode } from "@chakra-ui/react"; import { TurnstileInstance } from "@marsidev/react-turnstile"; +import { boolean } from "boolean"; import { Bug, Github, Mail } from "lucide-react"; import { GetServerSideProps } from "next"; import Head from "next/head"; @@ -144,6 +145,8 @@ Signin.getLayout = (page) => ( export default Signin; +const emailSigninCaptcha = boolean(process.env.NEXT_PUBLIC_ENABLE_EMAIL_SIGNIN_CAPTCHA); + const EmailSignInForm = ({ providerId }: { providerId: string }) => { const { register, handleSubmit } = useForm<{ email: string }>(); const captcha = useRef(); @@ -166,12 +169,19 @@ const EmailSignInForm = ({ providerId }: { providerId: string }) => { placeholder="Email Address" {...register("email")} /> - setCaptchaSuccess(true)} - > - } mt="4" disabled={!captchaSuccess}> + {emailSigninCaptcha && ( + setCaptchaSuccess(true)} + > + )} + } + mt="4" + disabled={!captchaSuccess && emailSigninCaptcha} + > Continue with Email diff --git a/website/types/env.d.ts b/website/types/env.d.ts index 79fc51fc..b5484154 100644 --- a/website/types/env.d.ts +++ b/website/types/env.d.ts @@ -4,6 +4,7 @@ declare global { NODE_ENV: "development" | "production"; NEXT_PUBLIC_CLOUDFLARE_CAPTCHA_SITE_KEY: string; CLOUDFLARE_CAPTCHA_SERCERT_KEY: string; + NEXT_PUBLIC_ENABLE_EMAIL_SIGNIN_CAPTCHA: boolean; } } }