blob: 997b5dc8ab03b92db2483bf91bbbe527935b617e [file] [log] [blame]
/**
******************************************************************************
* Copyright © 2017-2018 PTA GmbH.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
*
* http://www.eclipse.org/legal/epl-v10.html
*
******************************************************************************
*/
import { Component, OnInit, ViewChildren, AfterViewInit } from '@angular/core';
import { Router } from '@angular/router';
import { AuthenticationService } from '../../services/authentication.service';
import { BaseDataService } from '../../services/base-data.service';
import { SessionContext } from '../../common/session-context';
import { Globals } from '../../common/globals';
import { LoginCredentials } from '../../model/login-credentials';
import { User } from '../../model/user';
@Component({
selector: 'app-login',
templateUrl: './login.component.html',
styleUrls: ['./login.component.css']
})
export class LoginComponent implements OnInit, AfterViewInit {
@ViewChildren('username') usernameInput;
constructor(
private router: Router,
private authService: AuthenticationService,
private baseDataService: BaseDataService,
public sessionContext: SessionContext
) { }
ngOnInit() {
localStorage.removeItem('authenticatedUserId');
}
ngAfterViewInit() {
this.usernameInput.first.nativeElement.focus();
}
private pastLogin() {
this.initBaseData();
this.goToOverview();
}
private initBaseData() {
}
private setError(showErr: boolean) {
if (showErr) {
document.getElementById('error-message').classList.remove('hidden');
} else {
document.getElementById('error-message').classList.add('hidden');
}
}
public login(name: string, pw: string) {
this.setError(false);
const creds = new LoginCredentials();
creds.userName = name;
creds.password = pw;
this.authService.login(creds)
.subscribe(jwtToken => this.onLoggedInSuccessfully(jwtToken),
error => {
console.log(error);
this.setError(true);
});
}
private onLoggedInSuccessfully(jwtToken: any) {
this.sessionContext.setAccessToken(jwtToken.access_token);
this.pastLogin();
}
public goToOverview() {
this.router.navigate(['/overview']);
}
}