1 import { filter } from 'rxjs/operators'
2 import { Component, OnInit } from '@angular/core'
3 import { AuthService, Notifier, UserService } from '@app/core'
4 import { USER_CONFIRM_PASSWORD_VALIDATOR, USER_PASSWORD_VALIDATOR, USER_EXISTING_PASSWORD_VALIDATOR } from '@app/shared/form-validators/user-validators'
5 import { FormReactive, FormValidatorService } from '@app/shared/shared-forms'
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 notifier: Notifier,
20 private authService: AuthService,
21 private userService: UserService
28 'current-password': USER_EXISTING_PASSWORD_VALIDATOR,
29 'new-password': USER_PASSWORD_VALIDATOR,
30 'new-confirmed-password': USER_CONFIRM_PASSWORD_VALIDATOR
33 this.user = this.authService.getUser()
35 const confirmPasswordControl = this.form.get('new-confirmed-password')
37 confirmPasswordControl.valueChanges
38 .pipe(filter(v => v !== this.form.value[ 'new-password' ]))
39 .subscribe(() => confirmPasswordControl.setErrors({ matchPassword: true }))
43 const currentPassword = this.form.value[ 'current-password' ]
44 const newPassword = this.form.value[ 'new-password' ]
46 this.userService.changePassword(currentPassword, newPassword).subscribe(
48 this.notifier.success($localize`Password updated.`)
55 if (err.status === 401) {
56 this.error = $localize`You current password is invalid.`
60 this.error = err.message