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 } 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(''); constructor( private http: HttpClient, private httpService: HttpService, private storageService:StorageService, private router:Router ) { } public ValidatedUser:User; async login(user: User): Promise { user.BasicAuthKey = 'Basic ' + btoa(user.domainName + '\\' + user.username + ':' + user.password); //conversão em base64 das credenciais inseridas const options = { headers: {'Authorization': user.BasicAuthKey }}; const service = environment.apiURL + "userauthentication/GetValidateAuth"; let result: boolean | PromiseLike; result = await this.http.get(service, options).toPromise(); if (result) { this.ValidatedUser = user; } return result; } logout(){ this.ValidatedUser = null; } //Login to rocketChat server loginChat(postData: any):Observable { const res = this.httpService.post('login', postData); const res2 = res.subscribe(res=>{ this.storageService.store(res); }); return res; } //Get user data from RocketChat getUserData(){ const res = this.storageService.get('user'); console.log(res); /* this.storageService.get(AuthConnstants.AUTH).then(res=>{ this.userData$.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(['']); }) */ } }