Files
doneit-web/src/app/pages/inactivity/inactivity.page.ts
T

174 lines
3.9 KiB
TypeScript
Raw Normal View History

2021-07-26 15:19:03 +01:00
import { Component, OnInit } from '@angular/core';
import { Router } from '@angular/router';
import { AuthService } from 'src/app/services/auth.service';
import { UserForm } from 'src/app/models/user.model';
import { ToastService } from 'src/app/services/toast.service';
import { environment } from 'src/environments/environment';
import { AlertController } from '@ionic/angular';
import { NotificationsService } from 'src/app/services/notifications.service';
2021-08-27 13:39:52 +01:00
import { SessionStore } from 'src/app/store/session.service';
2021-07-26 15:19:03 +01:00
@Component({
selector: 'app-inactivity',
templateUrl: './inactivity.page.html',
styleUrls: ['./inactivity.page.scss'],
})
export class InactivityPage implements OnInit {
username: string = environment.defaultuser;
password: string = environment.defaultuserpwd;
userattempt: UserForm;
code = []
setPin = false
2021-08-27 13:39:52 +01:00
SessionStore = SessionStore
2021-08-27 17:11:05 +01:00
enterWithPassword = false
2021-07-26 15:19:03 +01:00
constructor(
private notificatinsservice: NotificationsService,
private router: Router,
private authService: AuthService,
private toastService: ToastService,
2021-08-27 13:39:52 +01:00
public alertController: AlertController
) {}
2021-07-26 15:19:03 +01:00
2021-08-27 13:39:52 +01:00
ngOnInit() {}
2021-07-26 15:19:03 +01:00
async presentAlert(message: string) {
const alert = await this.alertController.create({
cssClass: 'my-custom-class',
header: 'Mensagem do sistema',
message: message,
buttons: ['OK']
});
await alert.present();
}
2021-08-27 15:21:15 +01:00
//Function to validade the login inputs
validateUsername() {
return (
this.username.trim().length > 0
);
}
validatePassword() {
return (
this.password.trim().length > 0
);
2021-07-26 15:19:03 +01:00
}
async Login() {
2021-08-27 15:21:15 +01:00
if (this.validateUsername()) {
if(this.validatePassword()) {
this.userattempt = {
username: this.username,
password: this.password,
domainName: environment.domain,
BasicAuthKey: ""
}
let attempt = await this.authService.login(this.userattempt, false)
if (attempt) {
// if current attemp is equal to the current user
if (attempt.UserId == SessionStore.user.UserId) {
await this.authService.SetSession(attempt, this.userattempt);
2021-08-31 10:27:00 +01:00
this.authService.loginChat(this.userattempt);
this.getToken();
2021-08-30 11:41:21 +01:00
SessionStore.setInativity(true)
2021-08-27 17:11:05 +01:00
this.goback()
2021-08-27 15:21:15 +01:00
} else {
SessionStore.delete()
2021-08-30 11:07:49 +01:00
window.localStorage.clear();
2021-08-27 15:21:15 +01:00
await this.authService.SetSession(attempt, this.userattempt);
}
2021-08-27 17:11:05 +01:00
this.enterWithPassword = false
2021-08-27 15:21:15 +01:00
}
}
else {
this.toastService.badRequest('Por favor, insira a sua palavra-passe');
}
}
else {
this.toastService.badRequest('Por favor, insira o seu nome de utilizador');
}
}
2021-08-27 09:48:51 +01:00
2021-07-26 15:19:03 +01:00
getToken() {
this.notificatinsservice.getAndpostToken(this.username);
}
setCode(code: string) {
if(this.code.length < 4) {
this.code.push(code)
}
if(this.code.length == 4) {
2021-08-27 13:39:52 +01:00
if(!SessionStore.hasPin) {
2021-08-18 15:57:19 +01:00
// console.log('storePin')
2021-07-26 15:19:03 +01:00
this.storePin()
} else {
2021-08-18 15:57:19 +01:00
// console.log('pinLogin')
2021-07-26 15:19:03 +01:00
this.pinLogin()
}
}
}
clearCode() {
this.code =[]
}
pinLogin() {
const code = this.code.join('')
2021-08-27 13:39:52 +01:00
if( SessionStore.validatePin(code)) {
2021-08-31 10:43:38 +01:00
SessionStore.setInativity(true)
2021-08-27 17:11:05 +01:00
this.goback()
2021-08-31 10:43:38 +01:00
setTimeout(()=>{
this.clearCode()
}, 1000)
2021-07-26 15:19:03 +01:00
} else {
this.toastService.badRequest('Pin incorreto')
this.code = []
}
}
2021-08-27 17:11:05 +01:00
goback() {
const pathName = this.SessionStore.user.UrlBeforeInactivity
2021-08-31 10:27:00 +01:00
if(pathName) {
this.router.navigate([pathName]);
} else {
this.router.navigate(['/home/events']);
}
2021-08-27 17:11:05 +01:00
}
2021-07-26 15:19:03 +01:00
storePin() {
const code = this.code.join('')
2021-08-27 13:39:52 +01:00
SessionStore.setPin(code)
2021-07-26 15:19:03 +01:00
this.router.navigate(['/home/events']);
}
2021-08-27 17:11:05 +01:00
enterWithPasswordButton() {
this.enterWithPassword = true
}
2021-07-26 15:19:03 +01:00
}