]>
Commit | Line | Data |
---|---|---|
67ed6552 | 1 | import { filter } from 'rxjs/operators' |
df98563e | 2 | import { Component, OnInit } from '@angular/core' |
67ed6552 | 3 | import { AuthService, Notifier, UserService } from '@app/core' |
9df52d66 C |
4 | import { |
5 | USER_CONFIRM_PASSWORD_VALIDATOR, | |
6 | USER_EXISTING_PASSWORD_VALIDATOR, | |
7 | USER_PASSWORD_VALIDATOR | |
8 | } from '@app/shared/form-validators/user-validators' | |
7ed1edbb | 9 | import { FormReactive, FormValidatorService } from '@app/shared/shared-forms' |
67ed6552 | 10 | import { User } from '@shared/models' |
af5e743b C |
11 | |
12 | @Component({ | |
13 | selector: 'my-account-change-password', | |
4bb6886d C |
14 | templateUrl: './my-account-change-password.component.html', |
15 | styleUrls: [ './my-account-change-password.component.scss' ] | |
af5e743b | 16 | }) |
4bb6886d | 17 | export class MyAccountChangePasswordComponent extends FormReactive implements OnInit { |
df98563e | 18 | error: string = null |
3805ce3f | 19 | user: User = null |
af5e743b | 20 | |
df98563e | 21 | constructor ( |
d18d6478 | 22 | protected formValidatorService: FormValidatorService, |
f8b2c1b4 | 23 | private notifier: Notifier, |
3805ce3f | 24 | private authService: AuthService, |
66357162 | 25 | private userService: UserService |
9df52d66 | 26 | ) { |
df98563e | 27 | super() |
af5e743b C |
28 | } |
29 | ||
df98563e | 30 | ngOnInit () { |
d18d6478 | 31 | this.buildForm({ |
f8b530e0 | 32 | 'current-password': USER_EXISTING_PASSWORD_VALIDATOR, |
7ed1edbb C |
33 | 'new-password': USER_PASSWORD_VALIDATOR, |
34 | 'new-confirmed-password': USER_CONFIRM_PASSWORD_VALIDATOR | |
d18d6478 | 35 | }) |
af5e743b | 36 | |
3805ce3f B |
37 | this.user = this.authService.getUser() |
38 | ||
b0ee41df | 39 | const confirmPasswordControl = this.form.get('new-confirmed-password') |
af5e743b | 40 | |
b0ee41df | 41 | confirmPasswordControl.valueChanges |
9df52d66 | 42 | .pipe(filter(v => v !== this.form.value['new-password'])) |
b0ee41df | 43 | .subscribe(() => confirmPasswordControl.setErrors({ matchPassword: true })) |
a94419a6 J |
44 | } |
45 | ||
a890d1e0 | 46 | changePassword () { |
9df52d66 C |
47 | const currentPassword = this.form.value['current-password'] |
48 | const newPassword = this.form.value['new-password'] | |
3805ce3f | 49 | |
1378c0d3 C |
50 | this.userService.changePassword(currentPassword, newPassword) |
51 | .subscribe({ | |
52 | next: () => { | |
53 | this.notifier.success($localize`Password updated.`) | |
af5e743b | 54 | |
1378c0d3 C |
55 | this.form.reset() |
56 | this.error = null | |
57 | }, | |
af5e743b | 58 | |
1378c0d3 C |
59 | error: err => { |
60 | if (err.status === 401) { | |
61 | this.error = $localize`You current password is invalid.` | |
62 | return | |
63 | } | |
a890d1e0 | 64 | |
1378c0d3 C |
65 | this.error = err.message |
66 | } | |
67 | }) | |
af5e743b C |
68 | } |
69 | } |