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