2020-08-05 15:39:16 +01:00
|
|
|
import { Component, OnInit } from '@angular/core';
|
|
|
|
|
import { Router } from '@angular/router';
|
2020-08-06 14:31:07 +01:00
|
|
|
import { AuthService } from 'src/app/services/auth.service';
|
2021-07-01 09:52:36 +01:00
|
|
|
import { UserForm } from 'src/app/models/user.model';
|
2020-08-21 10:44:43 +01:00
|
|
|
import { ToastService } from 'src/app/services/toast.service';
|
2020-08-24 12:55:10 +01:00
|
|
|
import { environment } from 'src/environments/environment';
|
2021-07-01 09:52:36 +01:00
|
|
|
import { AlertController } from '@ionic/angular';
|
2021-02-01 13:31:33 +01:00
|
|
|
import { NotificationsService } from 'src/app/services/notifications.service';
|
2021-08-27 13:39:52 +01:00
|
|
|
import { SessionStore } from 'src/app/store/session.service';
|
2020-08-05 15:39:16 +01:00
|
|
|
|
|
|
|
|
@Component({
|
|
|
|
|
selector: 'app-login',
|
|
|
|
|
templateUrl: './login.page.html',
|
|
|
|
|
styleUrls: ['./login.page.scss'],
|
|
|
|
|
})
|
|
|
|
|
export class LoginPage implements OnInit {
|
|
|
|
|
|
2020-08-19 23:47:11 +01:00
|
|
|
logstatus: boolean;
|
2021-09-02 12:17:14 +01:00
|
|
|
username: string = SessionStore.user.Email || environment.defaultuser;
|
2020-08-24 12:55:10 +01:00
|
|
|
password: string = environment.defaultuserpwd;
|
2021-05-10 14:53:46 +01:00
|
|
|
userattempt: UserForm;
|
2021-05-28 14:41:56 +01:00
|
|
|
code = []
|
|
|
|
|
|
2021-07-01 09:52:36 +01:00
|
|
|
hasPin: boolean
|
|
|
|
|
loginPreference: string
|
2021-08-27 13:39:52 +01:00
|
|
|
|
|
|
|
|
sessionStore = SessionStore
|
2020-08-06 14:31:07 +01:00
|
|
|
|
2020-08-11 04:11:42 +01:00
|
|
|
constructor(
|
2021-02-01 13:31:33 +01:00
|
|
|
private notificatinsservice: NotificationsService,
|
2021-01-22 16:03:05 +01:00
|
|
|
private router: Router,
|
2020-08-21 10:44:43 +01:00
|
|
|
private authService: AuthService,
|
2020-08-25 10:37:41 +01:00
|
|
|
private toastService: ToastService,
|
2021-09-02 10:12:18 +01:00
|
|
|
public alertController: AlertController
|
2021-07-22 14:40:29 +01:00
|
|
|
) {
|
2021-07-01 11:26:45 +01:00
|
|
|
}
|
2021-07-01 09:52:36 +01:00
|
|
|
|
2021-09-02 12:17:14 +01:00
|
|
|
ngOnInit() {}
|
2021-05-28 14:41:56 +01:00
|
|
|
|
2020-11-24 13:46:13 +01:00
|
|
|
//Function to validade the login inputs
|
2021-06-09 13:34:55 +01:00
|
|
|
validateUsername() {
|
2020-08-06 14:31:07 +01:00
|
|
|
return (
|
2021-01-22 16:03:05 +01:00
|
|
|
this.username.trim().length > 0
|
2021-06-09 13:34:55 +01:00
|
|
|
);
|
|
|
|
|
}
|
2021-07-22 14:40:29 +01:00
|
|
|
|
2021-06-09 13:34:55 +01:00
|
|
|
validatePassword() {
|
|
|
|
|
return (
|
|
|
|
|
this.password.trim().length > 0
|
2021-01-22 16:03:05 +01:00
|
|
|
);
|
2020-08-07 10:31:33 +01:00
|
|
|
}
|
|
|
|
|
|
2021-01-22 16:03:05 +01:00
|
|
|
async presentAlert(message: string) {
|
2020-08-25 10:37:41 +01:00
|
|
|
const alert = await this.alertController.create({
|
|
|
|
|
cssClass: 'my-custom-class',
|
|
|
|
|
header: 'Mensagem do sistema',
|
|
|
|
|
message: message,
|
|
|
|
|
buttons: ['OK']
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
await alert.present();
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-09 09:48:14 +01:00
|
|
|
getToken() {
|
2021-07-01 09:52:36 +01:00
|
|
|
this.notificatinsservice.getAndpostToken(this.username);
|
2021-04-09 09:48:14 +01:00
|
|
|
}
|
2021-01-22 16:03:05 +01:00
|
|
|
|
|
|
|
|
async Login() {
|
2021-05-31 14:21:19 +01:00
|
|
|
|
2021-06-09 13:34:55 +01:00
|
|
|
if (this.validateUsername()) {
|
2021-08-20 18:02:50 +01:00
|
|
|
if(this.validatePassword()) {
|
2021-07-01 09:52:36 +01:00
|
|
|
|
2021-06-09 13:34:55 +01:00
|
|
|
this.userattempt = {
|
|
|
|
|
username: this.username,
|
|
|
|
|
password: this.password,
|
|
|
|
|
domainName: environment.domain,
|
|
|
|
|
BasicAuthKey: ""
|
2021-05-31 14:21:19 +01:00
|
|
|
}
|
2021-09-02 12:17:14 +01:00
|
|
|
|
|
|
|
|
let attempt = await this.authService.login(this.userattempt, {saveSession: false})
|
2021-07-22 14:40:29 +01:00
|
|
|
|
2021-06-09 14:00:14 +01:00
|
|
|
if (attempt) {
|
2021-09-01 17:14:57 +01:00
|
|
|
|
2021-09-02 13:22:10 +01:00
|
|
|
if (attempt.UserId == SessionStore.user.UserId) {
|
2021-09-02 12:17:14 +01:00
|
|
|
await this.authService.SetSession(attempt, this.userattempt);
|
2021-09-03 17:33:29 +01:00
|
|
|
await this.authService.loginChat(this.userattempt);
|
2021-09-02 12:17:14 +01:00
|
|
|
this.getToken();
|
|
|
|
|
SessionStore.setInativity(true)
|
2021-09-03 12:19:21 +01:00
|
|
|
|
2021-09-02 12:17:14 +01:00
|
|
|
this.goback()
|
|
|
|
|
} else {
|
|
|
|
|
SessionStore.delete()
|
|
|
|
|
window.localStorage.clear();
|
|
|
|
|
await this.authService.SetSession(attempt, this.userattempt);
|
2021-09-03 17:33:29 +01:00
|
|
|
await this.authService.loginChat(this.userattempt);
|
2021-09-02 12:17:14 +01:00
|
|
|
this.router.navigate(['/pin']);
|
|
|
|
|
}
|
2021-08-27 13:39:52 +01:00
|
|
|
|
2021-06-09 13:34:55 +01:00
|
|
|
}
|
2021-01-22 16:03:05 +01:00
|
|
|
}
|
2021-07-01 09:52:36 +01:00
|
|
|
else {
|
2021-06-09 13:34:55 +01:00
|
|
|
this.toastService.badRequest('Por favor, insira a sua palavra-passe');
|
2021-02-25 10:47:13 +01:00
|
|
|
}
|
2021-01-22 16:03:05 +01:00
|
|
|
}
|
|
|
|
|
else {
|
2021-06-09 13:34:55 +01:00
|
|
|
this.toastService.badRequest('Por favor, insira o seu nome de utilizador');
|
2021-01-22 16:03:05 +01:00
|
|
|
}
|
2020-08-05 15:39:16 +01:00
|
|
|
}
|
2021-05-28 14:41:56 +01:00
|
|
|
|
2021-09-02 12:17:14 +01:00
|
|
|
|
|
|
|
|
goback() {
|
|
|
|
|
const pathName = SessionStore.user.UrlBeforeInactivity
|
|
|
|
|
if(pathName) {
|
|
|
|
|
this.router.navigate([pathName]);
|
|
|
|
|
} else {
|
|
|
|
|
this.router.navigate(['/home/events']);
|
|
|
|
|
}
|
2021-09-02 13:22:10 +01:00
|
|
|
|
2021-09-02 12:17:14 +01:00
|
|
|
}
|
|
|
|
|
|
2021-07-22 14:40:29 +01:00
|
|
|
}
|