+import { filter } from 'rxjs/operators'
import { Component, OnInit } from '@angular/core'
-import { FormBuilder, FormGroup } from '@angular/forms'
-import { NotificationsService } from 'angular2-notifications'
-import { FormReactive, USER_PASSWORD, UserService } from '../../../shared'
-import { I18n } from '@ngx-translate/i18n-polyfill'
+import { AuthService, Notifier, UserService } from '@app/core'
+import {
+ USER_CONFIRM_PASSWORD_VALIDATOR,
+ USER_EXISTING_PASSWORD_VALIDATOR,
+ USER_PASSWORD_VALIDATOR
+} from '@app/shared/form-validators/user-validators'
+import { FormReactive, FormValidatorService } from '@app/shared/shared-forms'
+import { User } from '@shared/models'
@Component({
selector: 'my-account-change-password',
})
export class MyAccountChangePasswordComponent extends FormReactive implements OnInit {
error: string = null
-
- form: FormGroup
- formErrors = {
- 'new-password': '',
- 'new-confirmed-password': ''
- }
- validationMessages = {
- 'new-password': USER_PASSWORD.MESSAGES,
- 'new-confirmed-password': USER_PASSWORD.MESSAGES
- }
+ user: User = null
constructor (
- private formBuilder: FormBuilder,
- private notificationsService: NotificationsService,
- private userService: UserService,
- private i18n: I18n
+ protected formValidatorService: FormValidatorService,
+ private notifier: Notifier,
+ private authService: AuthService,
+ private userService: UserService
) {
super()
}
- buildForm () {
- this.form = this.formBuilder.group({
- 'new-password': [ '', USER_PASSWORD.VALIDATORS ],
- 'new-confirmed-password': [ '', USER_PASSWORD.VALIDATORS ]
+ ngOnInit () {
+ this.buildForm({
+ 'current-password': USER_EXISTING_PASSWORD_VALIDATOR,
+ 'new-password': USER_PASSWORD_VALIDATOR,
+ 'new-confirmed-password': USER_CONFIRM_PASSWORD_VALIDATOR
})
- this.form.valueChanges.subscribe(data => this.onValueChanged(data))
- }
+ this.user = this.authService.getUser()
- ngOnInit () {
- this.buildForm()
+ const confirmPasswordControl = this.form.get('new-confirmed-password')
+
+ confirmPasswordControl.valueChanges
+ .pipe(filter(v => v !== this.form.value['new-password']))
+ .subscribe(() => confirmPasswordControl.setErrors({ matchPassword: true }))
}
changePassword () {
+ const currentPassword = this.form.value['current-password']
const newPassword = this.form.value['new-password']
- const newConfirmedPassword = this.form.value['new-confirmed-password']
- this.error = null
+ this.userService.changePassword(currentPassword, newPassword)
+ .subscribe({
+ next: () => {
+ this.notifier.success($localize`Password updated.`)
- if (newPassword !== newConfirmedPassword) {
- this.error = this.i18n('The new password and the confirmed password do not correspond.')
- return
- }
+ this.form.reset()
+ this.error = null
+ },
- this.userService.changePassword(newPassword).subscribe(
- () => this.notificationsService.success(this.i18n('Success'), this.i18n('Password updated.')),
+ error: err => {
+ if (err.status === 401) {
+ this.error = $localize`You current password is invalid.`
+ return
+ }
- err => this.error = err.message
- )
+ this.error = err.message
+ }
+ })
}
}