]>
Commit | Line | Data |
---|---|---|
67ed6552 C |
1 | import { forkJoin } from 'rxjs' |
2 | import { tap } from 'rxjs/operators' | |
0ba5f5ba | 3 | import { Component, OnInit } from '@angular/core' |
67ed6552 | 4 | import { AuthService, ServerService, UserService } from '@app/core' |
7ed1edbb C |
5 | import { USER_EMAIL_VALIDATOR, USER_PASSWORD_VALIDATOR } from '@app/shared/form-validators/user-validators' |
6 | import { FormReactive, FormValidatorService } from '@app/shared/shared-forms' | |
67ed6552 | 7 | import { User } from '@shared/models' |
0ba5f5ba C |
8 | |
9 | @Component({ | |
10 | selector: 'my-account-change-email', | |
11 | templateUrl: './my-account-change-email.component.html', | |
12 | styleUrls: [ './my-account-change-email.component.scss' ] | |
13 | }) | |
14 | export class MyAccountChangeEmailComponent extends FormReactive implements OnInit { | |
15 | error: string = null | |
16 | success: string = null | |
17 | user: User = null | |
18 | ||
19 | constructor ( | |
20 | protected formValidatorService: FormValidatorService, | |
0ba5f5ba C |
21 | private authService: AuthService, |
22 | private userService: UserService, | |
66357162 | 23 | private serverService: ServerService |
7ed1edbb | 24 | ) { |
0ba5f5ba C |
25 | super() |
26 | } | |
27 | ||
28 | ngOnInit () { | |
29 | this.buildForm({ | |
7ed1edbb C |
30 | 'new-email': USER_EMAIL_VALIDATOR, |
31 | 'password': USER_PASSWORD_VALIDATOR | |
0ba5f5ba C |
32 | }) |
33 | ||
34 | this.user = this.authService.getUser() | |
35 | } | |
36 | ||
37 | changeEmail () { | |
38 | this.error = null | |
39 | this.success = null | |
40 | ||
41 | const password = this.form.value[ 'password' ] | |
42 | const email = this.form.value[ 'new-email' ] | |
43 | ||
ba430d75 C |
44 | forkJoin([ |
45 | this.serverService.getConfig(), | |
46 | this.userService.changeEmail(password, email) | |
47 | ]).pipe(tap(() => this.authService.refreshUserInformation())) | |
48 | .subscribe( | |
49 | ([ config ]) => { | |
50 | this.form.reset() | |
0ba5f5ba | 51 | |
ba430d75 | 52 | if (config.signup.requiresEmailVerification) { |
66357162 | 53 | this.success = $localize`Please check your emails to verify your new email.` |
ba430d75 | 54 | } else { |
66357162 | 55 | this.success = $localize`Email updated.` |
ba430d75 C |
56 | } |
57 | }, | |
0ba5f5ba | 58 | |
ba430d75 C |
59 | err => { |
60 | if (err.status === 401) { | |
66357162 | 61 | this.error = $localize`You current password is invalid.` |
ba430d75 | 62 | return |
0ba5f5ba | 63 | } |
ba430d75 C |
64 | |
65 | this.error = err.message | |
66 | } | |
67 | ) | |
0ba5f5ba C |
68 | } |
69 | } |