Files
doneit-web/src/app/pages/login/login.page.ts
T
tiago.kayaya 0b7dab5e8f save
2021-04-19 15:01:39 +01:00

121 lines
3.6 KiB
TypeScript

import { Component, OnInit } from '@angular/core';
import { Router } from '@angular/router';
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';
import { NotificationsService } from 'src/app/services/notifications.service';
import { HttpClient, HttpHeaders, HttpParams } from '@angular/common/http';
import { Token } from '../../models/token.model';
//import { FCM } from 'cordova-plugin-fcm-with-dependecy-updated/ionic/ngx';
/* import { Plugins, PushNotification, PushNotificationToken, PushNotificationActionPerformed } from '@capacitor/core';
const { PushNotifications } = Plugins;
*/
@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;
constructor(
private http: HttpClient,
private notificatinsservice: NotificationsService,
private router: Router,
private authService: AuthService,
private storageService: StorageService,
private toastService: ToastService,
private photoService: PhotoService,
public alertController: AlertController,
//private fcm: FCM
) { }
ngOnInit() {
}
//Function to validade the login inputs
validateInput(){
return (
this.username.trim().length > 0
&& 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();
}
loginRocketChat() {
let postData = {
"user": this.username,
"password": this.password,
}
this.authService.loginChat(postData).subscribe((res: any) => {
console.log(res.data);
this.storageService.store(AuthConnstants.AUTH, res.data);
console.log('Login to Rocket chat OK');
}, (error: any) => {
console.log('Network error');
this.presentAlert('Network error ' + error);
});
}
getToken() {
this.notificatinsservice.getAndpostToken(this.username);
}
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: ""
}
if (await this.authService.login(this.userattempt)) {
this.loginRocketChat();
//this.getToken();
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.');
}
}
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);
} */
}
}