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'; import crypto from 'crypto-js' import { LocalstoreService } from 'src/app/store/localstore.service'; @Component({ selector: 'app-login', templateUrl: './login.page.html', styleUrls: ['./login.page.scss'], }) export class LoginPage implements OnInit { logstatus: boolean; username: string = environment.defaultuser; password: string = environment.defaultuserpwd; userattempt: UserForm; code = [] hasPin: boolean loginPreference: string hasSession = false setPin = false constructor( private notificatinsservice: NotificationsService, private router: Router, private authService: AuthService, private toastService: ToastService, public alertController: AlertController, private localstoreService: LocalstoreService ) { } ngOnInit() { // clear local storage window.localStorage.clear(); // App has session if(!this.localstoreService.get('UserData', false)) { this.hasSession = false } else { this.hasSession = true // this.router.navigate(['/home/events']); } let userData = this.localstoreService.get('UserData', {}) const loginPreference = userData?.loginPreference const pin = userData?.PIN if (pin) { this.hasPin = true } else { this.hasPin = false } if (loginPreference) { this.loginPreference = loginPreference } else { this.loginPreference = '' } } //Function to validade the login inputs validateUsername() { return ( this.username.trim().length > 0 ); } validatePassword() { return ( this.password.trim().length > 0 ); } 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(); } /* loginRocketChat() { let postData = { "user": this.username, "password": this.password, } this.authService.loginChat(postData).subscribe((res: any) => { console.log(res.data); this.storageService.store(AuthConnstants.AUTH, res.data); console.log('Login to Rocket chat OK'); }, (error: any) => { console.log('Network error'); this.presentAlert('Network error ' + error); }); } */ getToken() { this.notificatinsservice.getAndpostToken(this.username); } async Login() { 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) if (attempt) { this.authService.loginChat(this.userattempt); this.getToken(); this.hasSession = true this.hasPin = false this.setPin = true if(!this.hasPin || this.hasPin) { } else { this.router.navigate(['/home/events']); } } } else { this.toastService.badRequest('Por favor, insira a sua palavra-passe'); } } else { this.toastService.badRequest('Por favor, insira o seu nome de utilizador'); } } setCode(code: string) { if(this.code.length < 4) { this.code.push(code) } if(this.code.length == 4) { const code = this.code.join('') const encrypted = crypto.SHA1(code) if(!this.hasPin) { // alert('storePin') this.storePin() } else { // alert('pinLogin') this.pinLogin() } } } clearCode() { this.code =[] } pinLogin() { const code = this.code.join('') const encrypted = crypto.SHA1(code) let userData = this.localstoreService.get('UserData', {}) const pin = userData?.PIN //if( encrypted == pin) { if( encrypted == this.localstoreService.get('UserData', false)) { //this.toastService.successMessage() this.router.navigate(['/home/events']); } else { this.toastService.badRequest('Pin incorreto') this.code = [] } } storePin() { const code = this.code.join('') const encrypted = crypto.SHA1(code) let userData: Object = this.localstoreService.get('UserData', {}) userData['PIN'] = encrypted userData['loginPreference'] = 'none' this.localstoreService.set('UserData', userData) this.localstoreService.set('PIN', encrypted) this.router.navigate(['/home/events']); } }