mirror of
https://code.equilibrium.co.ao/ITO/doneit-web.git
synced 2026-04-20 05:16:07 +00:00
fix
This commit is contained in:
@@ -0,0 +1,37 @@
|
||||
import { Injectable } from '@angular/core';
|
||||
import { HTTP_INTERCEPTORS, HttpEvent, HttpHandler, HttpInterceptor, HttpRequest, HttpResponse } from '@angular/common/http';
|
||||
import { Observable } from 'rxjs';
|
||||
import { tap } from 'rxjs/operators';
|
||||
import { meter, RequestCounter } from '../../../services/monitoring/opentelemetry/matrix';
|
||||
|
||||
|
||||
@Injectable()
|
||||
export class MetricsInterceptor implements HttpInterceptor {
|
||||
intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
|
||||
return next.handle(req).pipe(
|
||||
tap(event => {
|
||||
if (event instanceof HttpResponse) {
|
||||
// Capture the status code and check protocol
|
||||
if (req.method !== 'GET' && !req.urlWithParams.includes('metrics')) {
|
||||
|
||||
console.log('response', event.body)
|
||||
const path = req.urlWithParams;
|
||||
const url = new URL(path);
|
||||
if (window.location.protocol !== 'https:') {
|
||||
let attributes = { path: url.pathname, method: req.method };
|
||||
const statusCode = event.status;
|
||||
const extendedAttributes = { ...attributes, status: statusCode };
|
||||
RequestCounter.add(1, extendedAttributes);
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
export const metricsInterceptor = {
|
||||
provide: HTTP_INTERCEPTORS,
|
||||
useClass: MetricsInterceptor,
|
||||
multi: true
|
||||
};
|
||||
@@ -0,0 +1,167 @@
|
||||
import { Injectable } from "@angular/core";
|
||||
import {
|
||||
HttpRequest,
|
||||
HttpHandler,
|
||||
HttpEvent,
|
||||
HttpInterceptor,
|
||||
HttpErrorResponse,
|
||||
HTTP_INTERCEPTORS,
|
||||
HttpClient,
|
||||
} from "@angular/common/http";
|
||||
import { Observable, throwError, BehaviorSubject, of } from "rxjs";
|
||||
import { catchError, filter, take, switchMap, tap } from "rxjs/operators";
|
||||
import { SessionStore } from '../../../store/session.service';
|
||||
import { environment } from "src/environments/environment";
|
||||
import { Router } from "@angular/router";
|
||||
import { HttpErrorHandle } from 'src/app/services/http-error-handle.service';
|
||||
import { Platform } from '@ionic/angular';
|
||||
import { UserLoginOutputResponse } from "../../../core/user/repository/user-remote-repository";
|
||||
import { UserLoginMapper } from "../../../core/user/mapper/user-login";
|
||||
import { UserSession } from "../../../models/user.model";
|
||||
|
||||
@Injectable()
|
||||
export class TokenInterceptor implements HttpInterceptor {
|
||||
private isRefreshing = false;
|
||||
private refreshTokenSubject: BehaviorSubject<any> = new BehaviorSubject<any>(
|
||||
null
|
||||
);
|
||||
|
||||
private excludedDomains = [ 'Login', environment.apiChatUrl, 'http://localhost:8019']; // Add the domains you want to exclude
|
||||
|
||||
constructor(
|
||||
private http: HttpClient,
|
||||
private router: Router,
|
||||
private httpErrorHandle: HttpErrorHandle,
|
||||
private platform: Platform) { }
|
||||
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
return next.handle(request).pipe(
|
||||
|
||||
catchError((error) => {
|
||||
console.log('interceptor ',error)
|
||||
if(error.url.includes('/Users/RefreshToken') && error.status === 401) {
|
||||
console.log("refresh token error11",error)
|
||||
return throwError(error);
|
||||
}
|
||||
else if (error instanceof HttpErrorResponse && error.status === 401) {
|
||||
return this.handle401Error(request, next);
|
||||
} else if (error.url.includes('https://gdapi-dev.dyndns.info/stage/api/v2') && error.status === 0){
|
||||
return this.handle401Error(request, next);
|
||||
} else {
|
||||
return throwError(error);
|
||||
}
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
private shouldExcludeDomain(request: HttpRequest<any>): boolean {
|
||||
const url = request.url.toLowerCase();
|
||||
return this.excludedDomains.some((domain) => url.includes(domain.toLowerCase()));
|
||||
}
|
||||
|
||||
private addToken(request: HttpRequest<any>, token: string) {
|
||||
|
||||
return request.clone({
|
||||
setHeaders: {
|
||||
//'Access-Control-Allow-Origin': '*',
|
||||
Authorization: `Bearer ${token}`,
|
||||
},
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
private handle401Error(request: HttpRequest<any>, next: HttpHandler) {
|
||||
// if not getting the new token yet
|
||||
if (!this.isRefreshing) {
|
||||
this.isRefreshing = true;
|
||||
this.refreshTokenSubject.next(null);
|
||||
|
||||
return this.refreshToken().pipe(
|
||||
switchMap((data: UserLoginOutputResponse) => {
|
||||
this.isRefreshing = false;
|
||||
this.refreshTokenSubject.next(data?.data?.authorization);
|
||||
return next.handle(this.addToken(request, data?.data?.authorization));
|
||||
})
|
||||
);
|
||||
} else {
|
||||
// get new token
|
||||
return this.refreshTokenSubject.pipe(
|
||||
filter((token) => token != null),
|
||||
take(1),
|
||||
switchMap((jwt) => {
|
||||
return next.handle(this.addToken(request, jwt));
|
||||
})
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
//this method refresh token is declared here temporary beacouse a circular error
|
||||
refreshToken() {
|
||||
let channelId;
|
||||
if ( this.platform.is('desktop') || this.platform.is("mobileweb")){
|
||||
channelId = 2
|
||||
} else {
|
||||
channelId = 1
|
||||
}
|
||||
|
||||
return this.http
|
||||
.post<UserLoginOutputResponse>('https://gdapi-dev.dyndns.info/stage/api/v2/Users/RefreshToken', {
|
||||
authorization: SessionStore.user.Authorization,
|
||||
refreshToken: SessionStore.user.RefreshToken,
|
||||
channelId
|
||||
},)
|
||||
.pipe(
|
||||
tap((data) => {
|
||||
|
||||
SessionStore.user.Authorization = data.data.authorization;
|
||||
SessionStore.user.RefreshToken = data.data.refreshToken;
|
||||
//const userData = UserLoginMapper.toDomainData(data)
|
||||
//const session: UserSession = Object.assign(SessionStore.user, userData)
|
||||
//SessionStore.reset(session)ta
|
||||
|
||||
}),
|
||||
catchError((error) => {
|
||||
console.log("refresh token error",error)
|
||||
// SessionStore.user.Authorization = SessionStore.user.Authorization;
|
||||
// SessionStore.user.RefreshToken = SessionStore.user.RefreshToken;
|
||||
SessionStore.setInativity(false)
|
||||
/* SessionStore.setUrlBeforeInactivity(this.router.url); */
|
||||
|
||||
if (environment.production) {
|
||||
window.location.pathname = '/auth'
|
||||
} else {
|
||||
const pathBeforeGoOut = window.location.pathname
|
||||
console.log('Before auth',window.location.pathname)
|
||||
this.router.navigateByUrl('/auth', { replaceUrl: true }).then(() =>{
|
||||
if(pathBeforeGoOut != "/auth") {
|
||||
this.httpErrorHandle.httpsSucessMessagge('sessonExpired')
|
||||
}
|
||||
|
||||
})
|
||||
}
|
||||
return of(false);
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
export const tokenInterceptor = {
|
||||
provide: HTTP_INTERCEPTORS,
|
||||
useClass: TokenInterceptor,
|
||||
multi: true
|
||||
};
|
||||
Reference in New Issue
Block a user