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

147 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';
2021-08-27 15:21:15 +01:00
import { HttpClient, HttpHeaders } from '@angular/common/http';
import { LoginUserRespose, UserForm, UserSession } from '../models/user.model';
import { environment } from 'src/environments/environment';
import { HttpService } from './http.service';
2021-08-27 15:21:15 +01:00
import { BehaviorSubject } from 'rxjs';
import { AuthConnstants } from '../config/auth-constants';
2021-06-04 11:37:56 +01:00
import { AlertController } from '@ionic/angular';
2021-08-27 13:39:52 +01:00
import { SessionStore } from '../store/session.service';
2021-08-26 16:32:59 +01:00
import { AESEncrypt } from '../services/aesencrypt.service';
2021-10-15 15:21:26 +01:00
import { CookieService } from 'ngx-cookie-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-08-27 15:21:15 +01:00
public ValidatedUser: UserSession;
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
public alertController: AlertController,
2021-08-26 16:32:59 +01:00
private aesencrypt: AESEncrypt,
2021-10-15 15:21:26 +01:00
private cookieService: CookieService,
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-08-27 13:39:52 +01:00
if (SessionStore.exist) {
this.ValidatedUser = SessionStore.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
2021-09-02 12:17:14 +01:00
async login(user: UserForm, {saveSession = true}): Promise<LoginUserRespose> {
2021-08-27 10:13:35 +01:00
user.BasicAuthKey = 'Basic ' + btoa(user.username + ':' + this.aesencrypt.encrypt(user.password,user.username ));
2021-02-18 12:44:35 +01:00
2021-09-28 11:31:10 +01:00
this.headers = this.headers.set('Authorization', user.BasicAuthKey);
2021-01-19 10:44:55 +01:00
this.opts = {
headers: this.headers,
}
2021-07-22 17:07:04 +01:00
2021-01-19 10:44:55 +01:00
let response: any;
2021-06-09 14:00:14 +01:00
try {
2021-08-27 15:21:15 +01:00
response = await this.http.post<LoginUserRespose>(environment.apiURL + "UserAuthentication/Login", '', this.opts).toPromise();
if(saveSession) {
this.SetSession(response, user)
}
} catch (error) {
2021-03-31 16:32:33 +01:00
2021-08-27 15:21:15 +01:00
} finally {
return response
}
2021-05-10 15:45:09 +01:00
2021-08-27 15:21:15 +01:00
}
2021-03-31 16:32:33 +01:00
2021-08-27 15:21:15 +01:00
SetSession(response: LoginUserRespose, user:UserForm) {
2021-10-05 10:18:38 +01:00
const session: UserSession = Object.assign(SessionStore.user, response)
2021-05-10 14:51:51 +01:00
2021-08-27 15:21:15 +01:00
if (response) {
if( session.RoleID == 100000014) {
session.Profile = 'PR'
} else if(session.RoleID == 100000011) {
session.Profile = 'MDGPR'
2021-06-09 14:00:14 +01:00
}
2021-07-22 17:07:04 +01:00
2021-08-27 15:21:15 +01:00
session.BasicAuthKey = user.BasicAuthKey
2021-05-10 14:51:51 +01:00
2021-08-27 15:21:15 +01:00
SessionStore.reset(session)
this.ValidatedUser = SessionStore.user;
this.storageService.store(AuthConnstants.USER, response);
2021-07-22 17:07:04 +01:00
2021-08-27 15:21:15 +01:00
return true;
2021-05-10 14:51:51 +01:00
}
}
2020-08-06 14:31:07 +01:00
2021-08-27 15:21:15 +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,
}
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));
2021-10-15 15:21:26 +01:00
localStorage.setItem('Meteor.loginToken', responseChat['data'].authToken);
localStorage.setItem('Meteor.userId',responseChat['data'].userId);
this.cookieService.set('rc_token', responseChat['data'].authToken);
this.cookieService.set('rc_uid', responseChat['data'].userId);
2021-06-04 11:37:56 +01:00
this.storageService.store(AuthConnstants.AUTH, responseChat);
return true;
}
else{
console.log('Network error');
this.presentAlert('Network error');
return false;
}
}
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
});
}
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']
});
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
}