]>
Commit | Line | Data |
---|---|---|
1 | import { forkJoin } from 'rxjs' | |
2 | import { tap } from 'rxjs/operators' | |
3 | import { Component, OnInit } from '@angular/core' | |
4 | import { AuthService, Notifier, ServerService, UserService } from '@app/core' | |
5 | import { USER_EMAIL_VALIDATOR, USER_PASSWORD_VALIDATOR } from '@app/shared/form-validators/user-validators' | |
6 | import { FormReactive, FormReactiveService } from '@app/shared/shared-forms' | |
7 | import { HttpStatusCode, User } from '@shared/models' | |
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 formReactiveService: FormReactiveService, | |
21 | private authService: AuthService, | |
22 | private userService: UserService, | |
23 | private serverService: ServerService, | |
24 | private notifier: Notifier | |
25 | ) { | |
26 | super() | |
27 | } | |
28 | ||
29 | ngOnInit () { | |
30 | this.buildForm({ | |
31 | 'new-email': USER_EMAIL_VALIDATOR, | |
32 | password: USER_PASSWORD_VALIDATOR | |
33 | }) | |
34 | ||
35 | this.user = this.authService.getUser() | |
36 | } | |
37 | ||
38 | changeEmail () { | |
39 | this.error = null | |
40 | this.success = null | |
41 | ||
42 | const password = this.form.value['password'] | |
43 | const email = this.form.value['new-email'] | |
44 | ||
45 | forkJoin([ | |
46 | this.serverService.getConfig(), | |
47 | this.userService.changeEmail(password, email) | |
48 | ]).pipe(tap(() => this.authService.refreshUserInformation())) | |
49 | .subscribe({ | |
50 | next: ([ config ]) => { | |
51 | this.form.reset() | |
52 | ||
53 | if (config.signup.requiresEmailVerification) { | |
54 | this.success = $localize`Please check your emails to verify your new email.` | |
55 | } else { | |
56 | this.success = $localize`Email updated.` | |
57 | } | |
58 | }, | |
59 | ||
60 | error: err => { | |
61 | if (err.status === HttpStatusCode.UNAUTHORIZED_401) { | |
62 | this.error = $localize`You current password is invalid.` | |
63 | return | |
64 | } | |
65 | ||
66 | this.error = err.message | |
67 | } | |
68 | }) | |
69 | } | |
70 | } |