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

202 lines
5.8 KiB
TypeScript
Raw Normal View History

2023-06-30 15:46:41 +01:00
import { Injectable, ErrorHandler } from '@angular/core';
2021-02-25 10:25:46 +01:00
import { StorageService } from './storage.service';
2022-02-08 17:44:15 +01:00
import { HttpClient, HttpHeaders, HttpEventType } from '@angular/common/http';
2024-11-06 10:38:58 +01:00
import { UserForm, UserSession } from '../models/user.model';
import { environment } from 'src/environments/environment';
2023-11-29 12:17:52 +01:00
import { BehaviorSubject, of } from 'rxjs';
2023-12-06 17:01:00 +01:00
import { AlertController, Platform } 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';
2022-02-02 11:57:11 +01:00
import { Router } from '@angular/router';
2022-02-07 17:55:00 +01:00
import { Storage } from '@ionic/storage';
2022-03-28 13:34:01 +01:00
import { InitialsService } from './functions/initials.service';
import { PermissionService } from './permission.service';
2024-08-09 10:50:32 +01:00
// import { ChatSystemService } from 'src/app/services/chat/chat-system.service';
2023-03-09 09:09:10 +01:00
import { HttpErrorHandle } from 'src/app/services/http-error-handle.service';
2023-06-30 15:46:41 +01:00
import { captureException } from '@sentry/angular';
2023-11-29 12:17:52 +01:00
import { catchError, tap } from 'rxjs/operators';
2024-11-06 10:38:58 +01:00
import { UserLoginOutput } from '../core/user/use-case/user-login-use-case.service';
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>('');
2023-10-25 09:08:49 +01:00
headers: HttpHeaders = new HttpHeaders();
2023-10-20 17:05:39 +01:00
public wsValidatedUserChat: any;
2022-01-12 09:29:48 +01:00
public isWsAuthenticated: boolean = false;
2023-10-20 17:05:39 +01:00
opts: any;
2021-05-10 14:51:51 +01:00
2023-05-29 11:51:08 +01:00
tabIsActive = true
2023-09-29 16:40:50 +01:00
2020-08-06 14:31:07 +01:00
constructor(
2020-10-30 15:22:35 +01:00
private http: HttpClient,
2021-06-04 11:37:56 +01:00
public alertController: AlertController,
2021-08-26 16:32:59 +01:00
private aesencrypt: AESEncrypt,
2024-08-09 10:50:32 +01:00
// private RochetChatConnectorService: RochetChatConnectorService,
2022-02-07 17:55:00 +01:00
private router: Router,
2024-08-09 10:50:32 +01:00
// private NfService: NfService,
private initialsService: InitialsService,
2022-07-21 18:05:29 +01:00
public p: PermissionService,
2024-08-09 10:50:32 +01:00
// public ChatSystemService: ChatSystemService,
2023-06-30 15:46:41 +01:00
private httpErroHandle: HttpErrorHandle,
2023-12-06 17:01:00 +01:00
private errorHandler: ErrorHandler,
private platform: Platform,) {
2020-08-06 14:31:07 +01:00
2023-10-20 17:05:39 +01:00
}
2021-02-18 12:44:35 +01:00
2024-11-06 10:38:58 +01:00
async login(user: UserForm, { saveSession = true }): Promise<UserSession> {
2023-11-29 12:17:52 +01:00
user.BasicAuthKey = btoa(user.username + ':' + this.aesencrypt.encrypt(user.password, user.username));
2021-07-22 17:07:04 +01:00
2023-10-20 17:05:39 +01:00
this.headers = this.headers.set('Authorization', user.BasicAuthKey);
2023-11-29 12:17:52 +01:00
2023-10-20 17:05:39 +01:00
this.opts = {
2023-11-29 12:17:52 +01:00
/* headers: this.headers, */
"Content-Type": "application/json",
"Accept": "application/json",
}
2023-12-06 17:01:00 +01:00
let channelId;
if ( this.platform.is('desktop') || this.platform.is("mobileweb")){
channelId = 2
} else {
channelId = 1
}
2023-11-29 12:17:52 +01:00
let body = {
"Auth": user.BasicAuthKey,
2023-12-06 17:01:00 +01:00
"ChannelId": channelId
2023-10-20 17:05:39 +01:00
}
2023-10-20 17:05:39 +01:00
let response: any;
2023-09-29 16:40:50 +01:00
2023-10-20 17:05:39 +01:00
try {
2024-11-06 10:38:58 +01:00
response = await this.http.post<UserSession>(environment.apiURL + "UserAuthentication/Login", body, this.opts).toPromise();
2022-03-28 13:34:01 +01:00
2021-05-10 15:45:09 +01:00
2023-10-20 17:05:39 +01:00
if (saveSession) {
2024-11-06 10:38:58 +01:00
// this.SetSession(response, user)
2023-10-20 17:05:39 +01:00
}
} catch (error) {
this.errorHandler.handleError(error);
this.httpErroHandle.loginHttpStatusHandle(error)
captureException(error);
2024-10-15 10:41:21 +01:00
if(error?.status == 403) {
console.log('error?.status == 403')
}
2023-10-20 17:05:39 +01:00
} finally {
return response
2021-08-27 15:21:15 +01:00
}
2021-03-31 16:32:33 +01:00
2023-10-20 17:05:39 +01:00
}
2024-12-05 19:14:11 +01:00
// async loginContenteProduction(user: UserForm, { saveSession = true }): Promise<UserSession> {
// user.BasicAuthKey = 'Basic ' + btoa(user.username + ':' + this.aesencrypt.encrypt(user.password, user.username));
2024-12-05 19:14:11 +01:00
// this.headers = this.headers.set('Authorization', user.BasicAuthKey);
// this.opts = {
// headers: this.headers,
// }
2024-12-05 19:14:11 +01:00
// let response: any;
2024-12-05 19:14:11 +01:00
// try {
// response = await this.http.post<UserSession>(environment.apiURL + "UserAuthentication/Login", '', this.opts).toPromise();
// console.log('JWT', response)
2023-07-12 15:15:56 +01:00
2024-12-05 19:14:11 +01:00
// if (saveSession) {
// /* this.SetSession(response, user) */
// console.log('teste', response);
2023-07-06 13:13:23 +01:00
2024-12-05 19:14:11 +01:00
// return response
2024-12-05 19:14:11 +01:00
// }
// } catch (error) {
// this.httpErroHandle.httpStatusHandle(error)
// } finally {
// return response
// }
2024-12-05 19:14:11 +01:00
// }
2023-10-20 17:05:39 +01:00
// async UpdateLogin() {}
2023-09-29 16:40:50 +01:00
2023-10-20 17:05:39 +01:00
autologout(expirationDate: number) {
setTimeout(() => {
this.logout();
}, expirationDate)
}
2022-02-02 11:57:11 +01:00
2023-10-20 17:05:39 +01:00
logout() {
2022-02-02 11:57:11 +01:00
2023-10-20 17:05:39 +01:00
SessionStore.setInativity(false)
SessionStore.setUrlBeforeInactivity(this.router.url);
setTimeout(() => {
this.router.navigateByUrl('/', { replaceUrl: true });
}, 100)
2022-02-02 11:57:11 +01:00
2023-10-20 17:05:39 +01:00
}
2022-02-02 11:57:11 +01:00
2023-10-20 17:05:39 +01:00
logoutChat() {
}
2023-09-29 16:40:50 +01:00
2023-10-20 17:05:39 +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
2023-10-20 17:05:39 +01:00
await alert.present();
}
2023-11-29 12:17:52 +01:00
2024-12-05 19:14:11 +01:00
// async logoutUser() {
// this.headers = this.headers.set('Authorization', 'Bearer ' + SessionStore.user.Authorization);
// this.opts = {
// headers: this.headers,
// }
// let response: any;
// try {
// response = await this.http.delete<UserSession>(environment.apiURL + "userauthentication/Logout", this.opts).toPromise();
// SessionStore.user.Authorization = "";
// SessionStore.user.RefreshToken = "";
// } catch (error) {
// this.errorHandler.handleError(error);
// this.httpErroHandle.loginHttpStatusHandle(error)
// captureException(error);
// if(error?.status == 403) {
// console.log('error?.status == 403')
// }
// } finally {
// return response
// }
// }
// refreshToken() {
// return this.http
// .put<any>(environment.apiURL + "UserAuthentication/RefreshToken", {
// refreshToken: SessionStore.user.RefreshToken,
// },)
// .pipe(
// tap((tokens) => {
// console.log(tokens)
// SessionStore.user.Authorization = tokens.Authorization;
// SessionStore.user.RefreshToken = tokens.refreshToken;
// }),
// catchError((error) => {
// this.logoutUser();
// return of(false);
// })
// );
// }
2020-08-06 14:31:07 +01:00
}