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';
|
2020-08-21 00:22:51 +01:00
|
|
|
import { User } 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';
|
2020-08-25 10:37:41 +01:00
|
|
|
import { AlertController } 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';
|
|
|
|
|
import { Token } from '../../models/token.model';
|
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-02-23 16:10:14 +01:00
|
|
|
*/
|
2021-01-22 16:03:05 +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;
|
2020-08-21 00:22:51 +01:00
|
|
|
userattempt: User;
|
2020-08-06 14:31:07 +01:00
|
|
|
|
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-02-25 10:47:13 +01:00
|
|
|
public alertController: AlertController) { }
|
2020-08-05 15:39:16 +01:00
|
|
|
|
|
|
|
|
ngOnInit() {
|
2021-01-22 16:03:05 +01:00
|
|
|
|
2020-08-05 15:39:16 +01:00
|
|
|
}
|
2020-11-24 13:46:13 +01:00
|
|
|
//Function to validade the login inputs
|
2021-02-25 11:50:32 +01:00
|
|
|
validateInput(){
|
2020-08-06 14:31:07 +01:00
|
|
|
return (
|
2021-01-22 16:03:05 +01:00
|
|
|
this.username.trim().length > 0
|
2020-08-21 00:22:51 +01:00
|
|
|
&& 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-02-08 15:16:29 +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-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-02-01 13:31:33 +01:00
|
|
|
}
|
2021-01-22 16:03:05 +01:00
|
|
|
|
2021-02-23 16:10:14 +01:00
|
|
|
/* storeUserIdANdToken() {
|
2021-01-22 16:03:05 +01:00
|
|
|
|
2021-02-10 11:31:07 +01:00
|
|
|
(PushNotifications as any).requestPermission().then(result => {
|
2021-01-22 16:03:05 +01:00
|
|
|
PushNotifications.register();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
PushNotifications.addListener(
|
|
|
|
|
'registration',
|
|
|
|
|
(token: PushNotificationToken) => {
|
|
|
|
|
console.log('FIREBASE TOKEN', token.value)
|
|
|
|
|
this.storageService.store(this.username, token.value);
|
|
|
|
|
this.storageService.get(this.username).then(value => {
|
2021-02-01 13:31:33 +01:00
|
|
|
console.log('STORAGE TOKEN', value)
|
|
|
|
|
this.storageService.get(AuthConnstants.USER).then(res => {
|
|
|
|
|
console.log('USERID', res);
|
|
|
|
|
const headers = { 'Authorization': 'Basic cGF1bG8ucGludG9AZ2FiaW5ldGVkaWdpdGFsLmxvY2FsOnRhYnRlc3RlQDAwNg==' };
|
2021-02-09 20:20:36 +01:00
|
|
|
const body = {
|
|
|
|
|
UserId: res,
|
2021-02-01 13:58:29 +01:00
|
|
|
TokenId: token.value,
|
2021-02-01 13:31:33 +01:00
|
|
|
Status: 1,
|
2021-02-09 20:20:36 +01:00
|
|
|
Service: 1
|
|
|
|
|
};
|
2021-02-01 13:31:33 +01:00
|
|
|
|
2021-02-09 20:20:36 +01:00
|
|
|
this.http.post<Token>('https://equilibrium.dyndns.info/GabineteDigital.Services/V4/api/notifications/token', body, { headers }).subscribe(data => {
|
2021-02-01 13:31:33 +01:00
|
|
|
console.log('TOKEN USER MIDLE', data);
|
|
|
|
|
})
|
2021-02-10 11:31:07 +01:00
|
|
|
});
|
2021-02-01 13:31:33 +01:00
|
|
|
|
2021-02-01 09:58:15 +01:00
|
|
|
});
|
2021-02-01 13:31:33 +01:00
|
|
|
|
2021-01-22 16:03:05 +01:00
|
|
|
},
|
2021-02-10 11:31:07 +01:00
|
|
|
);
|
2021-01-22 16:03:05 +01:00
|
|
|
|
2021-02-23 16:10:14 +01:00
|
|
|
}; */
|
2021-01-22 16:03:05 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
async Login() {
|
|
|
|
|
/* try { */
|
|
|
|
|
//Go to our home in home/feed.
|
|
|
|
|
//this.router.navigate(['/home/events']);
|
|
|
|
|
if (this.validateInput()) {
|
|
|
|
|
this.userattempt = {
|
|
|
|
|
username: this.username,
|
|
|
|
|
password: this.password,
|
|
|
|
|
domainName: environment.domain,
|
|
|
|
|
BasicAuthKey: ""
|
|
|
|
|
}
|
2021-02-25 10:47:13 +01:00
|
|
|
if (await this.authService.login(this.userattempt)) {
|
2021-02-08 15:16:29 +01:00
|
|
|
this.loginRocketChat();
|
2021-02-23 16:10:14 +01:00
|
|
|
//this.storeUserIdANdToken();
|
2021-02-01 13:31:33 +01:00
|
|
|
|
2021-01-22 16:03:05 +01:00
|
|
|
this.router.navigate(['/home/events']);
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
//this.toastService.presentToast('Não foi possível fazer login"');
|
|
|
|
|
this.presentAlert('O nome de utilizador e palavra-passe estão incorretas ou verifique a sua conexão com a internet e volte a tentar.');
|
2021-02-25 10:47:13 +01:00
|
|
|
}
|
2021-01-22 16:03:05 +01:00
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
//this.toastService.presentToast('Preencha todos campos');
|
|
|
|
|
this.presentAlert('Por favor, insira o seu nome de utilizador e palavra-passe.');
|
|
|
|
|
}
|
|
|
|
|
/* } catch (error) {
|
|
|
|
|
error
|
|
|
|
|
this.presentAlert('Ocorreu um erro ao fazer login. Contacte o administrador de sistema. '+ error);
|
|
|
|
|
} */
|
2020-08-05 15:39:16 +01:00
|
|
|
}
|
|
|
|
|
}
|