mirror of
https://code.equilibrium.co.ao/ITO/doneit-web.git
synced 2026-04-18 20:47:54 +00:00
154 lines
4.3 KiB
TypeScript
154 lines
4.3 KiB
TypeScript
import { Injectable } from '@angular/core';
|
|
import { StorageService } from './storage.service';
|
|
import { Router } from '@angular/router';
|
|
import { HttpClient, HttpHeaders, HttpParams } from '@angular/common/http';
|
|
import { User, UserForm } from '../models/user.model';
|
|
import { environment } from 'src/environments/environment';
|
|
import { HttpService } from './http.service';
|
|
import { BehaviorSubject, Observable } from 'rxjs';
|
|
import { AuthConnstants } from '../config/auth-constants';
|
|
import { AlertController } from '@ionic/angular';
|
|
|
|
@Injectable({
|
|
providedIn: 'root'
|
|
})
|
|
export class AuthService {
|
|
userData$ = new BehaviorSubject<any>('');
|
|
userId$ = new BehaviorSubject<any>('');
|
|
headers: HttpHeaders;
|
|
public ValidatedUser:User;
|
|
public ValidatedUserChat:any;
|
|
opts:any;
|
|
|
|
constructor(
|
|
private http: HttpClient,
|
|
private httpService: HttpService,
|
|
private storageService:StorageService,
|
|
private router:Router,
|
|
public alertController: AlertController,
|
|
) {
|
|
|
|
|
|
|
|
this.headers = new HttpHeaders();
|
|
|
|
if (localStorage.getItem("user") != null) {
|
|
this.ValidatedUser = JSON.parse(localStorage.getItem('user'));
|
|
}
|
|
if (localStorage.getItem("userChat") != null) {
|
|
this.ValidatedUserChat = JSON.parse(localStorage.getItem('userChat'));
|
|
}
|
|
|
|
}
|
|
|
|
|
|
async login(user: UserForm): Promise<boolean> {
|
|
//user.BasicAuthKey = 'Basic ' + btoa(user.username + '@' + user.domainName + ':' + user.password);
|
|
user.BasicAuthKey = 'Basic ' + btoa(user.username + ':' + user.password); //conversão em base64 das credenciais inseridas
|
|
|
|
|
|
const options = { headers: {'Authorization': user.BasicAuthKey }};
|
|
this.headers = this.headers.set('Authorization',user.BasicAuthKey);
|
|
this.opts = {
|
|
headers: this.headers,
|
|
}
|
|
const service = environment.apiURL + "userauthentication/GetValidateAuth";
|
|
|
|
let result: boolean | PromiseLike<boolean> = false;
|
|
let response: any;
|
|
|
|
try {
|
|
response = await this.http.post<User>(environment.apiURL + "UserAuthentication/Login", '', this.opts).toPromise();
|
|
|
|
console.log(response);
|
|
|
|
if (response) {
|
|
if( response.RoleID == 100000014) {
|
|
response.Profile = 'PR'
|
|
} else if(response.RoleID == 100000011) {
|
|
response.Profile = 'MDGPR'
|
|
}
|
|
response.BasicAuthKey = user.BasicAuthKey
|
|
this.ValidatedUser = response;
|
|
|
|
localStorage.setItem('user', JSON.stringify(response));
|
|
|
|
this.storageService.store(AuthConnstants.USER, response);
|
|
|
|
return true;
|
|
}
|
|
|
|
} catch (error) {
|
|
return false;
|
|
}
|
|
|
|
}
|
|
|
|
logout(){
|
|
this.ValidatedUser = null;
|
|
}
|
|
|
|
//Login to rocketChat server
|
|
async loginChat(user: UserForm): Promise<boolean> {
|
|
let postData = {
|
|
"user": user.username,
|
|
"password": user.password,
|
|
}
|
|
|
|
let responseChat = await this.httpService.post('login', postData).toPromise();
|
|
if(responseChat){
|
|
console.log('Login to Rocket chat OK');
|
|
this.ValidatedUserChat = responseChat;
|
|
localStorage.setItem('userChat', JSON.stringify(responseChat));
|
|
this.storageService.store(AuthConnstants.AUTH, responseChat);
|
|
return true;
|
|
}
|
|
else{
|
|
console.log('Network error');
|
|
this.presentAlert('Network error');
|
|
return false;
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
//Get user data from RocketChat | global object
|
|
getUserData(){
|
|
this.storageService.get(AuthConnstants.AUTH).then(res=>{
|
|
this.userData$.next(res);
|
|
});
|
|
}
|
|
//Get user Id | global object
|
|
getUserId(){
|
|
/* this.storageService.get(AuthConnstants.USER).then(res=>{
|
|
this.userId$.next(res);
|
|
}); */
|
|
}
|
|
|
|
getProfile(){
|
|
/* this.storageService.get(AuthConnstants.PROFILE).then(res=>{
|
|
return res;
|
|
}); */
|
|
}
|
|
|
|
logoutChat(){
|
|
//this.storageService.clear();
|
|
/* this.storageService.removeStorageItem(AuthConnstants.AUTH).then(res =>{
|
|
this.userData$.next('');
|
|
this.router.navigate(['']);
|
|
}) */
|
|
|
|
}
|
|
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();
|
|
}
|
|
}
|