feat(server): remove phone number from authentication endpoints

This commit is contained in:
a.bouhuolia
2023-04-05 23:57:26 +02:00
parent 4a22576d88
commit 85b24c7a4f
9 changed files with 42 additions and 51 deletions

View File

@@ -152,9 +152,8 @@ export default class AuthenticationController extends BaseController {
const registerDTO: IRegisterDTO = this.matchedBodyData(req); const registerDTO: IRegisterDTO = this.matchedBodyData(req);
try { try {
const registeredUser: ISystemUser = await this.authApplication.signUp( await this.authApplication.signUp(registerDTO);
registerDTO
);
return res.status(200).send({ return res.status(200).send({
type: 'success', type: 'success',
code: 'REGISTER.SUCCESS', code: 'REGISTER.SUCCESS',
@@ -243,18 +242,10 @@ export default class AuthenticationController extends BaseController {
errors: [{ type: 'EMAIL.NOT.REGISTERED', code: 500 }], errors: [{ type: 'EMAIL.NOT.REGISTERED', code: 500 }],
}); });
} }
} if (error.errorType === 'EMAIL_EXISTS') {
if (error instanceof ServiceErrors) { return res.status(400).send({
const errorReasons = []; errors: [{ type: 'EMAIL.EXISTS', code: 600 }],
});
if (error.hasType('PHONE_NUMBER_EXISTS')) {
errorReasons.push({ type: 'PHONE_NUMBER_EXISTS', code: 100 });
}
if (error.hasType('EMAIL_EXISTS')) {
errorReasons.push({ type: 'EMAIL.EXISTS', code: 200 });
}
if (errorReasons.length > 0) {
return res.boom.badRequest(null, { errors: errorReasons });
} }
} }
next(error); next(error);

View File

