Files
doneit-web/src/app/services/auth.service.ts
T

103 lines
2.6 KiB
TypeScript
Raw Normal View History

2020-08-06 14:31:07 +01:00
import { Injectable } from '@angular/core';
import { StorageService } from './storage.service';
import { Router } from '@angular/router';
2020-10-30 15:22:35 +01:00
import { HttpClient, HttpHeaders, HttpParams } from '@angular/common/http';
import { User } from '../models/user.model';
import { environment } from 'src/environments/environment';
2020-10-30 15:22:35 +01:00
import { HttpService } from './http.service';
import { BehaviorSubject, Observable } from 'rxjs';
import { AuthConnstants } from '../config/auth-constants';
2021-01-15 12:31:21 +01:00
import { AlertController } from '@ionic/angular';
2020-08-06 14:31:07 +01:00
@Injectable({
providedIn: 'root'
})
export class AuthService {
2020-10-30 15:22:35 +01:00
userData$ = new BehaviorSubject<any>('');
2020-08-06 14:31:07 +01:00
constructor(
2020-10-30 15:22:35 +01:00
private http: HttpClient,
private httpService: HttpService,
private storageService:StorageService,
2021-01-15 12:31:21 +01:00
public alertController: AlertController,
2020-10-30 15:22:35 +01:00
private router:Router
2020-08-06 14:31:07 +01:00
) { }
public ValidatedUser:User;
2020-08-06 14:31:07 +01:00
2021-01-15 12:31:21 +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();
}
async login(user: User): Promise<boolean> {
2021-01-15 12:31:21 +01:00
user.BasicAuthKey = 'Basic ' + btoa(user.domainName + '\\' + user.username + ':' + user.password); //conversão em base64 das credenciais inseridas
2020-08-06 14:31:07 +01:00
2021-01-15 12:31:21 +01:00
const options = { headers: {'Authorization': user.BasicAuthKey }};
2021-01-15 12:31:21 +01:00
const service = environment.apiURL + "userauthentication/GetValidateAuth";
let result: boolean | PromiseLike<boolean>;
2021-01-15 12:31:21 +01:00
this.presentAlert(service);
2021-01-15 12:31:21 +01:00
try {
result = await this.http.get<boolean>(service, options).toPromise();
} catch (error) {
this.presentAlert('Erro '+error);
}
if (result)
{
2021-01-15 12:31:21 +01:00
this.presentAlert('7');
this.ValidatedUser = user;
2021-01-15 12:31:21 +01:00
this.presentAlert('8');
2020-12-21 16:37:44 +01:00
2020-10-30 15:22:35 +01:00
}
return result;
}
2020-08-06 14:31:07 +01:00
logout(){
this.ValidatedUser = null;
2020-10-30 15:22:35 +01:00
}
//Login to rocketChat server
loginChat(postData: any):Observable<any> {
2021-01-13 10:02:30 +01:00
return this.httpService.post('login', postData);
2020-10-30 15:22:35 +01:00
}
2021-01-13 10:02:30 +01:00
//Get user data from RocketChat | global object
2020-12-16 16:06:14 +01:00
getUserData(){
2021-01-13 10:02:30 +01:00
this.storageService.get(AuthConnstants.AUTH).then(res=>{
2020-10-30 15:22:35 +01:00
this.userData$.next(res);
2021-01-13 10:02:30 +01:00
});
2020-12-16 16:06:14 +01:00
}
2020-10-30 15:22:35 +01:00
2021-01-13 10:02:30 +01:00
getProfile(){
2020-11-24 13:46:13 +01:00
this.storageService.get(AuthConnstants.PROFILE).then(res=>{
return res;
});
2021-01-13 10:02:30 +01:00
}
2020-11-24 13:46:13 +01:00
2020-10-30 15:22:35 +01:00
logoutChat(){
//this.storageService.clear();
2021-01-13 10:02:30 +01:00
this.storageService.removeStorageItem(AuthConnstants.AUTH).then(res =>{
2020-10-30 15:22:35 +01:00
this.userData$.next('');
this.router.navigate(['']);
2021-01-13 10:02:30 +01:00
})
2020-10-30 15:22:35 +01:00
}
2020-08-06 14:31:07 +01:00
}