aboutsummaryrefslogtreecommitdiffhomepage
path: root/client/src/app/login/login.component.ts
blob: 7a4e15c2c28ff607ebc973d44c9d8dc98f17058e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import { Component, OnInit } from '@angular/core';
import { FormControl, FormGroup, Validators } from '@angular/forms';
import { Router } from '@angular/router';

import { AuthService } from '../shared';

@Component({
  selector: 'my-login',
  template: require('./login.component.html')
})

export class LoginComponent implements OnInit {
  error: string = null;
  username = '';
  password: '';
  loginForm: FormGroup;

  constructor(
    private authService: AuthService,
    private router: Router
  ) {}

  ngOnInit() {
    this.loginForm = new FormGroup({
      username: new FormControl('', [ <any>Validators.required ]),
      password: new FormControl('', [ <any>Validators.required ]),
    });
  }

  login() {
    this.authService.login(this.username, this.password).subscribe(
      result => {
        this.error = null;

        this.router.navigate(['/videos/list']);
      },
      error => {
        console.error(error.json);

        if (error.json.error === 'invalid_grant') {
          this.error = 'Credentials are invalid.';
        } else {
          this.error = `${error.json.error}: ${error.json.error_description}`;
        }
      }
    );
  }
}