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

155 lines
4.4 KiB
TypeScript
Raw Normal View History

2020-08-06 14:31:07 +01:00
import { Injectable } from '@angular/core';
2021-02-25 10:25:46 +01:00
import { StorageService } from './storage.service';
2020-08-06 14:31:07 +01:00
import { Router } from '@angular/router';
import { HttpClient, HttpHeaders, HttpParams } from '@angular/common/http';
2021-05-10 14:51:51 +01:00
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';
2021-06-04 11:37:56 +01:00
import { AlertController } from '@ionic/angular';
2021-07-18 20:20:30 +01:00
import { LocalstoreService } from '../store/localstore.service';
2020-08-06 14:31:07 +01:00
@Injectable({
providedIn: 'root'
})
export class AuthService {
userData$ = new BehaviorSubject<any>('');
2021-01-19 10:44:55 +01:00
userId$ = new BehaviorSubject<any>('');
headers: HttpHeaders;
2021-05-10 14:51:51 +01:00
public ValidatedUser:User;
2021-06-04 11:37:56 +01:00
public ValidatedUserChat:any;
2021-01-19 10:44:55 +01:00
opts:any;
2021-05-10 14:51:51 +01:00
2020-08-06 14:31:07 +01:00
constructor(
private http: HttpClient,
private httpService: HttpService,
2021-02-25 10:25:46 +01:00
private storageService:StorageService,
2021-06-04 11:37:56 +01:00
private router:Router,
public alertController: AlertController,
2021-07-18 20:20:30 +01:00
private localstoreService: LocalstoreService
2021-01-19 10:44:55 +01:00
) {
2021-05-10 15:31:16 +01:00
2021-01-19 10:44:55 +01:00
this.headers = new HttpHeaders();
2020-08-06 14:31:07 +01:00
2021-07-18 20:52:01 +01:00
if (this.localstoreService.get('user', null) != null) {
this.ValidatedUser = this.localstoreService.get('user',{});
2021-05-10 14:51:51 +01:00
}
2021-07-18 20:20:30 +01:00
2021-06-04 11:37:56 +01:00
if (localStorage.getItem("userChat") != null) {
this.ValidatedUserChat = JSON.parse(localStorage.getItem('userChat'));
}
2021-02-18 12:44:35 +01:00
}
2021-05-10 14:51:51 +01:00
2020-08-06 14:31:07 +01:00
2021-05-10 14:51:51 +01:00
async login(user: UserForm): Promise<boolean> {
2021-06-25 12:31:25 +01:00
//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
2021-03-31 16:32:33 +01:00
2021-02-18 12:44:35 +01:00
const options = { headers: {'Authorization': user.BasicAuthKey }};
2021-01-19 10:44:55 +01:00
this.headers = this.headers.set('Authorization',user.BasicAuthKey);
this.opts = {
headers: this.headers,
}
const service = environment.apiURL + "userauthentication/GetValidateAuth";
2021-06-09 14:00:14 +01:00
let result: boolean | PromiseLike<boolean> = false;
2021-01-19 10:44:55 +01:00
let response: any;
2021-06-09 14:00:14 +01:00
try {
response = await this.http.post<User>(environment.apiURL + "UserAuthentication/Login", '', this.opts).toPromise();
2021-02-26 08:38:33 +01:00
2021-06-09 14:00:14 +01:00
console.log(response);
2021-03-31 16:32:33 +01:00
2021-06-25 12:06:18 +01:00
if (response) {
2021-06-09 14:00:14 +01:00
if( response.RoleID == 100000014) {
response.Profile = 'PR'
} else if(response.RoleID == 100000011) {
response.Profile = 'MDGPR'
}
response.BasicAuthKey = user.BasicAuthKey
this.ValidatedUser = response;
2021-05-10 15:45:09 +01:00
2021-07-18 20:20:30 +01:00
this.localstoreService.set('user',response)
2021-03-31 16:32:33 +01:00
2021-06-09 14:00:14 +01:00
this.storageService.store(AuthConnstants.USER, response);
2021-05-10 14:51:51 +01:00
2021-06-25 12:06:18 +01:00
return true;
2021-06-09 14:00:14 +01:00
}
} catch (error) {
2021-06-25 12:06:18 +01:00
return false;
2021-05-10 14:51:51 +01:00
}
2021-06-09 14:00:14 +01:00
}
2020-08-06 14:31:07 +01:00
logout(){
this.ValidatedUser = null;
}
//Login to rocketChat server
2021-06-04 11:37:56 +01:00
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;
}
}
2021-06-04 11:37:56 +01:00
2021-01-13 10:02:30 +01:00
//Get user data from RocketChat | global object
getUserData(){
2021-02-25 12:41:29 +01:00
this.storageService.get(AuthConnstants.AUTH).then(res=>{
this.userData$.next(res);
2021-02-25 12:41:29 +01:00
});
}
2021-01-19 10:44:55 +01:00
//Get user Id | global object
getUserId(){
2021-02-23 16:10:14 +01:00
/* this.storageService.get(AuthConnstants.USER).then(res=>{
2021-01-19 10:44:55 +01:00
this.userId$.next(res);
2021-02-23 16:10:14 +01:00
}); */
2021-01-19 10:44:55 +01:00
}
2021-01-13 10:02:30 +01:00
getProfile(){
2021-02-23 16:10:14 +01:00
/* this.storageService.get(AuthConnstants.PROFILE).then(res=>{
2020-11-24 13:46:13 +01:00
return res;
2021-02-23 16:10:14 +01:00
}); */
2021-01-13 10:02:30 +01:00
}
2020-11-24 13:46:13 +01:00
logoutChat(){
//this.storageService.clear();
2021-02-23 16:10:14 +01:00
/* this.storageService.removeStorageItem(AuthConnstants.AUTH).then(res =>{
this.userData$.next('');
this.router.navigate(['']);
2021-02-23 16:10:14 +01:00
}) */
}
2021-06-04 11:37:56 +01:00
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();
}
2020-08-06 14:31:07 +01:00
}