definma-ui/src/app/login/login.component.ts

63 lines
1.7 KiB
TypeScript
Raw Normal View History

import {Component, OnInit, ViewChild} from '@angular/core';
import {ValidationService} from '../services/validation.service';
import {LoginService} from '../services/login.service';
2020-07-13 10:52:10 +02:00
import {Router} from '@angular/router';
2020-07-29 13:14:29 +02:00
import {ApiService} from '../services/api.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
username = ''; // credentials
password = '';
2020-07-29 13:14:29 +02:00
email = '';
message = ''; // message below login fields
2020-09-03 15:51:53 +02:00
passreset = false; // to toggle between normal login and password reset form
2020-07-29 13:14:29 +02:00
@ViewChild('loginForm') loginForm;
2020-05-19 12:49:06 +02:00
2020-05-20 10:07:34 +02:00
constructor(
private validate: ValidationService,
2020-08-26 19:25:31 +02:00
private login: LoginService,
2020-07-29 13:14:29 +02:00
private api: ApiService,
2020-07-13 10:52:10 +02:00
private router: Router
2020-05-20 10:07:34 +02:00
) { }
2020-05-19 12:49:06 +02:00
ngOnInit() {
}
2020-08-26 19:25:31 +02:00
userLogin() {
2020-09-03 15:51:53 +02:00
if (this.passreset) { // reset password
2020-07-29 13:14:29 +02:00
this.api.post('/user/passreset', {name: this.username, email: this.email}, (data, err) => {
if (err) {
this.message = 'Could not find a valid user';
}
else {
this.message = 'Password reset, check your inbox';
}
});
}
else {
2020-08-26 19:25:31 +02:00
this.login.login(this.username, this.password).then(ok => {
2020-07-29 13:14:29 +02:00
if (ok) {
this.message = 'Login successful';
2020-08-26 19:25:31 +02:00
if (this.login.isLevel.read) {
this.router.navigate(['/samples']);
}
2020-09-03 15:51:53 +02:00
else { // navigate prediction users to prediction as they cannot access samples
2020-08-26 19:25:31 +02:00
this.router.navigate(['/prediction']);
}
2020-07-29 13:14:29 +02:00
}
else {
this.message = 'Wrong credentials!';
}
});
}
2020-05-20 10:07:34 +02:00
}
2020-05-19 12:49:06 +02:00
}