Files
doneit-web/src/app/pages/login/login.page.ts
T
2021-01-27 16:01:49 +01:00

106 lines
3.1 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';
@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 router: Router,
private authService: AuthService,
private storageService: StorageService,
private toastService: ToastService,
private photoService: PhotoService,
public alertController: AlertController) { }
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(user:any){
let postData = {
"user": user.username,
"password": user.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);
});
}
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)){
console.log(this.userattempt);
this.loginRocketChat(this.userattempt);
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. ');
}
}
}