From 9953df2f4fb564bf243aec1c018d5e371beb7335 Mon Sep 17 00:00:00 2001 From: Javier Segarra Date: Fri, 24 May 2024 11:11:22 +0200 Subject: [PATCH] perf(salix): refs #6427 imrpove --- back/methods/vn-user/recover-passwordSMS.js | 54 ++++++++----------- e2e/paths/01-salix/04_recoverPassword.spec.js | 20 ++----- .../components/recover-password/index.js | 9 ++-- 3 files changed, 31 insertions(+), 52 deletions(-) diff --git a/back/methods/vn-user/recover-passwordSMS.js b/back/methods/vn-user/recover-passwordSMS.js index 9a45e6486..f5787610a 100644 --- a/back/methods/vn-user/recover-passwordSMS.js +++ b/back/methods/vn-user/recover-passwordSMS.js @@ -7,7 +7,7 @@ module.exports = Self => { accepts: [ { - arg: 'userId', + arg: 'user', type: 'string', description: 'The recoveryPhone user\'s', required: true @@ -28,44 +28,36 @@ module.exports = Self => { } }); - Self.recoverPasswordSMS = async function(userId, verificationCode, options) { + Self.recoverPasswordSMS = async function(user, verificationCode, options) { + const models = Self.app.models; const myOptions = {}; if (typeof options == 'object') Object.assign(myOptions, options); - const user = await Self.findOne({ - fields: ['id', 'name', 'email', 'name', 'recoveryPhone'], - where: { - or: [ - {id: userId}, - {nickname: userId}, - {name: userId}, - {email: userId}, - ] - } + const usesEmail = user.indexOf('@') !== -1; + const filter = usesEmail ? {email: user} : {name: user}; + // if (!usesEmail) { + const account = await models.VnUser.findOne({ + fields: ['id', 'name', 'recoveryPhone'], + where: filter }); - if (!user) throw new UserError('Credentials not valid'); + if (!account) return; + user = account; + // } - try { - if (verificationCode) { - await Self.validateCode(user.name, verificationCode); + if (verificationCode) { + await Self.validateCode(user.name, verificationCode); - return { - token: await user.accessTokens.create({}) - }; - } - - const code = await authCode(user, myOptions); - - if (isProduction(true)) - return {code}; - await Self.app.models.Sms.send({req: {accessToken: {userId}}}, +userId, code); - } catch (err) { - if (err.code === 'EMAIL_NOT_FOUND') - return; - else - throw err; + return { + token: await user.accessTokens.create({}) + }; } + + const code = await authCode(user, myOptions); + + if (isProduction(true)) + return {code}; + await Self.app.models.Sms.send({req: {accessToken: {user: user.id}}}, +user.recoveryPhone, code); }; }; diff --git a/e2e/paths/01-salix/04_recoverPassword.spec.js b/e2e/paths/01-salix/04_recoverPassword.spec.js index b04f7d690..3a5bc7ef3 100644 --- a/e2e/paths/01-salix/04_recoverPassword.spec.js +++ b/e2e/paths/01-salix/04_recoverPassword.spec.js @@ -8,6 +8,10 @@ fdescribe('RecoverPassword path', async() => { beforeAll(async() => { browser = await getBrowser(); page = browser.page; + }); + + beforeEach(async() => { + await page.waitForState('login'); await page.waitToClick(selectors.recoverPassword.recoverPasswordButton); await page.waitForState('recover-password'); @@ -33,47 +37,33 @@ fdescribe('RecoverPassword path', async() => { }); it('should send email using email', async() => { - await page.waitForState('login'); - await page.waitToClick(selectors.recoverPassword.recoverPasswordButton); - await page.write(selectors.recoverPassword.email, 'BruceWayne@mydomain.com'); await page.waitToClick(selectors.recoverPassword.emailOption); await page.waitToClick(selectors.recoverPassword.sendEmailButton); const message = await page.waitForSnackbar(); - await page.waitForState('login'); expect(message.text).toContain('Notification sent!'); }); it('should send email using username', async() => { - await page.waitForState('login'); - await page.waitToClick(selectors.recoverPassword.recoverPasswordButton); - await page.write(selectors.recoverPassword.email, 'BruceWayne'); await page.waitToClick(selectors.recoverPassword.emailOption); await page.waitToClick(selectors.recoverPassword.sendEmailButton); const message = await page.waitForSnackbar(); - await page.waitForState('login'); expect(message.text).toContain('Notification sent!'); }); it('should not throw error if not exist user when select sms option', async() => { - await page.waitForState('login'); - await page.waitToClick(selectors.recoverPassword.recoverPasswordButton); await page.write(selectors.recoverPassword.email, 'fakeEmail@mydomain.com'); await page.waitToClick(selectors.recoverPassword.smsOption); await page.waitToClick(selectors.recoverPassword.sendEmailButton); - const message = await page.waitForSnackbar(); - expect(message.text).toContain('Credentials not valid'); - expect(await page.getState()).toContain('recover-password'); + expect(message.text).toContain('Notification sent!'); }); it('should send sms using username', async() => { - await page.clearInput(selectors.recoverPassword.email); - await page.write(selectors.recoverPassword.email, 'BruceWayne'); await page.waitToClick(selectors.recoverPassword.smsOption); await page.waitToClick(selectors.recoverPassword.sendEmailButton); diff --git a/front/salix/components/recover-password/index.js b/front/salix/components/recover-password/index.js index 17d2a2bad..a982daba7 100644 --- a/front/salix/components/recover-password/index.js +++ b/front/salix/components/recover-password/index.js @@ -33,19 +33,16 @@ export default class Controller { 'email': { url: 'VnUsers/recoverPassword', data: {user: this.user}, - cb: data => { - if (data === '') this.goToLogin(); - } + }, 'sms': { url: 'VnUsers/recoverPasswordSMS', - data: {userId: this.user, verificationCode: this.verificationCode}, + data: {user: this.user, verificationCode: this.verificationCode}, cb: data => { if (this.method && this.code) { data.token && this.goToChangePassword(data); !data.token && this.goToLogin(); } else - data.code && this.handleCode(data.code); } }, @@ -57,7 +54,7 @@ export default class Controller { throw new UserError(`Credentials not valid`); const method = this.methodsAvailables()[this.method]; this.$http.post(method.url, method.data) - .then(({data}) => method.cb(data)); + .then(({data}) => data === '' ? this.goToLogin() : method.cb(data)); } } Controller.$inject = ['$scope', '$element', '$http', 'vnApp', '$translate', '$state', '$location'];