1 import { Component, OnInit } from '@angular/core'
2 import { AuthService, Notifier } from '@app/core'
3 import { FormReactive, UserService } from '../../../shared'
4 import { I18n } from '@ngx-translate/i18n-polyfill'
5 import { FormValidatorService } from '@app/shared/forms/form-validators/form-validator.service'
6 import { UserValidatorsService } from '@app/shared/forms/form-validators/user-validators.service'
7 import { filter } from 'rxjs/operators'
8 import { User } from '../../../../../../shared'
11 selector: 'my-account-change-password',
12 templateUrl: './my-account-change-password.component.html',
13 styleUrls: [ './my-account-change-password.component.scss' ]
15 export class MyAccountChangePasswordComponent extends FormReactive implements OnInit {
20 protected formValidatorService: FormValidatorService,
21 private userValidatorsService: UserValidatorsService,
22 private notifier: Notifier,
23 private authService: AuthService,
24 private userService: UserService,
32 'current-password': this.userValidatorsService.USER_PASSWORD,
33 'new-password': this.userValidatorsService.USER_PASSWORD,
34 'new-confirmed-password': this.userValidatorsService.USER_CONFIRM_PASSWORD
37 this.user = this.authService.getUser()
39 const confirmPasswordControl = this.form.get('new-confirmed-password')
41 confirmPasswordControl.valueChanges
42 .pipe(filter(v => v !== this.form.value[ 'new-password' ]))
43 .subscribe(() => confirmPasswordControl.setErrors({ matchPassword: true }))
47 const currentPassword = this.form.value[ 'current-password' ]
48 const newPassword = this.form.value[ 'new-password' ]
50 this.userService.changePassword(currentPassword, newPassword).subscribe(
52 this.notifier.success(this.i18n('Password updated.'))
59 if (err.status === 401) {
60 this.error = this.i18n('You current password is invalid.')
64 this.error = err.message