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';
|
2020-10-30 15:22:35 +01:00
|
|
|
import { HttpClient, HttpHeaders, HttpParams } from '@angular/common/http';
|
2021-05-10 14:51:51 +01:00
|
|
|
import { User, UserForm } from '../models/user.model';
|
2020-08-21 00:22:51 +01:00
|
|
|
import { environment } from 'src/environments/environment';
|
2020-10-30 15:22:35 +01:00
|
|
|
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';
|
2021-07-22 16:06:52 +01:00
|
|
|
import { ToastService } from './toast.service';
|
2021-07-29 17:02:16 +01:00
|
|
|
import { UserStore } from 'src/app/store/user.service'
|
2021-08-19 15:40:21 +01:00
|
|
|
import { SHA1, SHA256, AES, enc } from 'crypto-js'
|
2020-08-06 14:31:07 +01:00
|
|
|
|
|
|
|
|
@Injectable({
|
|
|
|
|
providedIn: 'root'
|
|
|
|
|
})
|
|
|
|
|
export class AuthService {
|
2020-10-30 15:22:35 +01:00
|
|
|
userData$ = new BehaviorSubject<any>('');
|
2021-01-19 10:44:55 +01:00
|
|
|
userId$ = new BehaviorSubject<any>('');
|
|
|
|
|
headers: HttpHeaders;
|
2021-07-29 17:02:16 +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-07-29 17:02:16 +01:00
|
|
|
userStore = UserStore
|
2021-05-10 14:51:51 +01:00
|
|
|
|
2020-08-06 14:31:07 +01:00
|
|
|
constructor(
|
2020-10-30 15:22:35 +01:00
|
|
|
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-22 16:06:52 +01:00
|
|
|
private localstoreService: LocalstoreService,
|
|
|
|
|
private toastService: ToastService,
|
2021-07-22 17:07:04 +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-07-22 17:07:04 +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-08-19 15:40:21 +01:00
|
|
|
console.log('Basic ' + btoa(user.username + ':' + SHA1(user.password).toString())); //conversão em base64 das credenciais inseridas
|
2021-03-31 16:32:33 +01:00
|
|
|
|
2021-02-18 12:44:35 +01:00
|
|
|
|
2020-08-21 00:22:51 +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,
|
|
|
|
|
}
|
2020-08-21 00:22:51 +01:00
|
|
|
const service = environment.apiURL + "userauthentication/GetValidateAuth";
|
2021-07-22 17:07:04 +01:00
|
|
|
|
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;
|
2020-08-21 00:22:51 +01:00
|
|
|
|
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-29 17:02:16 +01:00
|
|
|
console.log('response', response)
|
|
|
|
|
|
2021-07-18 20:52:14 +01:00
|
|
|
this.localstoreService.set('user', response)
|
2021-07-29 17:02:16 +01:00
|
|
|
this.userStore.reset(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
|
|
|
}
|
2021-07-22 17:07:04 +01:00
|
|
|
|
2021-06-09 14:00:14 +01:00
|
|
|
} catch (error) {
|
2021-07-22 16:06:52 +01:00
|
|
|
if(error.status == 0) {
|
|
|
|
|
this.toastService.badRequest('Verifique a sua conexão com a internet e volte a tentar')
|
|
|
|
|
} else {
|
|
|
|
|
this.toastService.badRequest('O email e/ou palavra-passe estão incorretas ou verifique a sua conexão com a internet e volte a tentar');
|
|
|
|
|
}
|
2021-06-25 12:06:18 +01:00
|
|
|
return false;
|
2021-05-10 14:51:51 +01:00
|
|
|
}
|
2021-07-22 17:07:04 +01:00
|
|
|
|
2020-08-21 00:22:51 +01:00
|
|
|
}
|
2020-08-06 14:31:07 +01:00
|
|
|
|
2020-08-21 00:22:51 +01:00
|
|
|
logout(){
|
|
|
|
|
this.ValidatedUser = null;
|
2020-10-30 15:22:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//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,
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-22 17:07:04 +01:00
|
|
|
console.log(postData);
|
|
|
|
|
|
2021-06-04 11:37:56 +01:00
|
|
|
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;
|
|
|
|
|
}
|
2020-10-30 15:22:35 +01:00
|
|
|
}
|
|
|
|
|
|
2021-06-04 11:37:56 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2021-01-13 10:02:30 +01:00
|
|
|
//Get user data from RocketChat | global object
|
2020-12-16 16:06:14 +01:00
|
|
|
getUserData(){
|
2021-02-25 12:41:29 +01:00
|
|
|
this.storageService.get(AuthConnstants.AUTH).then(res=>{
|
2020-10-30 15:22:35 +01:00
|
|
|
this.userData$.next(res);
|
2021-02-25 12:41:29 +01:00
|
|
|
});
|
2020-12-16 16:06:14 +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
|
|
|
}
|
2020-10-30 15:22:35 +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
|
|
|
|
2020-10-30 15:22:35 +01:00
|
|
|
logoutChat(){
|
|
|
|
|
//this.storageService.clear();
|
2021-02-23 16:10:14 +01:00
|
|
|
/* this.storageService.removeStorageItem(AuthConnstants.AUTH).then(res =>{
|
2020-10-30 15:22:35 +01:00
|
|
|
this.userData$.next('');
|
|
|
|
|
this.router.navigate(['']);
|
2021-02-23 16:10:14 +01:00
|
|
|
}) */
|
2020-10-30 15:22:35 +01:00
|
|
|
|
2020-08-19 23:47:11 +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']
|
|
|
|
|
});
|
2021-07-22 17:07:04 +01:00
|
|
|
|
2021-06-04 11:37:56 +01:00
|
|
|
await alert.present();
|
|
|
|
|
}
|
2020-08-06 14:31:07 +01:00
|
|
|
}
|