From 323380d7574c55e0b6da91ced6d02d0c7f44540d Mon Sep 17 00:00:00 2001 From: Navindu Amarakoon Date: Sat, 9 Dec 2023 10:37:16 +0530 Subject: [PATCH 1/8] feat: env variable to disable signing up --- apps/web/src/app/(unauthenticated)/signin/page.tsx | 14 ++++++++------ apps/web/src/app/(unauthenticated)/signup/page.tsx | 5 +++++ packages/trpc/server/auth-router/router.ts | 7 +++++++ 3 files changed, 20 insertions(+), 6 deletions(-) diff --git a/apps/web/src/app/(unauthenticated)/signin/page.tsx b/apps/web/src/app/(unauthenticated)/signin/page.tsx index a4890d849..0b0333b65 100644 --- a/apps/web/src/app/(unauthenticated)/signin/page.tsx +++ b/apps/web/src/app/(unauthenticated)/signin/page.tsx @@ -13,12 +13,14 @@ export default function SignInPage() { -

- Don't have an account?{' '} - - Sign up - -

+ {process.env.NEXT_PUBLIC_DISABLE_SIGNUP !== 'true' && ( +

+ Don't have an account?{' '} + + Sign up + +

+ )}

Create a new account

diff --git a/packages/trpc/server/auth-router/router.ts b/packages/trpc/server/auth-router/router.ts index 59c51ade5..24dd272ee 100644 --- a/packages/trpc/server/auth-router/router.ts +++ b/packages/trpc/server/auth-router/router.ts @@ -11,6 +11,13 @@ import { ZSignUpMutationSchema, ZVerifyPasswordMutationSchema } from './schema'; export const authRouter = router({ signup: procedure.input(ZSignUpMutationSchema).mutation(async ({ input }) => { try { + if (process.env.NEXT_PUBLIC_DISABLE_SIGNUP === 'true') { + throw new TRPCError({ + code: 'BAD_REQUEST', + message: 'Signups are disabled.', + }); + } + const { name, email, password, signature } = input; const user = await createUser({ name, email, password, signature }); From dbdef79263f5ae3acae594d9df78ea2598987226 Mon Sep 17 00:00:00 2001 From: Navindu Amarakoon Date: Sat, 9 Dec 2023 10:38:48 +0530 Subject: [PATCH 2/8] chore: remove old env variable from docker compose --- docker/compose.yml | 1 - 1 file changed, 1 deletion(-) diff --git a/docker/compose.yml b/docker/compose.yml index b427f419c..9d4f0e951 100644 --- a/docker/compose.yml +++ b/docker/compose.yml @@ -33,7 +33,6 @@ services: - SMTP_MAIL_USER=username - SMTP_MAIL_PASSWORD=password - MAIL_FROM=admin@example.com - - NEXT_PUBLIC_ALLOW_SIGNUP=true ports: - 3000:3000 volumes: From 78a1ee2af0ddc3465648e9c21aea351d73101570 Mon Sep 17 00:00:00 2001 From: Navindu Amarakoon Date: Sat, 9 Dec 2023 11:35:45 +0530 Subject: [PATCH 3/8] feat: disable oauth signup when DISABLE_SIGNUP is true --- apps/web/src/components/forms/signin.tsx | 2 ++ packages/lib/next-auth/auth-options.ts | 10 ++++++++++ packages/lib/next-auth/error-codes.ts | 1 + packages/tsconfig/process-env.d.ts | 2 ++ 4 files changed, 15 insertions(+) diff --git a/apps/web/src/components/forms/signin.tsx b/apps/web/src/components/forms/signin.tsx index 0d7dd723f..95dc6f9af 100644 --- a/apps/web/src/components/forms/signin.tsx +++ b/apps/web/src/components/forms/signin.tsx @@ -24,6 +24,7 @@ const ERROR_MESSAGES: Partial> = { 'This account appears to be using a social login method, please sign in using that method', [ErrorCode.INCORRECT_TWO_FACTOR_CODE]: 'The two-factor authentication code provided is incorrect', [ErrorCode.INCORRECT_TWO_FACTOR_BACKUP_CODE]: 'The backup code provided is incorrect', + [ErrorCode.SIGNUP_DISABLED]: 'Creating new accounts is currently disabled', }; const TwoFactorEnabledErrorCode = ErrorCode.TWO_FACTOR_MISSING_CREDENTIALS; @@ -146,6 +147,7 @@ export const SignInForm = ({ className }: SignInFormProps) => { try { await signIn('google', { callbackUrl: LOGIN_REDIRECT_PATH }); } catch (err) { + console.error(err); toast({ title: 'An unknown error occurred', description: diff --git a/packages/lib/next-auth/auth-options.ts b/packages/lib/next-auth/auth-options.ts index 6d59b0666..1d900c391 100644 --- a/packages/lib/next-auth/auth-options.ts +++ b/packages/lib/next-auth/auth-options.ts @@ -162,5 +162,15 @@ export const NEXT_AUTH_OPTIONS: AuthOptions = { return session; }, + + async signIn({ user }) { + if (process.env.NEXT_PUBLIC_DISABLE_SIGNUP === 'true') { + const userData = await getUserByEmail({ email: user.email! }); + + return !!userData; + } else { + return true; + } + }, }, }; diff --git a/packages/lib/next-auth/error-codes.ts b/packages/lib/next-auth/error-codes.ts index c3dfafece..f69206456 100644 --- a/packages/lib/next-auth/error-codes.ts +++ b/packages/lib/next-auth/error-codes.ts @@ -19,4 +19,5 @@ export const ErrorCode = { INCORRECT_PASSWORD: 'INCORRECT_PASSWORD', MISSING_ENCRYPTION_KEY: 'MISSING_ENCRYPTION_KEY', MISSING_BACKUP_CODE: 'MISSING_BACKUP_CODE', + SIGNUP_DISABLED: 'SIGNUP_DISABLED', } as const; diff --git a/packages/tsconfig/process-env.d.ts b/packages/tsconfig/process-env.d.ts index 717f13ade..7169120c8 100644 --- a/packages/tsconfig/process-env.d.ts +++ b/packages/tsconfig/process-env.d.ts @@ -55,6 +55,8 @@ declare namespace NodeJS { NEXT_PRIVATE_SMTP_FROM_NAME?: string; NEXT_PRIVATE_SMTP_FROM_ADDRESS?: string; + NEXT_PUBLIC_DISABLE_SIGNUP?: string; + /** * Vercel environment variables */ From 3b3987dcf8d55095384a3e74c427fbb7caa147b3 Mon Sep 17 00:00:00 2001 From: Navindu Amarakoon Date: Sat, 9 Dec 2023 11:43:30 +0530 Subject: [PATCH 4/8] chore: add env to env.example --- .env.example | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.env.example b/.env.example index 45c26f6be..dff33d77c 100644 --- a/.env.example +++ b/.env.example @@ -87,6 +87,8 @@ NEXT_PUBLIC_POSTHOG_KEY="" NEXT_PUBLIC_POSTHOG_HOST="https://eu.posthog.com" # OPTIONAL: Leave blank to disable billing. NEXT_PUBLIC_FEATURE_BILLING_ENABLED= +# OPTIONAL: Leave blank to allow users to signup through /signup page. +NEXT_PUBLIC_DISABLE_SIGNUP= # This is only required for the marketing site # [[REDIS]] From ee5ce78c822f512117f530aa886dc254c602b7c0 Mon Sep 17 00:00:00 2001 From: Navindu Amarakoon Date: Sat, 9 Dec 2023 11:48:46 +0530 Subject: [PATCH 5/8] chore: remove unused code --- apps/web/src/components/forms/signin.tsx | 1 - packages/lib/next-auth/error-codes.ts | 1 - 2 files changed, 2 deletions(-) diff --git a/apps/web/src/components/forms/signin.tsx b/apps/web/src/components/forms/signin.tsx index 95dc6f9af..9694bd581 100644 --- a/apps/web/src/components/forms/signin.tsx +++ b/apps/web/src/components/forms/signin.tsx @@ -24,7 +24,6 @@ const ERROR_MESSAGES: Partial> = { 'This account appears to be using a social login method, please sign in using that method', [ErrorCode.INCORRECT_TWO_FACTOR_CODE]: 'The two-factor authentication code provided is incorrect', [ErrorCode.INCORRECT_TWO_FACTOR_BACKUP_CODE]: 'The backup code provided is incorrect', - [ErrorCode.SIGNUP_DISABLED]: 'Creating new accounts is currently disabled', }; const TwoFactorEnabledErrorCode = ErrorCode.TWO_FACTOR_MISSING_CREDENTIALS; diff --git a/packages/lib/next-auth/error-codes.ts b/packages/lib/next-auth/error-codes.ts index f69206456..c3dfafece 100644 --- a/packages/lib/next-auth/error-codes.ts +++ b/packages/lib/next-auth/error-codes.ts @@ -19,5 +19,4 @@ export const ErrorCode = { INCORRECT_PASSWORD: 'INCORRECT_PASSWORD', MISSING_ENCRYPTION_KEY: 'MISSING_ENCRYPTION_KEY', MISSING_BACKUP_CODE: 'MISSING_BACKUP_CODE', - SIGNUP_DISABLED: 'SIGNUP_DISABLED', } as const; From 95041fa2e4d8fb58948e56420e79fe010c75d006 Mon Sep 17 00:00:00 2001 From: Navindu Amarakoon Date: Sat, 9 Dec 2023 12:05:36 +0530 Subject: [PATCH 6/8] fix: build error --- turbo.json | 1 + 1 file changed, 1 insertion(+) diff --git a/turbo.json b/turbo.json index 36b169a80..9ee878150 100644 --- a/turbo.json +++ b/turbo.json @@ -45,6 +45,7 @@ "NEXT_PUBLIC_STRIPE_COMMUNITY_PLAN_YEARLY_PRICE_ID", "NEXT_PUBLIC_STRIPE_COMMUNITY_PLAN_MONTHLY_PRICE_ID", "NEXT_PUBLIC_STRIPE_FREE_PLAN_ID", + "NEXT_PUBLIC_DISABLE_SIGNUP", "NEXT_PRIVATE_DATABASE_URL", "NEXT_PRIVATE_DIRECT_DATABASE_URL", "NEXT_PRIVATE_GOOGLE_CLIENT_ID", From 5c1d30bfbb8e935459885ac871b71c0fcbe7fbdd Mon Sep 17 00:00:00 2001 From: Navindu Amarakoon Date: Sun, 10 Dec 2023 09:23:31 +0530 Subject: [PATCH 7/8] chore: remove console log --- apps/web/src/components/forms/signin.tsx | 1 - 1 file changed, 1 deletion(-) diff --git a/apps/web/src/components/forms/signin.tsx b/apps/web/src/components/forms/signin.tsx index 9694bd581..0d7dd723f 100644 --- a/apps/web/src/components/forms/signin.tsx +++ b/apps/web/src/components/forms/signin.tsx @@ -146,7 +146,6 @@ export const SignInForm = ({ className }: SignInFormProps) => { try { await signIn('google', { callbackUrl: LOGIN_REDIRECT_PATH }); } catch (err) { - console.error(err); toast({ title: 'An unknown error occurred', description: From 682cb37786ea6ac78cdefb88063d000110d02c1d Mon Sep 17 00:00:00 2001 From: Lucas Smith Date: Fri, 15 Dec 2023 20:41:54 +1100 Subject: [PATCH 8/8] fix: update auth-options --- packages/lib/next-auth/auth-options.ts | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/packages/lib/next-auth/auth-options.ts b/packages/lib/next-auth/auth-options.ts index 1d900c391..3b9492807 100644 --- a/packages/lib/next-auth/auth-options.ts +++ b/packages/lib/next-auth/auth-options.ts @@ -164,13 +164,15 @@ export const NEXT_AUTH_OPTIONS: AuthOptions = { }, async signIn({ user }) { + // We do this to stop OAuth providers from creating an account + // when signups are disabled if (process.env.NEXT_PUBLIC_DISABLE_SIGNUP === 'true') { const userData = await getUserByEmail({ email: user.email! }); return !!userData; - } else { - return true; } + + return true; }, }, };