Merge branch 'feature/viewer-attachment' of bitbucket.org:equilibriumito/gabinete-digital-fo into feature/viewer-attachment

This commit is contained in:
Peter Maquiran
2024-03-06 15:26:31 +01:00
4 changed files with 152 additions and 182 deletions
+3 -2
View File
@@ -87,7 +87,7 @@ import { LoggingInterceptorService } from './services/logging-interceptor.servic
import { PopupQuestionPipe } from './modals/popup-question.pipe';
import '@teamhive/capacitor-video-recorder';
import { tokenInterceptor } from './interceptors/token.interceptors';
import { ChatTokenInterceptor } from './interceptors/chatToken.interceptor';
import { chatTokenInterceptor } from './interceptors/chatToken.interceptor';
import { InputFilterDirective } from './services/directives/input-filter.directive';
import { VisibilityDirective } from './services/directives/visibility.directive';
@@ -217,8 +217,9 @@ import { FirebaseX } from '@ionic-native/firebase-x/ngx'; */
DocumentViewer,
FFMpeg,
{ provide: HTTP_INTERCEPTORS, useClass: LoggingInterceptorService, multi: true },
chatTokenInterceptor,
tokenInterceptor,
/* ChatTokenInterceptor */
],
bootstrap: [AppComponent],
+143 -123
View File
@@ -1,12 +1,12 @@
import { Injectable } from '@angular/core';
import {
HttpInterceptor,
HttpRequest,
HttpHandler,
HttpEvent,
HttpErrorResponse,
HTTP_INTERCEPTORS,
HttpHeaders,
HttpInterceptor,
HttpRequest,
HttpHandler,
HttpEvent,
HttpErrorResponse,
HTTP_INTERCEPTORS,
HttpHeaders,
} from '@angular/common/http';
import { Observable, throwError, BehaviorSubject } from 'rxjs';
import { catchError, switchMap, filter, take } from 'rxjs/operators';
@@ -15,138 +15,158 @@ import { Router } from '@angular/router';
import { SessionStore } from '../store/session.service';
import { environment } from "src/environments/environment";
import { PermissionService } from '../services/permission.service';
import { NetworkServiceService , ConnectionStatus} from 'src/app/services/network-service.service';
import { NetworkServiceService, ConnectionStatus } from 'src/app/services/network-service.service';
import { RochetChatConnectorService } from 'src/app/services/chat/rochet-chat-connector.service';
@Injectable()
export class ChatTokenInterceptor implements HttpInterceptor {
private isRefreshing = false;
headers: HttpHeaders;
options: any;
private refreshTokenSubject: BehaviorSubject<any> = new BehaviorSubject<any>(
null
);
private excludedDomain = 'https://gdchat-dev.dyndns.info';// Add other domains as needed
constructor(private http: HttpClient, private router: Router,private p: PermissionService,private NetworkServiceService: NetworkServiceService,
private RochetChatConnectorService: RochetChatConnectorService) {}
intercept(
request: HttpRequest<any>,
next: HttpHandler
): Observable<HttpEvent<any>> {
if (this.shouldExcludeDomain(request)) {
return next.handle(request);
}
return next.handle(request).pipe(
catchError((error) => {
if (error instanceof HttpErrorResponse && error.status === 401) {
return this.handle401Error(request, next);
} else {
return throwError(error);
}
})
private isRefreshing = false;
headers: HttpHeaders;
options: any;
private refreshChatTokenSubject: BehaviorSubject<any> = new BehaviorSubject<any>(
null
);
}
private shouldExcludeDomain(request: HttpRequest<any>): boolean {
const url = request.url.toLowerCase();
return !url.includes(this.excludedDomain.toLowerCase());
}
private handle401Error(
request: HttpRequest<any>,
next: HttpHandler
): Observable<HttpEvent<any>> {
if (!this.isRefreshing) {
this.isRefreshing = true;
this.refreshTokenSubject.next(null);
return this.refreshToken().pipe(
switchMap((token: any) => {
this.isRefreshing = false;
let data = {
status: token['status'],
data: {
userId: token['data'].userId,
authToken: token['data'].authToken
}
private excludedDomains = ['Login',environment.apiURL];// Add other domains as needed
constructor(private http: HttpClient, private router: Router, private p: PermissionService, private NetworkServiceService: NetworkServiceService,
private RochetChatConnectorService: RochetChatConnectorService) { }
intercept(
request: HttpRequest<any>,
next: HttpHandler
): Observable<HttpEvent<any>> {
if (this.shouldExcludeDomain(request)) {
return next.handle(request);
}
if (SessionStore.user.Authorization) {
request = this.addToken(request, SessionStore.user.Authorization);
}
SessionStore.user.ChatData = data
SessionStore.save()
this.setheader()
this.refreshTokenSubject.next(token.Authorization);
return next.handle(this.addToken(request, token.Authorization));
})
);
} else {
return this.refreshTokenSubject.pipe(
filter((token) => token != null),
take(1),
switchMap((jwt) => {
return next.handle(this.addToken(request, jwt));
})
);
return next.handle(request).pipe(
catchError((error) => {
if (error instanceof HttpErrorResponse && error.status === 401) {
return this.handle401Error(request, next);
} else {
return throwError(error);
}
})
);
}
}
private addToken(request: HttpRequest<any>, token: string) {
return request.clone({
/* setHeaders: {
Authorization: `Bearer ${token}`,
}, */
});
}
private refreshToken(): Observable<any> {
return this.http
.get<any>(environment.apiURL + 'UserAuthentication/RegenereChatToken', {
/* refreshToken: SessionStore.user.RefreshToken, */
})
.pipe(
catchError((error) => {
// Handle token refresh failure
console.log('ChatToken refresh failed:', error);
return throwError(error);
})
);
}
private shouldExcludeDomain(request: HttpRequest<any>): boolean {
const url = request.url.toLowerCase();
return this.excludedDomains.some((domain) => url.includes(domain.toLowerCase()));
}
private handle401Error(
request: HttpRequest<any>,
next: HttpHandler
): Observable<HttpEvent<any>> {
if (!this.isRefreshing) {
this.isRefreshing = true;
this.refreshChatTokenSubject.next(null);
return this.refreshToken().pipe(
switchMap((token: any) => {
this.isRefreshing = false;
let data = {
status: token['status'],
data: {
userId: token['data'].userId,
authToken: token['data'].authToken
}
}
SessionStore.user.ChatData = data
SessionStore.save()
/* this.setheader() */
setheader() {
try {
if (this.p.userPermission(this.p.permissionList.Chat.access) && SessionStore.user.ChatData) {
this.headers = new HttpHeaders();;
this.refreshChatTokenSubject.next(token.Authorization);
return next.handle(this.addToken(request, token.Authorization));
})
);
} else {
return this.refreshChatTokenSubject.pipe(
filter((token) => token != null),
take(1),
switchMap((jwt) => {
return next.handle(this.addToken(request, jwt));
})
);
}
}
if (this.p.userPermission(this.p.permissionList.Chat.access)) {
//
this.headers = this.headers.set('X-User-Id', SessionStore.user.ChatData.data.userId);
this.headers = this.headers.set('X-Auth-Token', SessionStore.user.ChatData.data.authToken);
this.options = {
headers: this.headers,
};
private addToken(request: HttpRequest<any>, token: string) {
let headers = new HttpHeaders();
console.log('X-User-Id', SessionStore.user.ChatData.data.userId)
console.log('X-Auth-Token', SessionStore.user.ChatData.data.authToken)
headers = headers.set('X-User-Id', SessionStore.user.ChatData.data.userId);
headers = headers.set('X-Auth-Token', SessionStore.user.ChatData.data.authToken);
return request.clone({
setHeaders: {
Authorization: `Bearer ${token}`,
...headers.keys().reduce((acc, key) => ({ ...acc, [key]: headers.get(key) }), {}),
},
});
}
/* private addToken(request: HttpRequest<any>, token: string) {
return request.clone({
setHeaders: {
Authorization: `Bearer ${token}`,
},
});
} */
private refreshToken(): Observable<any> {
return this.http
.get<any>(environment.apiURL + 'UserAuthentication/RegenereChatToken', {
/* refreshToken: SessionStore.user.RefreshToken, */
})
.pipe(
catchError((error) => {
// Handle token refresh failure
console.log('ChatToken refresh failed:', error);
return throwError(error);
})
);
}
setheader() {
try {
if (this.p.userPermission(this.p.permissionList.Chat.access) && SessionStore.user.ChatData) {
this.headers = new HttpHeaders();;
if (this.p.userPermission(this.p.permissionList.Chat.access)) {
//
this.headers = this.headers.set('X-User-Id', SessionStore.user.ChatData.data.userId);
this.headers = this.headers.set('X-Auth-Token', SessionStore.user.ChatData.data.authToken);
this.options = {
headers: this.headers,
};
}
}
} catch (error) {
}
}
} catch (error) {
}
}
}
export const tokenInterceptor = {
export const chatTokenInterceptor = {
provide: HTTP_INTERCEPTORS,
useClass: ChatTokenInterceptor,
multi: true
};
};
+6 -2
View File
@@ -119,9 +119,13 @@ export class TokenInterceptor implements HttpInterceptor {
if (environment.production) {
window.location.pathname = '/auth'
} else {
/* const pathBeforeGoOut = window.location.pathname */
const pathBeforeGoOut = window.location.pathname
console.log('Before auth',window.location.pathname)
this.router.navigateByUrl('/auth', { replaceUrl: true }).then(() =>{
this.httpErrorHandle.httpsSucessMessagge('sessonExpired')
if(pathBeforeGoOut != "/auth") {
this.httpErrorHandle.httpsSucessMessagge('sessonExpired')
}
})
}
return of(false);
-55
View File
@@ -392,61 +392,6 @@ export class ChatService {
async refreshtoken() {
if(this.headers && SessionStore.user.ChatData) {
this.headers = this.headers.set('Authorization', 'Bearer ' + SessionStore.user.Authorization);
let options = {
headers: this.headers
};
try {
let res = await this.http.get(environment.apiURL + 'UserAuthentication/RegenereChatToken', options).toPromise();
let data = {
status: res['status'],
data: {
userId: res['data'].userId,
authToken: res['data'].authToken
}
}
SessionStore.user.ChatData = data
SessionStore.save()
this.setheader()
this.timerEventTriggerDateLastUpdate = new Date();
} catch (error) {
if(this.NetworkServiceService.getCurrentNetworkStatus() == ConnectionStatus.Offline) {
this.RochetChatConnectorService.registerCallback({
requestId: 'refreshtoken',
type: 'reConnect',
funx: async () => {
this.resetTimer();
await this.refreshtoken();
return true
}
})
} else {
if(SessionStore.user.Authorization != '') {
setTimeout(async () => {
this.resetTimer();
await this.refreshtoken();
}, 60000)
}
}
}
} else if(!SessionStore.user.ChatData) {
// do nothing
} else if (!this.headers) {
this.setheader()
this.refreshtoken()
}
}