mirror of
https://code.equilibrium.co.ao/ITO/doneit-web.git
synced 2026-04-19 04:57:52 +00:00
105 lines
3.0 KiB
TypeScript
105 lines
3.0 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 } 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<any>('');
|
|
userId$ = new BehaviorSubject<any>('');
|
|
headers: HttpHeaders;
|
|
opts:any;
|
|
constructor(
|
|
private http: HttpClient,
|
|
private httpService: HttpService,
|
|
private storageService:StorageService,
|
|
private router:Router
|
|
) {
|
|
this.headers = new HttpHeaders();
|
|
|
|
//if(!environment.production){
|
|
if (localStorage.getItem("user") != null) {
|
|
this.ValidatedUser = JSON.parse(localStorage.getItem('user'));
|
|
}
|
|
//}
|
|
|
|
}
|
|
public ValidatedUser:User;
|
|
|
|
async login(user: User): Promise<boolean> {
|
|
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<boolean>;
|
|
let response: any;
|
|
|
|
result = await this.http.get<boolean>(service, options).toPromise();
|
|
response = await this.http.post<any>(environment.apiURL + "UserAuthentication/Login", '', this.opts).toPromise();
|
|
|
|
if (result)
|
|
{
|
|
this.ValidatedUser = user;
|
|
|
|
//if(!environment.production){
|
|
localStorage.setItem('user', JSON.stringify(Object.assign(user, response) ) );
|
|
//}
|
|
|
|
this.storageService.store(AuthConnstants.USER, response);
|
|
}
|
|
return result;
|
|
}
|
|
|
|
logout(){
|
|
this.ValidatedUser = null;
|
|
}
|
|
|
|
//Login to rocketChat server
|
|
loginChat(postData: any):Observable<any> {
|
|
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(['']);
|
|
}) */
|
|
|
|
}
|
|
}
|