]>
Commit | Line | Data |
---|---|---|
0f6da32b C |
1 | import { Component, OnInit } from '@angular/core'; |
2 | import { Validators } from '@angular/common'; | |
3 | import { FormControl, FormGroup, REACTIVE_FORM_DIRECTIVES } from '@angular/forms'; | |
00a44645 | 4 | import { Router } from '@angular/router'; |
b1794c53 | 5 | |
bd5c83a8 | 6 | import { AuthService } from '../shared'; |
b1794c53 C |
7 | |
8 | @Component({ | |
a840d396 | 9 | selector: 'my-login', |
0f6da32b C |
10 | template: require('./login.component.html'), |
11 | directives: [ REACTIVE_FORM_DIRECTIVES ] | |
b1794c53 C |
12 | }) |
13 | ||
0f6da32b | 14 | export class LoginComponent implements OnInit { |
192ea60b | 15 | error: string = null; |
0f6da32b C |
16 | username = ''; |
17 | password: ''; | |
18 | loginForm: FormGroup; | |
192ea60b | 19 | |
4fd8aa32 C |
20 | constructor( |
21 | private authService: AuthService, | |
22 | private router: Router | |
23 | ) {} | |
b1794c53 | 24 | |
0f6da32b C |
25 | ngOnInit() { |
26 | this.loginForm = new FormGroup({ | |
27 | username: new FormControl('', [ <any>Validators.required ]), | |
28 | password: new FormControl('', [ <any>Validators.required ]), | |
29 | }); | |
30 | } | |
31 | ||
32 | login() { | |
33 | this.authService.login(this.username, this.password).subscribe( | |
b1794c53 | 34 | result => { |
192ea60b C |
35 | this.error = null; |
36 | ||
00a44645 | 37 | this.router.navigate(['/videos/list']); |
b1794c53 | 38 | }, |
1553e15d | 39 | error => { |
de59c48f | 40 | console.error(error.json); |
bd5c83a8 | 41 | |
de59c48f | 42 | if (error.json.error === 'invalid_grant') { |
192ea60b | 43 | this.error = 'Credentials are invalid.'; |
2e2bef6f | 44 | } else { |
de59c48f | 45 | this.error = `${error.json.error}: ${error.json.error_description}`; |
1553e15d C |
46 | } |
47 | } | |
b1794c53 C |
48 | ); |
49 | } | |
50 | } |