72 lines
1.9 KiB
TypeScript
72 lines
1.9 KiB
TypeScript
import { Component, OnInit } from '@angular/core';
|
|
import { ApiService } from '../api.service';
|
|
|
|
@Component({
|
|
selector: 'app-login',
|
|
templateUrl: './login.component.html',
|
|
styleUrls: ['./login.component.scss']
|
|
})
|
|
export class LoginComponent implements OnInit {
|
|
username = '';
|
|
password = '';
|
|
error: string = null;
|
|
|
|
constructor(private apiService: ApiService) { }
|
|
|
|
ngOnInit(): void {}
|
|
|
|
login(event): void
|
|
{
|
|
event.target.disabled = true;
|
|
event.target.style.visibility = 'hidden';
|
|
|
|
this.apiService.getAuthToken(this.username, this.password).toPromise()
|
|
.then(
|
|
(response) => {
|
|
this.apiService.storeData('token', response.token);
|
|
|
|
this.apiService.getChatToken(response.token).toPromise()
|
|
.then(
|
|
(chatTokenResponse) => {
|
|
this.apiService.storeData('chatToken', chatTokenResponse.token);
|
|
this.apiService.storeData('userId', chatTokenResponse.userId.toString());
|
|
|
|
window.location.reload();
|
|
}
|
|
).catch(
|
|
(error) => {
|
|
this.apiService.storeData('token', null);
|
|
this.error = error.name + ': ' + error.message;
|
|
|
|
event.target.disabled = false;
|
|
event.target.style.visibility = 'visible';
|
|
}
|
|
);
|
|
}
|
|
).catch (
|
|
(error) => {
|
|
this.apiService.storeData('token', null);
|
|
|
|
switch (error.status) {
|
|
case 401:
|
|
this.error = 'Login fehlgeschlagen: Die Zugangsdaten sind falsch!';
|
|
break;
|
|
|
|
case 0:
|
|
this.error = 'Verbindungsfehler: Konnte keine Verbindung zum Server herstellen!';
|
|
break;
|
|
|
|
default:
|
|
this.error = 'Fehler: Etwas unfassbar schlimmes ist passiert!';
|
|
break;
|
|
}
|
|
|
|
console.log(error);
|
|
|
|
event.target.disabled = false;
|
|
event.target.style.visibility = 'visible';
|
|
}
|
|
);
|
|
}
|
|
}
|