2020-05-19 12:49:06 +02:00
|
|
|
import { Component, OnInit } from '@angular/core';
|
2020-05-20 10:07:34 +02:00
|
|
|
import {ValidationService} from '../validation.service';
|
|
|
|
import {LoginService} from '../login.service';
|
2020-05-19 12:49:06 +02:00
|
|
|
|
|
|
|
@Component({
|
|
|
|
selector: 'app-login',
|
|
|
|
templateUrl: './login.component.html',
|
|
|
|
styleUrls: ['./login.component.scss']
|
|
|
|
})
|
|
|
|
export class LoginComponent implements OnInit {
|
|
|
|
|
2020-05-20 10:07:34 +02:00
|
|
|
message = ''; // message below login fields
|
|
|
|
username = ''; // credentials
|
|
|
|
password = '';
|
|
|
|
validCredentials = false; // true if entered credentials are valid
|
2020-05-19 12:49:06 +02:00
|
|
|
|
2020-05-20 10:07:34 +02:00
|
|
|
constructor(
|
|
|
|
private validate: ValidationService,
|
|
|
|
private loginService: LoginService
|
|
|
|
) { }
|
2020-05-19 12:49:06 +02:00
|
|
|
|
|
|
|
ngOnInit() {
|
|
|
|
}
|
|
|
|
|
2020-05-20 10:07:34 +02:00
|
|
|
login() {
|
|
|
|
const {ok: userOk, error: userError} = this.validate.username(this.username);
|
|
|
|
const {ok: passwordOk, error: passwordError} = this.validate.password(this.password);
|
|
|
|
this.message = userError + (userError + passwordError === '' ? '' : '\n') + passwordError; // display errors
|
|
|
|
console.log(this.message);
|
|
|
|
if (userOk && passwordOk) {
|
|
|
|
this.loginService.login(this.username, this.password).then(ok => {
|
|
|
|
if (ok) {
|
|
|
|
this.message = 'Login successful'; // TODO: think about following action, write tests!
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
this.message = 'Wrong credentials! Try again.';
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
2020-05-19 12:49:06 +02:00
|
|
|
|
|
|
|
}
|