diff options
Diffstat (limited to 'client/src/app/account')
-rw-r--r-- | client/src/app/account/account.component.html | 27 | ||||
-rw-r--r-- | client/src/app/account/account.component.ts | 67 | ||||
-rw-r--r-- | client/src/app/account/account.routes.ts | 5 | ||||
-rw-r--r-- | client/src/app/account/account.service.ts | 25 | ||||
-rw-r--r-- | client/src/app/account/index.ts | 3 |
5 files changed, 127 insertions, 0 deletions
diff --git a/client/src/app/account/account.component.html b/client/src/app/account/account.component.html new file mode 100644 index 000000000..5a8847acd --- /dev/null +++ b/client/src/app/account/account.component.html | |||
@@ -0,0 +1,27 @@ | |||
1 | <h3>Account</h3> | ||
2 | |||
3 | <div *ngIf="information" class="alert alert-success">{{ information }}</div> | ||
4 | <div *ngIf="error" class="alert alert-danger">{{ error }}</div> | ||
5 | |||
6 | <form role="form" (ngSubmit)="changePassword()" [formGroup]="form"> | ||
7 | <div class="form-group"> | ||
8 | <label for="new-password">New password</label> | ||
9 | <input | ||
10 | type="password" class="form-control" id="new-password" | ||
11 | formControlName="new-password" | ||
12 | > | ||
13 | <div *ngIf="formErrors['new-password']" class="alert alert-danger"> | ||
14 | {{ formErrors['new-password'] }} | ||
15 | </div> | ||
16 | </div> | ||
17 | |||
18 | <div class="form-group"> | ||
19 | <label for="name">Confirm new password</label> | ||
20 | <input | ||
21 | type="password" class="form-control" id="new-confirmed-password" | ||
22 | formControlName="new-confirmed-password" | ||
23 | > | ||
24 | </div> | ||
25 | |||
26 | <input type="submit" value="Change password" class="btn btn-default" [disabled]="!form.valid"> | ||
27 | </form> | ||
diff --git a/client/src/app/account/account.component.ts b/client/src/app/account/account.component.ts new file mode 100644 index 000000000..851eaf198 --- /dev/null +++ b/client/src/app/account/account.component.ts | |||
@@ -0,0 +1,67 @@ | |||
1 | import { } from '@angular/common'; | ||
2 | import { Component, OnInit } from '@angular/core'; | ||
3 | import { FormBuilder, FormGroup } from '@angular/forms'; | ||
4 | import { Router } from '@angular/router'; | ||
5 | |||
6 | import { AccountService } from './account.service'; | ||
7 | import { FormReactive, USER_PASSWORD } from '../shared'; | ||
8 | |||
9 | @Component({ | ||
10 | selector: 'my-account', | ||
11 | templateUrl: './account.component.html' | ||
12 | }) | ||
13 | |||
14 | export class AccountComponent extends FormReactive implements OnInit { | ||
15 | information: string = null; | ||
16 | error: string = null; | ||
17 | |||
18 | form: FormGroup; | ||
19 | formErrors = { | ||
20 | 'new-password': '', | ||
21 | 'new-confirmed-password': '' | ||
22 | }; | ||
23 | validationMessages = { | ||
24 | 'new-password': USER_PASSWORD.MESSAGES, | ||
25 | 'new-confirmed-password': USER_PASSWORD.MESSAGES | ||
26 | }; | ||
27 | |||
28 | constructor( | ||
29 | private accountService: AccountService, | ||
30 | private formBuilder: FormBuilder, | ||
31 | private router: Router | ||
32 | ) { | ||
33 | super(); | ||
34 | } | ||
35 | |||
36 | buildForm() { | ||
37 | this.form = this.formBuilder.group({ | ||
38 | 'new-password': [ '', USER_PASSWORD.VALIDATORS ], | ||
39 | 'new-confirmed-password': [ '', USER_PASSWORD.VALIDATORS ], | ||
40 | }); | ||
41 | |||
42 | this.form.valueChanges.subscribe(data => this.onValueChanged(data)); | ||
43 | } | ||
44 | |||
45 | ngOnInit() { | ||
46 | this.buildForm(); | ||
47 | } | ||
48 | |||
49 | changePassword() { | ||
50 | const newPassword = this.form.value['new-password']; | ||
51 | const newConfirmedPassword = this.form.value['new-confirmed-password']; | ||
52 | |||
53 | this.information = null; | ||
54 | this.error = null; | ||
55 | |||
56 | if (newPassword !== newConfirmedPassword) { | ||
57 | this.error = 'The new password and the confirmed password do not correspond.'; | ||
58 | return; | ||
59 | } | ||
60 | |||
61 | this.accountService.changePassword(newPassword).subscribe( | ||
62 | ok => this.information = 'Password updated.', | ||
63 | |||
64 | err => this.error = err | ||
65 | ); | ||
66 | } | ||
67 | } | ||
diff --git a/client/src/app/account/account.routes.ts b/client/src/app/account/account.routes.ts new file mode 100644 index 000000000..e348c6ebe --- /dev/null +++ b/client/src/app/account/account.routes.ts | |||
@@ -0,0 +1,5 @@ | |||
1 | import { AccountComponent } from './account.component'; | ||
2 | |||
3 | export const AccountRoutes = [ | ||
4 | { path: 'account', component: AccountComponent } | ||
5 | ]; | ||
diff --git a/client/src/app/account/account.service.ts b/client/src/app/account/account.service.ts new file mode 100644 index 000000000..355bcef74 --- /dev/null +++ b/client/src/app/account/account.service.ts | |||
@@ -0,0 +1,25 @@ | |||
1 | import { Injectable } from '@angular/core'; | ||
2 | |||
3 | import { AuthHttp, AuthService, RestExtractor } from '../shared'; | ||
4 | |||
5 | @Injectable() | ||
6 | export class AccountService { | ||
7 | private static BASE_USERS_URL = '/api/v1/users/'; | ||
8 | |||
9 | constructor( | ||
10 | private authHttp: AuthHttp, | ||
11 | private authService: AuthService, | ||
12 | private restExtractor: RestExtractor | ||
13 | ) {} | ||
14 | |||
15 | changePassword(newPassword: string) { | ||
16 | const url = AccountService.BASE_USERS_URL + this.authService.getUser().id; | ||
17 | const body = { | ||
18 | password: newPassword | ||
19 | }; | ||
20 | |||
21 | return this.authHttp.put(url, body) | ||
22 | .map(this.restExtractor.extractDataBool) | ||
23 | .catch((res) => this.restExtractor.handleError(res)); | ||
24 | } | ||
25 | } | ||
diff --git a/client/src/app/account/index.ts b/client/src/app/account/index.ts new file mode 100644 index 000000000..823d9fe5f --- /dev/null +++ b/client/src/app/account/index.ts | |||
@@ -0,0 +1,3 @@ | |||
1 | export * from './account.component'; | ||
2 | export * from './account.routes'; | ||
3 | export * from './account.service'; | ||