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

149 lines
4.6 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';
import { User } 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 { StorageService } from 'src/app/services/storage.service';
import { AuthConnstants } from 'src/app/config/auth-constants';
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';
2021-02-23 16:10:14 +01:00
/* import { Plugins, PushNotification, PushNotificationToken, PushNotificationActionPerformed } from '@capacitor/core';
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 {
logstatus: boolean;
username: string = environment.defaultuser;
password: string = environment.defaultuserpwd;
userattempt: User;
2020-08-06 14:31:07 +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,
private authService: AuthService,
private storageService: StorageService,
private toastService: ToastService,
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
&& 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-02-08 15:16:29 +01:00
loginRocketChat() {
2021-02-01 13:31:33 +01:00
let postData = {
2021-02-08 15:16:29 +01:00
"user": this.username,
"password": this.password,
}
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);
console.log('Login to Rocket chat OK');
2021-02-01 13:31:33 +01:00
}, (error: any) => {
console.log('Network error');
2021-02-01 13:31:33 +01:00
this.presentAlert('Network error ' + error);
});
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
(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==' };
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,
Service: 1
};
2021-02-01 13:31:33 +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-01 13:31:33 +01:00
});
2021-02-01 13:31:33 +01:00
2021-01-22 16:03:05 +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
}
}