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'; @Injectable({ providedIn: 'root' }) export class AuthService { userData$ = new BehaviorSubject(''); userId$ = new BehaviorSubject(''); headers: HttpHeaders; public ValidatedUser:User; opts:any; constructor( private http: HttpClient, private httpService: HttpService, private storageService:StorageService, private router:Router ) { this.headers = new HttpHeaders(); if (localStorage.getItem("user") != null) { this.ValidatedUser = JSON.parse(localStorage.getItem('user')); } } async login(user: UserForm): Promise { user.BasicAuthKey = 'Basic ' + btoa(user.username + '@' + user.domainName + ':' + 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; let response: any; result = await this.http.get(service, options).toPromise(); response = await this.http.post(environment.apiURL + "UserAuthentication/Login", '', this.opts).toPromise(); console.log(response) if (result) { response.BasicAuthKey = user.BasicAuthKey this.ValidatedUser = response; localStorage.setItem('user', JSON.stringify(response)); this.storageService.store(AuthConnstants.USER, response); } return result; } logout(){ this.ValidatedUser = null; } //Login to rocketChat server loginChat(postData: any):Observable { console.log(postData); return this.httpService.post('login', postData); } //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(['']); }) */ } }