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