]>
Commit | Line | Data |
---|---|---|
0f6da32b | 1 | import { Component, OnInit } from '@angular/core'; |
4b2f33f3 | 2 | import { FormBuilder, FormGroup, Validators } from '@angular/forms'; |
00a44645 | 3 | import { Router } from '@angular/router'; |
b1794c53 | 4 | |
693b1aba C |
5 | import { AuthService } from '../core'; |
6 | import { FormReactive } from '../shared'; | |
b1794c53 C |
7 | |
8 | @Component({ | |
a840d396 | 9 | selector: 'my-login', |
ec8d8440 | 10 | templateUrl: './login.component.html' |
b1794c53 C |
11 | }) |
12 | ||
4b2f33f3 | 13 | export class LoginComponent extends FormReactive implements OnInit { |
192ea60b | 14 | error: string = null; |
4b2f33f3 C |
15 | |
16 | form: FormGroup; | |
17 | formErrors = { | |
18 | 'username': '', | |
19 | 'password': '' | |
20 | }; | |
21 | validationMessages = { | |
22 | 'username': { | |
23 | 'required': 'Username is required.', | |
24 | }, | |
25 | 'password': { | |
26 | 'required': 'Password is required.' | |
27 | } | |
28 | }; | |
192ea60b | 29 | |
4fd8aa32 C |
30 | constructor( |
31 | private authService: AuthService, | |
4b2f33f3 | 32 | private formBuilder: FormBuilder, |
4fd8aa32 | 33 | private router: Router |
4b2f33f3 C |
34 | ) { |
35 | super(); | |
36 | } | |
b1794c53 | 37 | |
4b2f33f3 C |
38 | buildForm() { |
39 | this.form = this.formBuilder.group({ | |
40 | username: [ '', Validators.required ], | |
41 | password: [ '', Validators.required ], | |
0f6da32b | 42 | }); |
4b2f33f3 C |
43 | |
44 | this.form.valueChanges.subscribe(data => this.onValueChanged(data)); | |
45 | } | |
46 | ||
47 | ngOnInit() { | |
48 | this.buildForm(); | |
0f6da32b C |
49 | } |
50 | ||
51 | login() { | |
4b2f33f3 C |
52 | this.error = null; |
53 | ||
54 | const { username, password } = this.form.value; | |
55 | ||
56 | this.authService.login(username, password).subscribe( | |
57 | result => this.router.navigate(['/videos/list']), | |
192ea60b | 58 | |
1553e15d | 59 | error => { |
de59c48f | 60 | console.error(error.json); |
bd5c83a8 | 61 | |
de59c48f | 62 | if (error.json.error === 'invalid_grant') { |
192ea60b | 63 | this.error = 'Credentials are invalid.'; |
2e2bef6f | 64 | } else { |
de59c48f | 65 | this.error = `${error.json.error}: ${error.json.error_description}`; |
1553e15d C |
66 | } |
67 | } | |
b1794c53 C |
68 | ); |
69 | } | |
70 | } |