Merge branch 'signup-restrictions' into develop

This commit is contained in:
a.bouhuolia
2023-05-08 00:36:50 +02:00
4 changed files with 19 additions and 47 deletions

View File

@@ -277,30 +277,18 @@ export default class AuthenticationController extends BaseController {
], ],
}); });
} }
if (error.errorType === 'SIGNUP_NOT_ALLOWED_EMAIL_DOMAIN') { if (error.errorType === 'SIGNUP_RESTRICTED_NOT_ALLOWED') {
return res.status(400).send({ return res.status(400).send({
errors: [ errors: [
{ {
type: 'SIGNUP_NOT_ALLOWED_EMAIL_DOMAIN', type: 'SIGNUP_RESTRICTED_NOT_ALLOWED',
message: message:
'Sign-up is restricted the given email domain is not allowed to sign-up.', 'Sign-up is restricted the given email address is not allowed to sign-up.',
code: 710, code: 710,
}, },
], ],
}); });
} }
if (error.errorType === 'SIGNUP_NOT_ALLOWED_EMAIL_ADDRESS') {
return res.status(400).send({
errors: [
{
type: 'SIGNUP_NOT_ALLOWED_EMAIL_ADDRESS',
message:
'The sign-up restricted the given email address is not allowed to sign-up.',
code: 720,
},
],
});
}
} }
next(error); next(error);
} }

View File

@@ -87,30 +87,24 @@ export class AuthSignupService {
// Can't continue if the signup is not disabled. // Can't continue if the signup is not disabled.
if (!config.signupRestrictions.disabled) return; if (!config.signupRestrictions.disabled) return;
// Validate the allowed domains. // Validate the allowed email addresses and domains.
if (!isEmpty(config.signupRestrictions.allowedDomains)) { if (
!isEmpty(config.signupRestrictions.allowedEmails) ||
!isEmpty(config.signupRestrictions.allowedDomains)
) {
const emailDomain = email.split('@').pop(); const emailDomain = email.split('@').pop();
const isAllowed = config.signupRestrictions.allowedDomains.some( const isAllowedEmail =
(domain) => emailDomain === domain
);
if (!isAllowed) {
throw new ServiceError(ERRORS.SIGNUP_NOT_ALLOWED_EMAIL_DOMAIN);
}
}
// Validate the allowed email addresses.
if (!isEmpty(config.signupRestrictions.allowedEmails)) {
const isAllowed =
config.signupRestrictions.allowedEmails.indexOf(email) !== -1; config.signupRestrictions.allowedEmails.indexOf(email) !== -1;
if (!isAllowed) { const isAllowedDomain = config.signupRestrictions.allowedDomains.some(
throw new ServiceError(ERRORS.SIGNUP_NOT_ALLOWED_EMAIL_ADDRESS); (domain) => emailDomain === domain
} );
if (!isAllowedEmail && !isAllowedDomain) {
throw new ServiceError(ERRORS.SIGNUP_RESTRICTED_NOT_ALLOWED);
} }
// Throw error if the signup is disabled with no exceptions. // Throw error if the signup is disabled with no exceptions.
if ( } else {
isEmpty(config.signupRestrictions.allowedDomains) &&
isEmpty(config.signupRestrictions.allowedEmails)
) {
throw new ServiceError(ERRORS.SIGNUP_RESTRICTED); throw new ServiceError(ERRORS.SIGNUP_RESTRICTED);
} }
} }

View File

@@ -7,8 +7,6 @@ export const ERRORS = {
TOKEN_EXPIRED: 'TOKEN_EXPIRED', TOKEN_EXPIRED: 'TOKEN_EXPIRED',
PHONE_NUMBER_EXISTS: 'PHONE_NUMBER_EXISTS', PHONE_NUMBER_EXISTS: 'PHONE_NUMBER_EXISTS',
EMAIL_EXISTS: 'EMAIL_EXISTS', EMAIL_EXISTS: 'EMAIL_EXISTS',
SIGNUP_RESTRICTED_NOT_ALLOWED: 'SIGNUP_RESTRICTED_NOT_ALLOWED',
SIGNUP_NOT_ALLOWED_EMAIL_ADDRESS: 'SIGNUP_NOT_ALLOWED_EMAIL_ADDRESS',
SIGNUP_NOT_ALLOWED_EMAIL_DOMAIN: 'SIGNUP_NOT_ALLOWED_EMAIL_DOMAIN',
SIGNUP_RESTRICTED: 'SIGNUP_RESTRICTED', SIGNUP_RESTRICTED: 'SIGNUP_RESTRICTED',
}; };

View File

@@ -98,15 +98,7 @@ export const transformRegisterErrorsToForm = (errors) => {
export const transformRegisterToastMessages = (errors) => { export const transformRegisterToastMessages = (errors) => {
const toastErrors = []; const toastErrors = [];
if (errors.some((e) => e.type === 'SIGNUP_NOT_ALLOWED_EMAIL_DOMAIN')) { if (errors.some((e) => e.type === 'SIGNUP_RESTRICTED_NOT_ALLOWED')) {
toastErrors.push({
message:
'The sign-up is restricted, the given email domain is not allowed to sign-up.',
intent: Intent.DANGER,
});
} else if (
errors.some((e) => e.type === 'SIGNUP_NOT_ALLOWED_EMAIL_ADDRESS')
) {
toastErrors.push({ toastErrors.push({
message: message:
'The sign-up is restricted, the given email address is not allowed to sign-up.', 'The sign-up is restricted, the given email address is not allowed to sign-up.',