1 import { filter } from 'rxjs/operators'
2 import { Component, OnInit } from '@angular/core'
3 import { AuthService, Notifier, UserService } from '@app/core'
4 import { FormReactive, FormValidatorService, UserValidatorsService } from '@app/shared/shared-forms'
5 import { I18n } from '@ngx-translate/i18n-polyfill'
6 import { User } from '@shared/models'
9 selector: 'my-account-change-password',
10 templateUrl: './my-account-change-password.component.html',
11 styleUrls: [ './my-account-change-password.component.scss' ]
13 export class MyAccountChangePasswordComponent extends FormReactive implements OnInit {
18 protected formValidatorService: FormValidatorService,
19 private userValidatorsService: UserValidatorsService,
20 private notifier: Notifier,
21 private authService: AuthService,
22 private userService: UserService,
30 'current-password': this.userValidatorsService.USER_PASSWORD,
31 'new-password': this.userValidatorsService.USER_PASSWORD,
32 'new-confirmed-password': this.userValidatorsService.USER_CONFIRM_PASSWORD
35 this.user = this.authService.getUser()
37 const confirmPasswordControl = this.form.get('new-confirmed-password')
39 confirmPasswordControl.valueChanges
40 .pipe(filter(v => v !== this.form.value[ 'new-password' ]))
41 .subscribe(() => confirmPasswordControl.setErrors({ matchPassword: true }))
45 const currentPassword = this.form.value[ 'current-password' ]
46 const newPassword = this.form.value[ 'new-password' ]
48 this.userService.changePassword(currentPassword, newPassword).subscribe(
50 this.notifier.success(this.i18n('Password updated.'))
57 if (err.status === 401) {
58 this.error = this.i18n('You current password is invalid.')
62 this.error = err.message