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-05-10 14:53:46 +01:00
|
|
|
import { User, 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-06-08 15:59:06 +01:00
|
|
|
import { AlertController, AnimationController, ModalController } from '@ionic/angular';
|
2020-10-30 15:22:35 +01:00
|
|
|
import { StorageService } from 'src/app/services/storage.service';
|
|
|
|
|
import { AuthConnstants } from 'src/app/config/auth-constants';
|
2020-12-11 18:00:38 +01:00
|
|
|
import { PhotoService } from 'src/app/services/photo.service';
|
2021-02-01 13:31:33 +01:00
|
|
|
import { NotificationsService } from 'src/app/services/notifications.service';
|
|
|
|
|
import { HttpClient, HttpHeaders, HttpParams } from '@angular/common/http';
|
2021-05-28 14:41:56 +01:00
|
|
|
import crypto from 'crypto-js'
|
2021-06-08 15:59:06 +01:00
|
|
|
import { SuccessMessagePage } from 'src/app/shared/popover/success-message/success-message.page';
|
|
|
|
|
import { BadRequestPage } from 'src/app/shared/popover/bad-request/bad-request.page';
|
2021-05-28 14:41:56 +01:00
|
|
|
|
2021-04-20 13:51:51 +01:00
|
|
|
//import { Token } from '../../models/token.model';
|
2021-04-16 15:53:24 +01:00
|
|
|
//import { FCM } from 'cordova-plugin-fcm-with-dependecy-updated/ionic/ngx';
|
2020-08-21 00:22:51 +01:00
|
|
|
|
2021-02-23 16:10:14 +01:00
|
|
|
/* import { Plugins, PushNotification, PushNotificationToken, PushNotificationActionPerformed } from '@capacitor/core';
|
2021-02-10 11:31:07 +01:00
|
|
|
const { PushNotifications } = Plugins;
|
2021-05-28 14:41:56 +01:00
|
|
|
*/
|
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;
|
2020-08-24 12:55:10 +01:00
|
|
|
username: string = environment.defaultuser;
|
|
|
|
|
password: string = environment.defaultuserpwd;
|
2021-05-10 14:53:46 +01:00
|
|
|
userattempt: UserForm;
|
2021-05-28 14:41:56 +01:00
|
|
|
code = []
|
|
|
|
|
|
2021-06-09 15:59:26 +01:00
|
|
|
setPint = false
|
|
|
|
|
pin = false
|
2020-08-06 14:31:07 +01:00
|
|
|
|
2021-06-01 11:47:09 +01:00
|
|
|
userLoginPreference: any
|
|
|
|
|
|
2020-08-11 04:11:42 +01:00
|
|
|
constructor(
|
2021-02-01 13:31:33 +01:00
|
|
|
private http: HttpClient,
|
|
|
|
|
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-10-30 15:22:35 +01:00
|
|
|
private storageService: StorageService,
|
2020-08-25 10:37:41 +01:00
|
|
|
private toastService: ToastService,
|
2020-12-11 18:00:38 +01:00
|
|
|
private photoService: PhotoService,
|
2021-04-09 09:48:14 +01:00
|
|
|
public alertController: AlertController,
|
2021-06-08 15:59:06 +01:00
|
|
|
private modalController: ModalController,
|
|
|
|
|
private animationController: AnimationController,
|
2021-06-15 15:09:20 +01:00
|
|
|
|
2021-04-16 15:53:24 +01:00
|
|
|
//private fcm: FCM
|
2021-06-01 11:47:09 +01:00
|
|
|
) {
|
|
|
|
|
|
|
|
|
|
let userData = JSON.parse(localStorage.getItem('UserData')) || {}
|
|
|
|
|
|
|
|
|
|
if (userData.hasOwnProperty('loginPreference')) {
|
|
|
|
|
this.userLoginPreference = userData.loginPreference
|
2021-06-09 15:59:26 +01:00
|
|
|
this.pin = true
|
|
|
|
|
|
2021-06-01 11:47:09 +01:00
|
|
|
} else {
|
2021-06-09 16:05:00 +01:00
|
|
|
|
2021-06-01 11:47:09 +01:00
|
|
|
this.userLoginPreference = ''
|
2021-06-09 16:34:14 +01:00
|
|
|
this.setPint = false
|
2021-06-09 15:59:26 +01:00
|
|
|
this.pin = false
|
|
|
|
|
|
2021-06-01 11:47:09 +01:00
|
|
|
}
|
2021-06-09 15:59:26 +01:00
|
|
|
|
2021-06-01 11:47:09 +01:00
|
|
|
}
|
2020-08-05 15:39:16 +01:00
|
|
|
|
2021-05-31 14:21:19 +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-06-10 14:00:44 +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-06-04 11:37:56 +01:00
|
|
|
/* loginRocketChat() {
|
2021-02-01 13:31:33 +01:00
|
|
|
|
2021-01-26 11:03:00 +01:00
|
|
|
let postData = {
|
2021-02-08 15:16:29 +01:00
|
|
|
"user": this.username,
|
|
|
|
|
"password": this.password,
|
2021-01-26 11:03:00 +01:00
|
|
|
}
|
2021-05-28 14:41:56 +01:00
|
|
|
|
2021-02-01 13:31:33 +01:00
|
|
|
this.authService.loginChat(postData).subscribe((res: any) => {
|
2021-01-27 16:01:49 +01:00
|
|
|
console.log(res.data);
|
2021-02-25 12:41:29 +01:00
|
|
|
this.storageService.store(AuthConnstants.AUTH, res.data);
|
2020-12-16 15:07:28 +01:00
|
|
|
console.log('Login to Rocket chat OK');
|
2021-02-01 13:31:33 +01:00
|
|
|
}, (error: any) => {
|
2020-12-16 15:07:28 +01:00
|
|
|
console.log('Network error');
|
2021-02-01 13:31:33 +01:00
|
|
|
this.presentAlert('Network error ' + error);
|
2020-12-16 15:07:28 +01:00
|
|
|
});
|
2021-06-04 11:37:56 +01:00
|
|
|
} */
|
2021-01-22 16:03:05 +01:00
|
|
|
|
2021-04-09 09:48:14 +01:00
|
|
|
getToken() {
|
2021-06-25 09:35:50 +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()) {
|
|
|
|
|
if(this.validatePassword()){
|
|
|
|
|
this.userattempt = {
|
|
|
|
|
username: this.username,
|
|
|
|
|
password: this.password,
|
|
|
|
|
domainName: environment.domain,
|
|
|
|
|
BasicAuthKey: ""
|
2021-05-31 14:21:19 +01:00
|
|
|
}
|
2021-06-09 14:00:14 +01:00
|
|
|
let attempt = await this.authService.login(this.userattempt);
|
|
|
|
|
console.log(JSON.stringify(attempt));
|
|
|
|
|
|
|
|
|
|
if (attempt) {
|
2021-06-09 13:34:55 +01:00
|
|
|
//this.loginRocketChat();
|
|
|
|
|
this.authService.loginChat(this.userattempt);
|
2021-06-25 09:35:50 +01:00
|
|
|
this.getToken();
|
2021-06-09 13:34:55 +01:00
|
|
|
|
2021-06-09 16:05:00 +01:00
|
|
|
if(!this.pin) {
|
2021-06-09 13:34:55 +01:00
|
|
|
this.setPint = true
|
|
|
|
|
this.pin = true
|
|
|
|
|
} else {
|
|
|
|
|
this.router.navigate(['/home/events']);
|
|
|
|
|
}
|
2021-05-31 14:21:19 +01:00
|
|
|
|
2021-06-09 13:34:55 +01:00
|
|
|
}
|
|
|
|
|
else {
|
2021-06-09 15:13:56 +01:00
|
|
|
this.toastService.badRequest('O nome de utilizador e/ou palavra-passe estão incorretas ou verifique a sua conexão com a internet e volte a tentar');
|
2021-06-09 13:34:55 +01:00
|
|
|
}
|
2021-01-22 16:03:05 +01:00
|
|
|
}
|
2021-06-09 13:34:55 +01:00
|
|
|
else{
|
|
|
|
|
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
|
|
|
|
|
|
|
|
setCode(code: string) {
|
2021-06-01 11:47:09 +01:00
|
|
|
|
|
|
|
|
|
2021-05-28 14:41:56 +01:00
|
|
|
if(this.code.length < 4) {
|
|
|
|
|
this.code.push(code)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(this.code.length == 4) {
|
2021-05-31 14:21:19 +01:00
|
|
|
|
|
|
|
|
const code = this.code.join('')
|
|
|
|
|
const encrypted = crypto.SHA1(code)
|
|
|
|
|
|
|
|
|
|
if(this.setPint) {
|
|
|
|
|
this.storePin()
|
|
|
|
|
} else {
|
|
|
|
|
this.pinLogin()
|
|
|
|
|
}
|
2021-05-28 14:41:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
clearCode() {
|
|
|
|
|
this.code =[]
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pinLogin() {
|
|
|
|
|
|
|
|
|
|
const code = this.code.join('')
|
|
|
|
|
const encrypted = crypto.SHA1(code)
|
|
|
|
|
|
|
|
|
|
if( encrypted == localStorage.getItem('PIN')) {
|
2021-06-15 15:09:20 +01:00
|
|
|
//this.toastService.successMessage()
|
2021-05-28 14:47:58 +01:00
|
|
|
this.router.navigate(['/home/events']);
|
2021-05-28 14:41:56 +01:00
|
|
|
} else {
|
2021-06-15 15:09:20 +01:00
|
|
|
this.toastService.badRequest('Pin incorreto')
|
2021-05-28 14:41:56 +01:00
|
|
|
this.code = []
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-31 14:21:19 +01:00
|
|
|
storePin() {
|
2021-06-01 10:42:14 +01:00
|
|
|
|
2021-05-31 14:21:19 +01:00
|
|
|
const code = this.code.join('')
|
|
|
|
|
const encrypted = crypto.SHA1(code)
|
|
|
|
|
|
|
|
|
|
localStorage.setItem('PIN', encrypted)
|
|
|
|
|
|
|
|
|
|
this.router.navigate(['/home/events']);
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-08 15:59:06 +01:00
|
|
|
|
2021-05-28 14:41:56 +01:00
|
|
|
|
2021-06-09 16:34:14 +01:00
|
|
|
async badRequest(message?: string, callback?) {
|
2021-06-08 15:59:06 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
const enterAnimation = (baseEl: any) => {
|
|
|
|
|
const backdropAnimation = this.animationController.create()
|
|
|
|
|
.addElement(baseEl.querySelector('ion-backdrop')!)
|
|
|
|
|
.fromTo('opacity', '0.01', 'var(--backdrop-opacity)');
|
|
|
|
|
|
|
|
|
|
const wrapperAnimation = this.animationController.create()
|
|
|
|
|
.addElement(baseEl.querySelector('.modal-wrapper')!)
|
|
|
|
|
.keyframes([
|
|
|
|
|
{ offset: 0, opacity: '1', right: '-100%' },
|
|
|
|
|
{ offset: 1, opacity: '1', right: '0px' }
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
return this.animationController.create()
|
|
|
|
|
.addElement(baseEl)
|
|
|
|
|
.easing('ease-out')
|
|
|
|
|
.duration(500)
|
|
|
|
|
.addAnimation([backdropAnimation, wrapperAnimation]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const leaveAnimation = (baseEl: any) => {
|
|
|
|
|
return enterAnimation(baseEl).direction('reverse');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2021-05-28 14:41:56 +01:00
|
|
|
const modal = await this.modalController.create({
|
2021-06-08 15:59:06 +01:00
|
|
|
enterAnimation,
|
|
|
|
|
leaveAnimation,
|
|
|
|
|
component: BadRequestPage,
|
2021-05-28 14:41:56 +01:00
|
|
|
componentProps: {
|
|
|
|
|
message: message || 'Processo não efetuado' ,
|
|
|
|
|
},
|
2021-06-08 15:59:06 +01:00
|
|
|
cssClass: 'notification-modal'
|
2021-05-28 14:41:56 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
modal.present()
|
|
|
|
|
|
2021-06-08 15:59:06 +01:00
|
|
|
setTimeout(()=> {
|
2021-05-28 14:41:56 +01:00
|
|
|
modal.dismiss()
|
|
|
|
|
},3000)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|