refs #6427 feat: front restorePasswordSMS

This commit is contained in:
Javier Segarra 2024-03-20 13:50:37 +01:00
parent bfc8cda8a2
commit 8d6fc401a1
4 changed files with 60 additions and 11 deletions

View File

@ -12,10 +12,29 @@
</vn-textfield>
<vn-textfield
ng-if="$ctrl.sms"
disabled="$ctrl.otp"
label="User's id"
ng-model="$ctrl.userId"
type="number"
required="true"
vn-focus>
</vn-textfield>
<vn-textfield
ng-if="$ctrl.sms"
disabled="$ctrl.otp"
label="User's phone"
ng-model="$ctrl.user"
required="true"
vn-focus>
</vn-textfield>
<vn-textfield
ng-if="$ctrl.otp"
label="Verification code"
ng-model="$ctrl.code"
vn-name="code"
autocomplete="false"
class="vn-mt-md">
</vn-textfield>
<div class="text-secondary">
<span ng-if="$ctrl.sms" translate>
We will sent you a sms

View File

@ -1,14 +1,16 @@
import UserError from '../../../core/lib/user-error';
import ngModule from '../../module';
export default class Controller {
constructor($scope, $element, $http, vnApp, $translate, $state) {
constructor($scope, $element, $http, vnApp, $translate, $state, $location) {
Object.assign(this, {
$scope,
$element,
$http,
vnApp,
$translate,
$state
$state,
$location
});
$scope.$watch('$ctrl.user', function(nuevoValor) {
let isSms = /([+]\d{2})?\d{9}$/ig.test(nuevoValor ?? '');
@ -16,24 +18,50 @@ export default class Controller {
$scope.$ctrl.sms = isSms;
});
}
// ONLY FOR TESTS
// $onInit() {
// this.$scope.$ctrl.sms = true;
// this.$scope.$ctrl.userId = '9';
// this.$scope.$ctrl.user = '432978106';
// }
goToLogin() {
this.vnApp.showSuccess(this.$translate.instant('Notification sent!'));
this.$state.go('login');
}
goToChangePassword({valid: isValid, token}) {
if (!isValid)
this.vnApp.showError(this.$translate.instant('Invalid login'));
else {
this.$state.params.userId = this.userId;
this.$location.path('/reset-password').search('access_token', token.id);
}
}
goToOTP(otp) {
this.otp = true;
this.$state.params.otp = otp;
this.code = otp;
}
submit() {
const params = {
user: this.user
};
if (!this.user || (this.sms && !this.userId) || (this.otp && !this.code))
throw new UserError(`Credentials not valid`);
this.$http.post('VnUsers/recoverPassword', params)
.then(() => {
this.goToLogin();
});
if (this.sms || this.otp) {
this.$http.post('VnUsers/recoverPasswordSMS', {id: this.userId, phone: this.user, otp: this.code})
.then(({data}) => {
data.otp && this.goToOTP(data.otp);
data.valid && this.goToChangePassword(data);
});
} else {
this.$http.post('VnUsers/recoverPassword', {user: this.user})
.then(() => {
this.goToLogin();
});
}
}
}
Controller.$inject = ['$scope', '$element', '$http', 'vnApp', '$translate', '$state'];
Controller.$inject = ['$scope', '$element', '$http', 'vnApp', '$translate', '$state', '$location'];
ngModule.vnComponent('vnRecoverPassword', {
template: require('./index.html'),

View File

@ -4,3 +4,5 @@ We will sent you a sms: Te enviaremos un sms para restablecer tu contraseña
Notification sent!: ¡Notificación enviada!
User or recovery email: Usuario o correo de recuperación
User's phone: Móvil del usuario
User's id: Id del usuario
Credentials not valid: Credenciales no válidas

View File

@ -28,7 +28,7 @@ export default class Controller {
throw new UserError(`Passwords don't match`);
const headers = {
Authorization: this.$location.$$search.access_token
Authorization: this.$location.$$search.access_token ?? this.$state.params.access_token
};
const newPassword = this.newPassword;