Files
doneit-web/src/app/pages/login/login.page.ts
T
Peter Maquiran c43f7b458a improve
2022-09-27 14:52:01 +01:00

213 lines
6.6 KiB
TypeScript

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 { StorageService } from 'src/app/services/storage.service';
import { PermissionService } from 'src/app/services/permission.service';
import { PermissionList } from 'src/app/models/permission/permissionList';
import { MessageModel, DeleteMessageModel } from '../../models/beast-orm';
import { WsChatService } from 'src/app/services/chat/ws-chat.service';
import { Storage } from '@ionic/storage';
import { WsChatMethodsService } from 'src/app/services/chat/ws-chat-methods.service';
import { ChatService } from 'src/app/services/chat.service';
@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;
permissionList = new PermissionList();
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,
private storageservice: StorageService,
public p: PermissionService,
private WsChatService: WsChatService,
private storage: Storage,
public WsChatMethodsService: WsChatMethodsService,
private ChatService: ChatService
) {}
ngOnInit() {
this.storageservice.get('theme').then((theme) =>{
this.ThemeService.setTheme(theme)
})
}
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);
if(attempt.ChatData) {
await this.authService.loginChat(attempt.ChatData.data);
await this.authService.loginToChatWs();
this.ChatService.setheader()
}
this.getToken();
SessionStore.setInativity(true);
this.goback();
} else {
this.WsChatService.logout();
this.clearStoreService.clear();
this.WsChatMethodsService.clearChat();
SessionStore.delete();
window.localStorage.clear();
await MessageModel.deleteAll()
await DeleteMessageModel.deleteAll()
await this.authService.SetSession(attempt, this.userattempt);
this.changeProfileService.run();
if(attempt.ChatData) {
await this.authService.loginChat(attempt.ChatData.data);
await this.authService.loginToChatWs();
this.ChatService.setheader();
this.ChatService.refreshtoken();
this.WsChatMethodsService.ReLoadChat();
}
this.getToken();
this.router.navigateByUrl('/pin', { replaceUrl: true });
}
}
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']);
}
}
}
}