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

81 lines
2.2 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';
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';
2020-08-06 14:31:07 +01:00
@Injectable({
providedIn: 'root'
})
export class AuthService {
userData$ = new BehaviorSubject<any>('');
2020-08-06 14:31:07 +01:00
constructor(
private http: HttpClient,
private httpService: HttpService,
private storageService:StorageService,
private router:Router
2020-08-06 14:31:07 +01:00
) { }
public ValidatedUser:User;
2020-08-06 14:31:07 +01:00
async login(user: User): Promise<boolean> {
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
const options = { headers: {'Authorization': user.BasicAuthKey }};
const service = environment.apiURL + "userauthentication/GetValidateAuth";
let result: boolean | PromiseLike<boolean>;
result = await this.http.get<boolean>(service, options).toPromise();
if (result)
{
this.ValidatedUser = user;
}
return result;
}
2020-08-06 14:31:07 +01:00
logout(){
this.ValidatedUser = null;
}
loginChat(){
const body = {"user": "admin","password": "tabteste@006"};
const url = "http://192.168.100.111:3000/api/v1/login";
return this.http.post(url, body);
}
//Login to rocketChat server
loginChat2(postData: any):Observable<any> {
return this.httpService.post('login', postData);
}
//Get user data from RocketChat
getUserData(){
this.storageService.get(AuthConnstants.AUTH).then(res=>{
this.userData$.next(res);
})
}
2020-11-24 13:46:13 +01:00
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(['']);
})
}
2020-08-06 14:31:07 +01:00
}