@@ -8,18 +8,12 @@ import JWTAuth from '@/api/middleware/jwtAuth';
import TenancyMiddleware from '@/api/middleware/TenancyMiddleware'; import TenancyMiddleware from '@/api/middleware/TenancyMiddleware';
import AttachCurrentTenantUser from '@/api/middleware/AttachCurrentTenantUser'; import AttachCurrentTenantUser from '@/api/middleware/AttachCurrentTenantUser';
import OrganizationService from '@/services/Organization/OrganizationService'; import OrganizationService from '@/services/Organization/OrganizationService';
import { import { MONTHS, ACCEPTED_LOCALES } from '@/services/Organization/constants';
ACCEPTED_CURRENCIES,
MONTHS,
ACCEPTED_LOCALES,
} from '@/services/Organization/constants';
import { DATE_FORMATS } from '@/services/Miscellaneous/DateFormats/constants'; import { DATE_FORMATS } from '@/services/Miscellaneous/DateFormats/constants';
import { ServiceError } from '@/exceptions'; import { ServiceError } from '@/exceptions';
import BaseController from '@/api/controllers/BaseController'; import BaseController from '@/api/controllers/BaseController';
const ACCEPTED_LOCATIONS = ['libya'];
@Service() @Service()
export default class OrganizationController extends BaseController { export default class OrganizationController extends BaseController {
@Inject() @Inject()
@@ -65,8 +59,8 @@ export default class OrganizationController extends BaseController {
return [ return [
check('name').exists().trim(), check('name').exists().trim(),
check('industry').optional().isString(), check('industry').optional().isString(),
check('location').exists().isString().isIn(ACCEPTED_LOCATIONS), check('location').exists().isString().isISO31661Alpha2(),
check('base_currency').exists().isIn(ACCEPTED_CURRENCIES), check('base_currency').exists().isISO4217(),
check('timezone').exists().isIn(moment.tz.names()), check('timezone').exists().isIn(moment.tz.names()),
check('fiscal_year').exists().isIn(MONTHS), check('fiscal_year').exists().isIn(MONTHS),
check('language').exists().isString().isIn(ACCEPTED_LOCALES), check('language').exists().isString().isIn(ACCEPTED_LOCALES),

View File

@@ -2,6 +2,7 @@ import { Container, Inject } from 'typedi';
import { cloneDeep } from 'lodash'; import { cloneDeep } from 'lodash';
import { Tenant } from '@/system/models'; import { Tenant } from '@/system/models';
import { import {
IAuthSignedInEventPayload,
IAuthSigningInEventPayload, IAuthSigningInEventPayload,
IAuthSignInPOJO, IAuthSignInPOJO,
ISystemUser, ISystemUser,
@@ -22,9 +23,9 @@ export class AuthSigninService {
/** /**
* Validates the given email and password. * Validates the given email and password.
* @param {ISystemUser} user * @param {ISystemUser} user
* @param {string} email * @param {string} email
* @param {string} password * @param {string} password
*/ */
public async validateSignIn( public async validateSignIn(
user: ISystemUser, user: ISystemUser,
@@ -69,7 +70,7 @@ export class AuthSigninService {
await this.validateSignIn(user, email, password); await this.validateSignIn(user, email, password);
// Triggers on signing-in event. // Triggers on signing-in event.
await this.eventPublisher.emitAsync(events.auth.logining, { await this.eventPublisher.emitAsync(events.auth.signingIn, {
email, email,
password, password,
user, user,
@@ -80,12 +81,13 @@ export class AuthSigninService {
// Update the last login at of the user. // Update the last login at of the user.
await systemUserRepository.patchLastLoginAt(user.id); await systemUserRepository.patchLastLoginAt(user.id);
// Triggers `onLogin` event. // Triggers `onSignIn` event.
await this.eventPublisher.emitAsync(events.auth.login, { await this.eventPublisher.emitAsync(events.auth.signIn, {
email, email,
password, password,
user, user,
}); } as IAuthSignedInEventPayload);
const tenant = await Tenant.query() const tenant = await Tenant.query()
.findById(user.tenantId) .findById(user.tenantId)
.withGraphFetched('metadata'); .withGraphFetched('metadata');

View File

@@ -39,7 +39,7 @@ export class AuthSignupService {
const hashedPassword = await hashPassword(signupDTO.password); const hashedPassword = await hashPassword(signupDTO.password);
// Triggers signin up event. // Triggers signin up event.
await this.eventPublisher.emitAsync(events.auth.registering, { await this.eventPublisher.emitAsync(events.auth.signingUp, {
signupDTO, signupDTO,
} as IAuthSigningUpEventPayload); } as IAuthSigningUpEventPayload);
@@ -52,7 +52,7 @@ export class AuthSignupService {
inviteAcceptedAt: moment().format('YYYY-MM-DD'), inviteAcceptedAt: moment().format('YYYY-MM-DD'),
}); });
// Triggers signed up event. // Triggers signed up event.
await this.eventPublisher.emitAsync(events.auth.register, { await this.eventPublisher.emitAsync(events.auth.signUp, {
signupDTO, signupDTO,
tenant, tenant,
user: registeredUser, user: registeredUser,

View File

@@ -14,8 +14,6 @@ export const DATE_FORMATS = [
'MMMM dd, YYYY', 'MMMM dd, YYYY',
'EEE, MMMM dd, YYYY', 'EEE, MMMM dd, YYYY',
]; ];
export const ACCEPTED_CURRENCIES = Object.keys(currencies);
export const MONTHS = [ export const MONTHS = [
'january', 'january',
'february', 'february',

View File

@@ -1,27 +1,29 @@
import { Container, Service } from 'typedi'; import { Container, Service } from 'typedi';
import events from '@/subscribers/events'; import events from '@/subscribers/events';
import { IAuthSignedInEventPayload } from '@/interfaces';
@Service() @Service()
export default class ResetLoginThrottleSubscriber { export default class ResetLoginThrottleSubscriber {
/** /**
* Attaches events with handlers. * Attaches events with handlers.
* @param bus * @param bus
*/ */
public attach(bus) { public attach(bus) {
bus.subscribe(events.auth.login, this.resetLoginThrottleOnceSuccessLogin); bus.subscribe(events.auth.signIn, this.resetLoginThrottleOnceSuccessLogin);
} }
/** /**
* Resets the login throttle once the login success. * Resets the login throttle once the login success.
* @param {IAuthSignedInEventPayload} payload -
*/ */
private async resetLoginThrottleOnceSuccessLogin(payload) { private async resetLoginThrottleOnceSuccessLogin(
const { emailOrPhone, password, user } = payload; payload: IAuthSignedInEventPayload
) {
const { email, user } = payload;
const loginThrottler = Container.get('rateLimiter.login'); const loginThrottler = Container.get('rateLimiter.login');
// Reset the login throttle by the given email and phone number. // Reset the login throttle by the given email and phone number.
await loginThrottler.reset(user.email); await loginThrottler.reset(user.email);
await loginThrottler.reset(user.phoneNumber); await loginThrottler.reset(email);
await loginThrottler.reset(emailOrPhone);
} }
} }

View File

@@ -10,14 +10,14 @@ export default class AuthSendWelcomeMailSubscriber {
* Attaches events with handlers. * Attaches events with handlers.
*/ */
public attach(bus) { public attach(bus) {
bus.subscribe(events.auth.register, this.sendWelcomeEmailOnceUserRegister); bus.subscribe(events.auth.signUp, this.sendWelcomeEmailOnceUserRegister);
} }
/** /**
* Sends welcome email once the user register. * Sends welcome email once the user register.
*/ */
private sendWelcomeEmailOnceUserRegister = async (payload) => { private sendWelcomeEmailOnceUserRegister = async (payload) => {
const { registerDTO, tenant, user } = payload; const { tenant, user } = payload;
// Send welcome mail to the user. // Send welcome mail to the user.
await this.agenda.now('welcome-email', { await this.agenda.now('welcome-email', {

View File

@@ -3,13 +3,17 @@ export default {
* Authentication service. * Authentication service.
*/ */
auth: { auth: {
login: 'onLogin', signIn: 'onSignIn',
logining: 'onLogining', signingIn: 'onSigningIn',
register: 'onRegister',
registering: 'onAuthRegistering', signUp: 'onSignUp',
sendResetPassword: 'onSendResetPassword', signingUp: 'onSigningUp',
sendingResetPassword: 'onSendingResetPassword', sendingResetPassword: 'onSendingResetPassword',
sendResetPassword: 'onSendResetPassword',
resetPassword: 'onResetPassword', resetPassword: 'onResetPassword',
resetingPassword: 'onResetingPassword'
}, },
/** /**

View File

@@ -15,14 +15,15 @@ import {
} from '@/components'; } from '@/components';
import { inputIntent } from '@/utils'; import { inputIntent } from '@/utils';
import { CLASSES } from '@/constants/classes'; import { CLASSES } from '@/constants/classes';
import { getCountries } from '@/constants/countries';
import { getAllCurrenciesOptions } from '@/constants/currencies'; import { getAllCurrenciesOptions } from '@/constants/currencies';
import { getFiscalYear } from '@/constants/fiscalYearOptions'; import { getFiscalYear } from '@/constants/fiscalYearOptions';
import { getLanguages } from '@/constants/languagesOptions'; import { getLanguages } from '@/constants/languagesOptions';
import { useGeneralFormContext } from './GeneralFormProvider'; import { useGeneralFormContext } from './GeneralFormProvider';
import { getAllCountries } from '@/utils/countries';
import { shouldBaseCurrencyUpdate } from './utils'; import { shouldBaseCurrencyUpdate } from './utils';
const Countries = getAllCountries();
/** /**
* Preferences general form. * Preferences general form.
*/ */
@@ -30,7 +31,6 @@ export default function PreferencesGeneralForm({ isSubmitting }) {
const history = useHistory(); const history = useHistory();
const FiscalYear = getFiscalYear(); const FiscalYear = getFiscalYear();
const Countries = getCountries();
const Languages = getLanguages(); const Languages = getLanguages();
const Currencies = getAllCurrenciesOptions(); const Currencies = getAllCurrenciesOptions();