1 import { Component, OnInit } from '@angular/core'
2 import { NotificationsService } from 'angular2-notifications'
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 { AuthService } from '@app/core';
9 import { User } from '../../../../../../shared';
12 selector: 'my-account-change-password',
13 templateUrl: './my-account-change-password.component.html',
14 styleUrls: [ './my-account-change-password.component.scss' ]
16 export class MyAccountChangePasswordComponent extends FormReactive implements OnInit {
21 protected formValidatorService: FormValidatorService,
22 private userValidatorsService: UserValidatorsService,
23 private notificationsService: NotificationsService,
24 private authService: AuthService,
25 private userService: UserService,
33 'old-password': this.userValidatorsService.USER_PASSWORD,
34 'new-password': this.userValidatorsService.USER_PASSWORD,
35 'new-confirmed-password': this.userValidatorsService.USER_CONFIRM_PASSWORD
38 this.user = this.authService.getUser()
40 const confirmPasswordControl = this.form.get('new-confirmed-password')
42 confirmPasswordControl.valueChanges
43 .pipe(filter(v => v !== this.form.value[ 'new-password' ]))
44 .subscribe(() => confirmPasswordControl.setErrors({ matchPassword: true }))
49 const oldPassword = this.form.value[ 'old-password' ];
51 // compare old password
52 this.authService.login(this.user.account.name, oldPassword)
54 () => this.changePassword(),
56 if (err.message.indexOf('credentials are invalid') !== -1) this.error = this.i18n('Incorrect old password.')
57 else this.error = err.message
63 private changePassword(){
64 this.userService.changePassword(this.form.value[ 'new-password' ]).subscribe(
66 this.notificationsService.success(this.i18n('Success'), this.i18n('Password updated.'))
71 err => this.error = err.message