opt-cc/static/src/app/login/login.component.ts

48 lines
1.3 KiB
TypeScript

import {Component, OnInit} from '@angular/core';
import {Router} from '@angular/router';
import {LoginService} from '../services/app-user-service/login-service';
import {RouteConfig} from '../app.config';
import {SnackBarService} from '../services/user-interface/snack-bar/snack-bar.service';
@Component({
moduleId: module.id,
templateUrl: 'login.component.html',
styleUrls: ['login.component.scss']
})
export class LoginComponent implements OnInit {
loading = false;
returnUrl: string;
constructor(private router: Router,
private loginService: LoginService,
private snackBarService: SnackBarService) {
}
ngOnInit() {
// reset login status
this.loginService.logout();
// redirect on success
this.returnUrl = RouteConfig.overviewPath;
}
login(username: string, password: string) {
if (username.length > 0 && password.length > 0) {
this.loading = true;
this.loginService.login(username, password)
.subscribe(
data => {
this.router.navigate([this.returnUrl]);
},
error => {
// TODO: return i18n key from backend to use translate error method
this.snackBarService.showUntranslatedError(error.error, 15000);
this.loading = false;
});
}
}
}