diff options
Diffstat (limited to 'client/src/app/login')
-rw-r--r-- | client/src/app/login/login.component.html | 19 | ||||
-rw-r--r-- | client/src/app/login/login.component.ts | 58 |
2 files changed, 54 insertions, 23 deletions
diff --git a/client/src/app/login/login.component.html b/client/src/app/login/login.component.html index 5848fcba3..94a405405 100644 --- a/client/src/app/login/login.component.html +++ b/client/src/app/login/login.component.html | |||
@@ -1,17 +1,16 @@ | |||
1 | <h3>Login</h3> | 1 | <h3>Login</h3> |
2 | 2 | ||
3 | |||
4 | <div *ngIf="error" class="alert alert-danger">{{ error }}</div> | 3 | <div *ngIf="error" class="alert alert-danger">{{ error }}</div> |
5 | 4 | ||
6 | <form role="form" (ngSubmit)="login(username.value, password.value)" #loginForm="ngForm"> | 5 | <form role="form" (ngSubmit)="login()" [formGroup]="form"> |
7 | <div class="form-group"> | 6 | <div class="form-group"> |
8 | <label for="username">Username</label> | 7 | <label for="username">Username</label> |
9 | <input | 8 | <input |
10 | type="text" class="form-control" name="username" id="username" placeholder="Username" required | 9 | type="text" class="form-control" id="username" placeholder="Username" required |
11 | ngControl="username" #username="ngForm" | 10 | formControlName="username" |
12 | > | 11 | > |
13 | <div [hidden]="username.valid || username.pristine" class="alert alert-danger"> | 12 | <div *ngIf="formErrors.username" class="alert alert-danger"> |
14 | Username is required | 13 | {{ formErrors.username }} |
15 | </div> | 14 | </div> |
16 | </div> | 15 | </div> |
17 | 16 | ||
@@ -19,12 +18,12 @@ | |||
19 | <label for="password">Password</label> | 18 | <label for="password">Password</label> |
20 | <input | 19 | <input |
21 | type="password" class="form-control" name="password" id="password" placeholder="Password" required | 20 | type="password" class="form-control" name="password" id="password" placeholder="Password" required |
22 | ngControl="password" #password="ngForm" | 21 | formControlName="password" |
23 | > | 22 | > |
24 | <div [hidden]="password.valid || password.pristine" class="alert alert-danger"> | 23 | <div *ngIf="formErrors.password" class="alert alert-danger"> |
25 | Password is required | 24 | {{ formErrors.password }} |
26 | </div> | 25 | </div> |
27 | </div> | 26 | </div> |
28 | 27 | ||
29 | <input type="submit" value="Login" class="btn btn-default" [disabled]="!loginForm.form.valid"> | 28 | <input type="submit" value="Login" class="btn btn-default" [disabled]="!form.valid"> |
30 | </form> | 29 | </form> |
diff --git a/client/src/app/login/login.component.ts b/client/src/app/login/login.component.ts index ddd62462e..c4ff7050b 100644 --- a/client/src/app/login/login.component.ts +++ b/client/src/app/login/login.component.ts | |||
@@ -1,35 +1,67 @@ | |||
1 | import { Component } from '@angular/core'; | 1 | import { Component, OnInit } from '@angular/core'; |
2 | import { FormBuilder, FormGroup, Validators } from '@angular/forms'; | ||
2 | import { Router } from '@angular/router'; | 3 | import { Router } from '@angular/router'; |
3 | 4 | ||
4 | import { AuthService } from '../shared'; | 5 | import { AuthService, FormReactive } from '../shared'; |
5 | 6 | ||
6 | @Component({ | 7 | @Component({ |
7 | selector: 'my-login', | 8 | selector: 'my-login', |
8 | template: require('./login.component.html') | 9 | templateUrl: './login.component.html' |
9 | }) | 10 | }) |
10 | 11 | ||
11 | export class LoginComponent { | 12 | export class LoginComponent extends FormReactive implements OnInit { |
12 | error: string = null; | 13 | error: string = null; |
13 | 14 | ||
15 | form: FormGroup; | ||
16 | formErrors = { | ||
17 | 'username': '', | ||
18 | 'password': '' | ||
19 | }; | ||
20 | validationMessages = { | ||
21 | 'username': { | ||
22 | 'required': 'Username is required.', | ||
23 | }, | ||
24 | 'password': { | ||
25 | 'required': 'Password is required.' | ||
26 | } | ||
27 | }; | ||
28 | |||
14 | constructor( | 29 | constructor( |
15 | private authService: AuthService, | 30 | private authService: AuthService, |
31 | private formBuilder: FormBuilder, | ||
16 | private router: Router | 32 | private router: Router |
17 | ) {} | 33 | ) { |
34 | super(); | ||
35 | } | ||
36 | |||
37 | buildForm() { | ||
38 | this.form = this.formBuilder.group({ | ||
39 | username: [ '', Validators.required ], | ||
40 | password: [ '', Validators.required ], | ||
41 | }); | ||
42 | |||
43 | this.form.valueChanges.subscribe(data => this.onValueChanged(data)); | ||
44 | } | ||
45 | |||
46 | ngOnInit() { | ||
47 | this.buildForm(); | ||
48 | } | ||
49 | |||
50 | login() { | ||
51 | this.error = null; | ||
52 | |||
53 | const { username, password } = this.form.value; | ||
18 | 54 | ||
19 | login(username: string, password: string) { | ||
20 | this.authService.login(username, password).subscribe( | 55 | this.authService.login(username, password).subscribe( |
21 | result => { | 56 | result => this.router.navigate(['/videos/list']), |
22 | this.error = null; | ||
23 | 57 | ||
24 | this.router.navigate(['/videos/list']); | ||
25 | }, | ||
26 | error => { | 58 | error => { |
27 | console.error(error); | 59 | console.error(error.json); |
28 | 60 | ||
29 | if (error.error === 'invalid_grant') { | 61 | if (error.json.error === 'invalid_grant') { |
30 | this.error = 'Credentials are invalid.'; | 62 | this.error = 'Credentials are invalid.'; |
31 | } else { | 63 | } else { |
32 | this.error = `${error.error}: ${error.error_description}`; | 64 | this.error = `${error.json.error}: ${error.json.error_description}`; |
33 | } | 65 | } |
34 | } | 66 | } |
35 | ); | 67 | ); |