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

166 lines
5.1 KiB
TypeScript
Raw Normal View History

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-07-01 09:52:36 +01:00
import { UserForm } from 'src/app/models/user.model';
import { ToastService } from 'src/app/services/toast.service';
import { environment } from 'src/environments/environment';
2021-07-01 09:52:36 +01:00
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-09-27 16:23:41 +01:00
import { ClearStoreService } from 'src/app/services/clear-store.service';
2021-09-28 11:31:10 +01:00
import { ChangeProfileService } from 'src/app/services/change-profile.service';
import { ThemeService } from 'src/app/services/theme.service';
2021-11-15 15:29:43 +01:00
import { StorageService } from 'src/app/services/storage.service';
2022-01-11 12:12:45 +01:00
import { ChatService } from 'src/app/services/chat.service';
import { PermissionService } from 'src/app/services/permission.service';
2022-03-30 15:08:23 +01:00
import { PermissionList } from 'src/app/models/permission/permissionList';
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 {
logstatus: boolean;
2021-09-02 12:17:14 +01:00
username: string = SessionStore.user.Email || 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-07-01 09:52:36 +01:00
hasPin: boolean
loginPreference: string
2021-08-27 13:39:52 +01:00
sessionStore = SessionStore;
permissionList = new PermissionList();
2020-08-06 14:31:07 +01:00
constructor(
private notificatinsservice: NotificationsService,
2021-01-22 16:03:05 +01:00
private router: Router,
private authService: AuthService,
private toastService: ToastService,
2021-09-21 06:09:41 +01:00
public alertController: AlertController,
2021-09-28 11:31:10 +01:00
private clearStoreService: ClearStoreService,
2021-10-21 15:47:00 +01:00
private changeProfileService: ChangeProfileService,
2021-10-27 07:11:58 +01:00
public ThemeService: ThemeService,
private storageservice: StorageService,
public permissionService: PermissionService,
2021-10-21 15:47:00 +01:00
) {}
2021-07-01 09:52:36 +01:00
2021-09-21 06:09:41 +01:00
ngOnInit() {
this.storageservice.get('theme').then((theme) =>{
console.log('LOGIN THEME',theme)
2021-10-27 07:11:58 +01:00
this.ThemeService.setTheme(theme)
})
2021-09-21 06:09:41 +01:00
}
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-07-22 14:40:29 +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) {
const alert = await this.alertController.create({
cssClass: 'my-custom-class',
header: 'Mensagem do sistema',
message: message,
buttons: ['OK']
});
await alert.present();
}
2021-04-09 09:48:14 +01:00
getToken() {
this.notificatinsservice.requestPermissions();
this.notificatinsservice.registrationError();
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()) {
2021-08-20 18:02:50 +01:00
if(this.validatePassword()) {
2021-07-01 09:52:36 +01:00
2021-06-09 13:34:55 +01:00
this.userattempt = {
username: this.username,
password: this.password,
domainName: environment.domain,
BasicAuthKey: ""
2021-05-31 14:21:19 +01:00
}
2021-09-27 16:23:41 +01:00
const loader = this.toastService.loading()
2021-09-02 12:17:14 +01:00
let attempt = await this.authService.login(this.userattempt, {saveSession: false})
2021-07-22 14:40:29 +01:00
2021-09-27 16:23:41 +01:00
loader.remove()
2022-01-14 09:03:31 +01:00
// login to API successfully
2021-06-09 14:00:14 +01:00
if (attempt) {
2022-01-14 10:35:54 +01:00
if (attempt.UserId == SessionStore.user.UserId) {
2021-09-02 12:17:14 +01:00
await this.authService.SetSession(attempt, this.userattempt);
2022-02-24 12:10:09 +01:00
await this.authService.loginChat();
await this.authService.loginToChatWs();
2021-09-02 12:17:14 +01:00
this.getToken();
2021-09-21 06:09:41 +01:00
SessionStore.setInativity(true);
this.goback();
2021-09-02 12:17:14 +01:00
} else {
2021-09-27 16:23:41 +01:00
this.clearStoreService.clear();
SessionStore.delete();
2021-09-02 12:17:14 +01:00
window.localStorage.clear();
2021-09-28 11:31:10 +01:00
2021-09-02 12:17:14 +01:00
await this.authService.SetSession(attempt, this.userattempt);
this.changeProfileService.run();
2022-02-24 12:10:09 +01:00
await this.authService.loginChat();
await this.authService.loginToChatWs();
2021-10-13 08:38:11 +01:00
this.getToken();
2022-01-14 08:57:11 +01:00
this.router.navigateByUrl('/pin', { replaceUrl: true });
2021-09-02 12:17:14 +01:00
}
2022-02-16 13:52:32 +01:00
}
else{
this.toastService._badRequest('Ocorreu um problema por favor valide o username e password');
2021-06-09 13:34:55 +01:00
}
2021-01-22 16:03:05 +01:00
}
2021-07-01 09:52:36 +01:00
else {
2021-11-09 15:37:59 +01:00
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-11-09 15:37:59 +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
2021-09-02 12:17:14 +01:00
goback() {
const pathName = SessionStore.user.UrlBeforeInactivity
if(pathName) {
this.router.navigate([pathName]);
} else {
2022-03-30 15:08:23 +01:00
if(this.permissionService.userPermission(this.permissionList.Agenda.access) || this.permissionService.userPermission(this.permissionList.Gabinete.access)){
this.router.navigate(['/home/events']);
}
2022-03-30 15:08:23 +01:00
else if(this.permissionService.userPermission(this.permissionList.Chat.access) && this.permissionService.userPermission(this.permissionList.Actions.access)){
this.router.navigate(['/home/chat']);
}
2022-03-30 15:08:23 +01:00
else if(this.permissionService.userPermission(this.permissionList.Actions.access)){
alert('here')
this.router.navigate(['/home/publications']);
}
2021-09-02 12:17:14 +01:00
}
2021-09-02 12:17:14 +01:00
}
2021-07-22 14:40:29 +01:00
}