feat(salix): refs #6427 update recover-password

This commit is contained in:
Javier Segarra 2024-05-23 14:33:33 +02:00
parent 9cf4ef04c5
commit bdaf5da826
5 changed files with 56 additions and 54 deletions

View File

@ -6,13 +6,13 @@ module.exports = Self => {
accepts: [ accepts: [
{ {
arg: 'recoveryPhone', arg: 'userId',
type: 'string', type: 'string',
description: 'The recoveryPhone user\'s', description: 'The recoveryPhone user\'s',
required: true required: true
}, },
{ {
arg: 'otp', arg: 'verificationCode',
type: 'string', type: 'string',
description: 'Code tovalidate operation' description: 'Code tovalidate operation'
} }
@ -27,21 +27,21 @@ module.exports = Self => {
} }
}); });
Self.recoverPasswordSMS = async function(recoveryPhone, otp, options) { Self.recoverPasswordSMS = async function(userId, verificationCode, options) {
const myOptions = {}; const myOptions = {};
if (typeof options == 'object') if (typeof options == 'object')
Object.assign(myOptions, options); Object.assign(myOptions, options);
const user = await Self.findOne({ const user = await Self.findOne({
fields: ['id', 'recoveryPhone', 'email', 'name'], fields: ['id', 'userId', 'email', 'name', 'recoveryPhone'],
where: {recoveryPhone} where: {userId}
}); });
if (!user) throw new UserError('Credentials not valid'); if (!user) throw new UserError('Credentials not valid');
try { try {
if (otp) { if (verificationCode) {
await Self.validateCode(user.name, otp); await Self.validateCode(user.name, verificationCode);
return { return {
token: await user.accessTokens.create({}) token: await user.accessTokens.create({})
@ -51,8 +51,8 @@ module.exports = Self => {
const code = await authCode(user, myOptions); const code = await authCode(user, myOptions);
if (process.env.NODE_ENV === 'production') if (process.env.NODE_ENV === 'production')
await Self.app.models.Sms.send({req: {accessToken: {userId: id}}}, +recoveryPhone, code); await Self.app.models.Sms.send({req: {accessToken: {userId: id}}}, +userId, code);
return {otp: true}; return {code};
} catch (err) { } catch (err) {
if (err.code === 'EMAIL_NOT_FOUND') if (err.code === 'EMAIL_NOT_FOUND')
return; return;

View File

@ -1,27 +1,42 @@
<h5 class="vn-mb-md vn-mt-lg" translate>Recover password</h5> <h5 class="vn-mb-md vn-mt-lg" translate>Recover password</h5>
<vn-textfield <vn-textfield
disabled="$ctrl.code"
label="User or recovery email" label="User or recovery email"
ng-model="$ctrl.user" ng-model="$ctrl.user"
vn-focus> vn-focus>
</vn-textfield> </vn-textfield>
<vn-textfield <vn-textfield
ng-if="$ctrl.otp" ng-if="$ctrl.code"
label="Verification code" label="Verification code"
ng-model="$ctrl.code" ng-model="$ctrl.verificationCode"
vn-name="code" vn-name="verificationCode"
autocomplete="false" autocomplete="false"
class="vn-mt-md"> class="vn-mt-md">
</vn-textfield> </vn-textfield>
<div class="text-secondary" ng-if="$ctrl.user"> <vn-one>
<span ng-if="$ctrl.isPhone" translate> <vn-vertical class="vn-mb-sm">
<vn-radio
disabled="$ctrl.code"
label="Móvil"
val="sms"
ng-model="$ctrl.method" >
</vn-radio>
<vn-radio
disabled="$ctrl.code"
label="Correo de recuperación"
val="email"
ng-model="$ctrl.method" >
</vn-radio></vn-vertical></vn-one>
<div class="text-secondary" ng-if="$ctrl.method && $ctrl.user">
<span ng-if="$ctrl.method ==='sms'" translate>
We will sent you a sms to recover your password We will sent you a sms to recover your password
</span> </span>
<span ng-if="!$ctrl.isPhone " translate> <span ng-if="$ctrl.method ==='email' " translate>
We will sent you an email to recover your password We will sent you an email to recover your password
</span> </span>
</div> </div>
<div class="footer"> <div class="footer">
<vn-submit disabled="!$ctrl.user" label="Recover password" ng-click="$ctrl.submit()"></vn-submit> <vn-submit disabled="!$ctrl.user || !$ctrl.method || ($ctrl.code&&!$ctrl.verificationCode)" label="Recover password" ng-click="$ctrl.submit()"></vn-submit>
<div class="spinner-wrapper"> <div class="spinner-wrapper">
<vn-spinner enable="$ctrl.loading"></vn-spinner> <vn-spinner enable="$ctrl.loading"></vn-spinner>
</div> </div>

View File

@ -12,11 +12,6 @@ export default class Controller {
$state, $state,
$location $location
}); });
$scope.$watch('$ctrl.user', function(nuevoValor) {
let isPhone = /^[\d+]+$/ig.test(nuevoValor ?? '');
// Evaluo si el valor introducido es un número y se marca o desmarca el checkbox de revibir por sms
$scope.$ctrl.isPhone = isPhone;
});
} }
goToLogin() { goToLogin() {
@ -29,28 +24,34 @@ export default class Controller {
else else
this.$location.path('/reset-password').search('access_token', token.id); this.$location.path('/reset-password').search('access_token', token.id);
} }
goToOTP(otp) { goToOTP(code) {
this.otp = true; this.code = true;
this.$state.params.otp = otp; this.$state.params.verificationCode = code;
this.code = null; // this.code = null;
}
methodsAvailables() {
return {
'email': {
url: 'VnUsers/recoverPassword', data: {user: this.user}
},
'sms': {
url: 'VnUsers/recoverPasswordSMS', data: {userId: this.user, verificationCode: this.verificationCode}
},
};
} }
submit() { submit() {
if (!this.user || (this.sms) || (this.otp && !this.code)) if (!this.user || (this.sms) || (this.code && !this.code))
throw new UserError(`Credentials not valid`); throw new UserError(`Credentials not valid`);
const method = this.methodsAvailables()[this.method];
if (this.isPhone || this.otp) { this.$http.post(method.url, method.data)
this.$http.post('VnUsers/recoverPasswordSMS', {recoveryPhone: this.user, otp: this.code}) .then(({data}) => {
.then(({data}) => { if (this.method && this.code) {
data.otp && this.goToOTP(data.otp);
data.token && this.goToChangePassword(data); data.token && this.goToChangePassword(data);
}); !data.token && this.goToLogin();
} else { } else
this.$http.post('VnUsers/recoverPassword', {user: this.user}) data.code && this.goToOTP(data.code);
.then(() => { });
this.goToLogin();
});
}
} }
} }
Controller.$inject = ['$scope', '$element', '$http', 'vnApp', '$translate', '$state', '$location']; Controller.$inject = ['$scope', '$element', '$http', 'vnApp', '$translate', '$state', '$location'];

View File

@ -3,7 +3,7 @@ We will sent you an email to recover your password: Te enviaremos un correo para
We will sent you a sms to recover your password: Te enviaremos un sms para restablecer tu contraseña We will sent you a sms to recover your password: Te enviaremos un sms para restablecer tu contraseña
We will sent you a item to recover your password: Te enviaremos un {{mode}} para restablecer tu contraseña We will sent you a item to recover your password: Te enviaremos un {{mode}} para restablecer tu contraseña
Notification sent!: ¡Notificación enviada! Notification sent!: ¡Notificación enviada!
User or recovery email: Usuario, móvil o email recuperación User or recovery email: Usuario
User's phone: Móvil del usuario User's phone: Móvil del usuario
User's id: Id del usuario User's id: Id del usuario
Credentials not valid: Credenciales no válidas Credentials not valid: Credenciales no válidas

View File

@ -49,11 +49,7 @@ module.exports = Self => {
const userId = ctx.options.accessToken.userId; const userId = ctx.options.accessToken.userId;
const isOwner = instanceId === userId; const isOwner = instanceId === userId;
const phoneHasChanged = !!ctx.data.user?.recoveryPhone; const phoneHasChanged = !!ctx.data.user?.recoveryPhone;
try {
await Self.app.models.VnUser.userSecurity(ctx, ctx.where.id);
} catch (error) {
throw new UserError('Phone can\'t be updated');
}
if (!isOwner) { if (!isOwner) {
if (phoneHasChanged) { if (phoneHasChanged) {
const {recoveryPhone} = ctx.data.user; const {recoveryPhone} = ctx.data.user;
@ -64,14 +60,4 @@ module.exports = Self => {
} }
delete ctx.data.user; delete ctx.data.user;
}); });
// async function checkModifyPermission(ctx) {
// const instanceId = ctx.currentInstance.id;
// const userId = ctx.options.accessToken.userId;
// try {
// if (instanceId === userId) return true;
// return
// } catch (error) {
// throw new UserError(error);
// }
// }
}; };