]>
Commit | Line | Data |
---|---|---|
1 | import { Component, OnInit } from '@angular/core'; | |
2 | import { FormControl, FormGroup, Validators } from '@angular/forms'; | |
3 | import { Router } from '@angular/router'; | |
4 | ||
5 | import { AuthService } from '../shared'; | |
6 | ||
7 | @Component({ | |
8 | selector: 'my-login', | |
9 | template: require('./login.component.html') | |
10 | }) | |
11 | ||
12 | export class LoginComponent implements OnInit { | |
13 | error: string = null; | |
14 | username = ''; | |
15 | password: ''; | |
16 | loginForm: FormGroup; | |
17 | ||
18 | constructor( | |
19 | private authService: AuthService, | |
20 | private router: Router | |
21 | ) {} | |
22 | ||
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( | |
32 | result => { | |
33 | this.error = null; | |
34 | ||
35 | this.router.navigate(['/videos/list']); | |
36 | }, | |
37 | error => { | |
38 | console.error(error.json); | |
39 | ||
40 | if (error.json.error === 'invalid_grant') { | |
41 | this.error = 'Credentials are invalid.'; | |
42 | } else { | |
43 | this.error = `${error.json.error}: ${error.json.error_description}`; | |
44 | } | |
45 | } | |
46 | ); | |
47 | } | |
48 | } |