2016-05-13 08:18:37 -04:00
|
|
|
import { Component } from '@angular/core';
|
2016-06-10 11:43:40 -04:00
|
|
|
import { Router } from '@angular/router';
|
2016-03-22 10:51:54 -04:00
|
|
|
|
2016-06-03 16:08:03 -04:00
|
|
|
import { AuthService, AuthStatus, User } from '../shared';
|
2016-03-22 10:51:54 -04:00
|
|
|
|
|
|
|
@Component({
|
2016-06-01 14:36:27 -04:00
|
|
|
selector: 'my-login',
|
2016-06-03 16:08:03 -04:00
|
|
|
template: require('./login.component.html')
|
2016-03-22 10:51:54 -04:00
|
|
|
})
|
|
|
|
|
2016-06-01 14:36:27 -04:00
|
|
|
export class LoginComponent {
|
2016-06-04 07:31:23 -04:00
|
|
|
error: string = null;
|
|
|
|
|
2016-05-27 11:49:18 -04:00
|
|
|
constructor(
|
|
|
|
private authService: AuthService,
|
|
|
|
private router: Router
|
|
|
|
) {}
|
2016-03-22 10:51:54 -04:00
|
|
|
|
|
|
|
login(username: string, password: string) {
|
2016-05-27 11:25:52 -04:00
|
|
|
this.authService.login(username, password).subscribe(
|
2016-03-22 10:51:54 -04:00
|
|
|
result => {
|
2016-06-04 07:31:23 -04:00
|
|
|
this.error = null;
|
|
|
|
|
2016-04-14 16:07:46 -04:00
|
|
|
const user = new User(username, result);
|
|
|
|
user.save();
|
2016-03-22 10:51:54 -04:00
|
|
|
|
2016-05-27 11:25:52 -04:00
|
|
|
this.authService.setStatus(AuthStatus.LoggedIn);
|
2016-03-22 10:51:54 -04:00
|
|
|
|
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 => {
|
|
|
|
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
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|