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 { SessionStore } from 'src/app/store/session.service'; import { ClearStoreService } from 'src/app/services/clear-store.service'; import { ChangeProfileService } from 'src/app/services/change-profile.service'; import { ThemeService } from 'src/app/services/theme.service'; import { PermissionService } from 'src/app/services/permission.service'; import { MessageModel, DeleteMessageModel } from '../../models/beast-orm'; import { RochetChatConnectorService } from 'src/app/services/chat/rochet-chat-connector.service'; import { ChatSystemService } from 'src/app/services/chat/chat-system.service'; import { ChatService } from 'src/app/services/chat.service'; import { Platform } from '@ionic/angular'; import { FirstEnterService } from '../../services/first-enter.service'; import { Storage } from '@ionic/storage'; @Component({ selector: 'app-login', templateUrl: './login.page.html', styleUrls: ['./login.page.scss'], }) export class LoginPage implements OnInit { logstatus: boolean; username: string = SessionStore.user.Email || environment.defaultuser; password: string = environment.defaultuserpwd; userattempt: UserForm; code = [] hasPin: boolean loginPreference: string sessionStore = SessionStore; showPassword = false; passwordIcon = "eye"; constructor( private notificatinsservice: NotificationsService, private router: Router, private authService: AuthService, private toastService: ToastService, public alertController: AlertController, private clearStoreService: ClearStoreService, private changeProfileService: ChangeProfileService, public ThemeService: ThemeService, public p: PermissionService, private RochetChatConnectorService: RochetChatConnectorService, public ChatSystemService: ChatSystemService, private ChatService: ChatService, private platform: Platform, private FirstEnterService: FirstEnterService, private storage:Storage, ) {} ngOnInit() {} togglePassword() { this.showPassword = !this.showPassword; if(this.passwordIcon == "eye") { this.passwordIcon = "eye-off"; } else { this.passwordIcon = "eye"; } } //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(); } getToken() { this.notificatinsservice.requestPermissions(); this.notificatinsservice.registrationError(); this.notificatinsservice.getAndpostToken(this.username); } async Login() { if (this.validateUsername()) { if(this.validatePassword()) { this.userattempt = { username: this.username.trim(), password: this.password.trim(), domainName: environment.domain, BasicAuthKey: "" } const loader = this.toastService.loading() let attempt = await this.authService.login(this.userattempt, {saveSession: false}) loader.remove() if (attempt) { if (attempt.UserId == SessionStore.user.UserId) { await this.authService.SetSession(attempt, this.userattempt); this.changeProfileService.run(); if(attempt.ChatData) { await this.authService.loginToChatWs(); this.ChatService.setheader() this.ChatSystemService.loadChat(); } this.changeProfileService.runLogin(); this.getToken(); SessionStore.setInativity(true); this.goback(); } else { this.RochetChatConnectorService.logout(); this.clearStoreService.clear(); this.ChatSystemService.clearChat(); SessionStore.delete(); window.localStorage.clear(); await MessageModel.deleteAll(); await DeleteMessageModel.deleteAll(); this.storage.clear(); await this.authService.SetSession(attempt, this.userattempt); this.changeProfileService.run(); if(attempt.ChatData) { await this.authService.loginToChatWs(); this.ChatService.setheader(); this.ChatSystemService.loadChat(); } this.getToken(); if(!this.platform.is('desktop') && !this.platform.is('mobileweb')) { if(this.sessionStore.hasPin) { this.router.navigateByUrl('/home/events'); } else { this.router.navigateByUrl('/pin', { replaceUrl: true }); } } else { this.router.navigate(['/home/events']); } } } else{ this.toastService._badRequest('Ocorreu um problema por favor valide o username e password'); } } else { this.toastService._badRequest('Por favor, insira a sua palavra-passe'); } } else { this.toastService._badRequest('Por favor, insira o seu nome de utilizador'); } } goback() { const pathName = SessionStore.user.UrlBeforeInactivity if(pathName) { this.router.navigate([pathName]); } else { if(this.p.userPermission(this.p.permissionList.Agenda.access) || this.p.userPermission(this.p.permissionList.Gabinete.access)){ //When user has got access to Agenda but does not have their own calendar, goes to Agenda if(this.p.userPermission(this.p.permissionList.Agenda.access) && SessionStore.user.OwnerCalendars.length == 0){ this.router.navigate(['/home/agenda']); } else{ this.router.navigate(['/home/events']); } } //If user has access permission to both Chat and Action, goes to Chat by default. else if((this.p.userPermission(this.p.permissionList.Chat.access) && this.p.userPermission(this.p.permissionList.Actions.access)) || this.p.userPermission(this.p.permissionList.Chat.access)){ this.router.navigate(['/home/chat']); } else if(this.p.userPermission(this.p.permissionList.Actions.access)){ this.router.navigate(['/home/publications']); } } } }