From 78a1ee2af0ddc3465648e9c21aea351d73101570 Mon Sep 17 00:00:00 2001 From: Navindu Amarakoon Date: Sat, 9 Dec 2023 11:35:45 +0530 Subject: [PATCH] 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 */