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);
try {
const registeredUser: ISystemUser = await this.authApplication.signUp(
registerDTO
);
await this.authApplication.signUp(registerDTO);
return res.status(200).send({
type: 'success',
code: 'REGISTER.SUCCESS',
@@ -243,18 +242,10 @@ export default class AuthenticationController extends BaseController {
errors: [{ type: 'EMAIL.NOT.REGISTERED', code: 500 }],
});
}
}
if (error instanceof ServiceErrors) {
const errorReasons = [];
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 });
if (error.errorType === 'EMAIL_EXISTS') {
return res.status(400).send({
errors: [{ type: 'EMAIL.EXISTS', code: 600 }],
});
}
}
next(error);

View File

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

View File

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

View File

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

View File

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

View File

@@ -1,27 +1,29 @@
import { Container, Service } from 'typedi';
import events from '@/subscribers/events';
import { IAuthSignedInEventPayload } from '@/interfaces';
@Service()
export default class ResetLoginThrottleSubscriber {
/**
* Attaches events with handlers.
* @param bus
* @param 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.
* @param {IAuthSignedInEventPayload} payload -
*/
private async resetLoginThrottleOnceSuccessLogin(payload) {
const { emailOrPhone, password, user } = payload;
private async resetLoginThrottleOnceSuccessLogin(
payload: IAuthSignedInEventPayload
) {
const { email, user } = payload;
const loginThrottler = Container.get('rateLimiter.login');
// Reset the login throttle by the given email and phone number.
await loginThrottler.reset(user.email);
await loginThrottler.reset(user.phoneNumber);
await loginThrottler.reset(emailOrPhone);
await loginThrottler.reset(email);
}
}

View File

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

View File

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

View File

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