feat: disable oauth signup when DISABLE_SIGNUP is true

This commit is contained in:
Navindu Amarakoon
2023-12-09 11:35:45 +05:30
parent dbdef79263
commit 78a1ee2af0
4 changed files with 15 additions and 0 deletions

View File

@ -24,6 +24,7 @@ const ERROR_MESSAGES: Partial<Record<keyof typeof ErrorCode, string>> = {
'This account appears to be using a social login method, please sign in using that method', '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_CODE]: 'The two-factor authentication code provided is incorrect',
[ErrorCode.INCORRECT_TWO_FACTOR_BACKUP_CODE]: 'The backup 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; const TwoFactorEnabledErrorCode = ErrorCode.TWO_FACTOR_MISSING_CREDENTIALS;
@ -146,6 +147,7 @@ export const SignInForm = ({ className }: SignInFormProps) => {
try { try {
await signIn('google', { callbackUrl: LOGIN_REDIRECT_PATH }); await signIn('google', { callbackUrl: LOGIN_REDIRECT_PATH });
} catch (err) { } catch (err) {
console.error(err);
toast({ toast({
title: 'An unknown error occurred', title: 'An unknown error occurred',
description: description:

View File

@ -162,5 +162,15 @@ export const NEXT_AUTH_OPTIONS: AuthOptions = {
return session; 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;
}
},
}, },
}; };

View File

@ -19,4 +19,5 @@ export const ErrorCode = {
INCORRECT_PASSWORD: 'INCORRECT_PASSWORD', INCORRECT_PASSWORD: 'INCORRECT_PASSWORD',
MISSING_ENCRYPTION_KEY: 'MISSING_ENCRYPTION_KEY', MISSING_ENCRYPTION_KEY: 'MISSING_ENCRYPTION_KEY',
MISSING_BACKUP_CODE: 'MISSING_BACKUP_CODE', MISSING_BACKUP_CODE: 'MISSING_BACKUP_CODE',
SIGNUP_DISABLED: 'SIGNUP_DISABLED',
} as const; } as const;

View File

@ -55,6 +55,8 @@ declare namespace NodeJS {
NEXT_PRIVATE_SMTP_FROM_NAME?: string; NEXT_PRIVATE_SMTP_FROM_NAME?: string;
NEXT_PRIVATE_SMTP_FROM_ADDRESS?: string; NEXT_PRIVATE_SMTP_FROM_ADDRESS?: string;
NEXT_PUBLIC_DISABLE_SIGNUP?: string;
/** /**
* Vercel environment variables * Vercel environment variables
*/ */