2016-08-23 08:37:49 -04:00
|
|
|
import { Component, OnInit } from '@angular/core';
|
|
|
|
import { Validators } from '@angular/common';
|
|
|
|
import { FormControl, FormGroup, REACTIVE_FORM_DIRECTIVES } from '@angular/forms';
|
2016-06-10 11:43:40 -04:00
|
|
|
import { Router } from '@angular/router';
|
2016-03-22 10:51:54 -04:00
|
|
|
|
2016-07-20 10:24:18 -04:00
|
|
|
import { AuthService } from '../shared';
|
2016-03-22 10:51:54 -04:00
|
|
|
|
|
|
|
@Component({
|
2016-06-01 14:36:27 -04:00
|
|
|
selector: 'my-login',
|
2016-08-23 08:37:49 -04:00
|
|
|
template: require('./login.component.html'),
|
|
|
|
directives: [ REACTIVE_FORM_DIRECTIVES ]
|
2016-03-22 10:51:54 -04:00
|
|
|
})
|
|
|
|
|
2016-08-23 08:37:49 -04:00
|
|
|
export class LoginComponent implements OnInit {
|
2016-06-04 07:31:23 -04:00
|
|
|
error: string = null;
|
2016-08-23 08:37:49 -04:00
|
|
|
username = '';
|
|
|
|
password: '';
|
|
|
|
loginForm: FormGroup;
|
2016-06-04 07:31:23 -04:00
|
|
|
|
2016-05-27 11:49:18 -04:00
|
|
|
constructor(
|
|
|
|
private authService: AuthService,
|
|
|
|
private router: Router
|
|
|
|
) {}
|
2016-03-22 10:51:54 -04:00
|
|
|
|
2016-08-23 08:37:49 -04:00
|
|
|
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(
|
2016-03-22 10:51:54 -04:00
|
|
|
result => {
|
2016-06-04 07:31:23 -04:00
|
|
|
this.error = null;
|
|
|
|
|
2016-06-10 11:43:40 -04:00
|
|
|
this.router.navigate(['/videos/list']);
|
2016-03-22 10:51:54 -04:00
|
|
|
},
|
2016-04-14 16:07:46 -04:00
|
|
|
error => {
|
2016-07-20 10:24:18 -04:00
|
|
|
console.error(error);
|
|
|
|
|
2016-04-14 16:07:46 -04:00
|
|
|
if (error.error === 'invalid_grant') {
|
2016-06-04 07:31:23 -04:00
|
|
|
this.error = 'Credentials are invalid.';
|
2016-04-14 16:12:03 -04:00
|
|
|
} else {
|
2016-06-04 07:31:23 -04:00
|
|
|
this.error = `${error.error}: ${error.error_description}`;
|
2016-04-14 16:07:46 -04:00
|
|
|
}
|
|
|
|
}
|
2016-03-22 10:51:54 -04:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|