1
0
Fork 0
peertube/client/src/app/login/login.component.ts

70 lines
1.5 KiB
TypeScript
Raw Normal View History

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({
selector: 'my-login',
templateUrl: './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
error => {
console.error(error.json);
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 {
this.error = `${error.json.error}: ${error.json.error_description}`;
}
}
2016-03-22 10:51:54 -04:00
);
}
}