1 import { filter } from 'rxjs/operators'
2 import { Component, OnInit } from '@angular/core'
3 import { AuthService, Notifier, UserService } from '@app/core'
5 USER_CONFIRM_PASSWORD_VALIDATOR,
6 USER_EXISTING_PASSWORD_VALIDATOR,
7 USER_PASSWORD_VALIDATOR
8 } from '@app/shared/form-validators/user-validators'
9 import { FormReactive, FormValidatorService } from '@app/shared/shared-forms'
10 import { User } from '@shared/models'
13 selector: 'my-account-change-password',
14 templateUrl: './my-account-change-password.component.html',
15 styleUrls: [ './my-account-change-password.component.scss' ]
17 export class MyAccountChangePasswordComponent extends FormReactive implements OnInit {
22 protected formValidatorService: FormValidatorService,
23 private notifier: Notifier,
24 private authService: AuthService,
25 private userService: UserService
32 'current-password': USER_EXISTING_PASSWORD_VALIDATOR,
33 'new-password': USER_PASSWORD_VALIDATOR,
34 'new-confirmed-password': USER_CONFIRM_PASSWORD_VALIDATOR
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)
53 this.notifier.success($localize`Password updated.`)
60 if (err.status === 401) {
61 this.error = $localize`You current password is invalid.`
65 this.error = err.message