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

216 lines
4.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,
2021-07-26 15:19:03 +01:00
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-09-02 10:12:18 +01:00
loop = false
ngOnInit() {
// window.addEventListener('resize', (event) => {
// if(this.router.url != '/login') return false
// if(this.loop == false) {
// this.loop = true
// this.runloop()
// }
// }, true);
}
runloop() {
// const containerHeight = 651
// let circleHeight = document.querySelector('.circle')['offsetHeight']
// let circleWidth = document.querySelector('.circle')['offsetWidth']
// console.log(window.innerHeight, ' < ', containerHeight)
// console.log(circleHeight)
// document.querySelectorAll('.circle').forEach(e=>{
// e['style']['height'] = (circleHeight -1 )+'px'
// e['style']['width'] = (circleWidth -1 )+'px'
// })
// if( window.innerHeight< containerHeight) {
// setTimeout(()=>{
// this.runloop()
// }, 100)
// } else {
// this.loop = false
// }
}
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: ""
}
2021-09-02 12:17:14 +01:00
let attempt = await this.authService.login(this.userattempt, {saveSession: false})
2021-08-27 15:21:15 +01:00
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 {
2021-11-09 15:37:59 +01:00
this.toastService._badRequest('Por favor, insira a sua palavra-passe');
2021-08-27 15:21:15 +01:00
}
}
else {
2021-11-09 15:37:59 +01:00
this.toastService._badRequest('Por favor, insira o seu nome de utilizador');
2021-08-27 15:21:15 +01:00
}
}
2021-08-27 09:48:51 +01:00
2021-07-26 15:19:03 +01:00
getToken() {
this.notificatinsservice.requestPermissions();
this.notificatinsservice.registrationError();
this.notificatinsservice.getAndpostToken(this.username);
2021-07-26 15:19:03 +01:00
}
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 {
2021-11-09 15:37:59 +01:00
this.toastService._badRequest('Pin incorreto')
2021-07-26 15:19:03 +01:00
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
}