2016-08-23 08:37:49 -04:00
|
|
|
import { Component, OnInit } from '@angular/core';
|
2016-09-09 16:16:51 -04:00
|
|
|
import { FormBuilder, FormGroup, Validators } 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-09-09 16:16:51 -04:00
|
|
|
import { AuthService, FormReactive } from '../shared';
|
2016-03-22 10:51:54 -04:00
|
|
|
|
|
|
|
@Component({
|
2016-06-01 14:36:27 -04:00
|
|
|
selector: 'my-login',
|
2016-09-06 16:40:57 -04:00
|
|
|
template: require('./login.component.html')
|
2016-03-22 10:51:54 -04:00
|
|
|
})
|
|
|
|
|
2016-09-09 16:16:51 -04:00
|
|
|
export class LoginComponent extends FormReactive implements OnInit {
|
2016-06-04 07:31:23 -04:00
|
|
|
error: string = null;
|
2016-09-09 16:16:51 -04:00
|
|
|
|
|
|
|
form: FormGroup;
|
|
|
|
formErrors = {
|
|
|
|
'username': '',
|
|
|
|
'password': ''
|
|
|
|
};
|
|
|
|
validationMessages = {
|
|
|
|
'username': {
|
|
|
|
'required': 'Username is required.',
|
|
|
|
},
|
|
|
|
'password': {
|
|
|
|
'required': 'Password is required.'
|
|
|
|
}
|
|
|
|
};
|
2016-06-04 07:31:23 -04:00
|
|
|
|
2016-05-27 11:49:18 -04:00
|
|
|
constructor(
|
|
|
|
private authService: AuthService,
|
2016-09-09 16:16:51 -04:00
|
|
|
private formBuilder: FormBuilder,
|
2016-05-27 11:49:18 -04:00
|
|
|
private router: Router
|
2016-09-09 16:16:51 -04:00
|
|
|
) {
|
|
|
|
super();
|
|
|
|
}
|
2016-03-22 10:51:54 -04:00
|
|
|
|
2016-09-09 16:16:51 -04:00
|
|
|
buildForm() {
|
|
|
|
this.form = this.formBuilder.group({
|
|
|
|
username: [ '', Validators.required ],
|
|
|
|
password: [ '', Validators.required ],
|
2016-08-23 08:37:49 -04:00
|
|
|
});
|
2016-09-09 16:16:51 -04:00
|
|
|
|
|
|
|
this.form.valueChanges.subscribe(data => this.onValueChanged(data));
|
|
|
|
}
|
|
|
|
|
|
|
|
ngOnInit() {
|
|
|
|
this.buildForm();
|
2016-08-23 08:37:49 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
login() {
|
2016-09-09 16:16:51 -04:00
|
|
|
this.error = null;
|
|
|
|
|
|
|
|
const { username, password } = this.form.value;
|
|
|
|
|
|
|
|
this.authService.login(username, password).subscribe(
|
|
|
|
result => this.router.navigate(['/videos/list']),
|
2016-06-04 07:31:23 -04:00
|
|
|
|
2016-04-14 16:07:46 -04:00
|
|
|
error => {
|
2016-08-23 10:54:21 -04:00
|
|
|
console.error(error.json);
|
2016-07-20 10:24:18 -04:00
|
|
|
|
2016-08-23 10:54:21 -04:00
|
|
|
if (error.json.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-08-23 10:54:21 -04:00
|
|
|
this.error = `${error.json.error}: ${error.json.error_description}`;
|
2016-04-14 16:07:46 -04:00
|
|
|
}
|
|
|
|
}
|
2016-03-22 10:51:54 -04:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|