fix(pdf): improve PDF blob handling and improve viewer destruction logic (#506)
* fix(pdf): improve PDF blob handling and improve viewer destruction logic * fix(pdf): PDF annotation saving logic and improve responsive design for mobile * fix(pdf): improve PDF fetching (once again) and caching logic to improve performance and reduce network requests * fix(pdf): hide footer and notification elements in PDF viewer * fix(pdf): improve loading behavior and improve annotation persistence logic * fix(pdf): improve PDF annotation persistence with retry mechanism and logging * fix(pdf): improve PDF annotation saving with raw fetch and improved error handling * fix(pdf): cap highlight opacity and adjust rendering for small viewports
This commit is contained in:
@@ -1,21 +1,26 @@
|
||||
import {HttpErrorResponse, HttpEvent, HttpHandlerFn, HttpInterceptorFn, HttpRequest} from '@angular/common/http';
|
||||
import {inject} from '@angular/core';
|
||||
import {catchError, filter, switchMap, take} from 'rxjs/operators';
|
||||
import {BehaviorSubject, Observable, throwError} from 'rxjs';
|
||||
import {BehaviorSubject, Observable, throwError, defer} from 'rxjs';
|
||||
import {AuthService} from '../../shared/service/auth.service';
|
||||
import {API_CONFIG} from '../config/api-config';
|
||||
|
||||
let isRefreshing = false;
|
||||
const refreshTokenSubject = new BehaviorSubject<string | null>(null);
|
||||
|
||||
export const AuthInterceptorService: HttpInterceptorFn = (req, next: HttpHandlerFn) => {
|
||||
const authService = inject(AuthService);
|
||||
|
||||
const token = authService.getInternalAccessToken();
|
||||
const isApiRequest = req.url.startsWith(`${API_CONFIG.BASE_URL}/api/`);
|
||||
const isAuthRequest = req.url.startsWith(`${API_CONFIG.BASE_URL}/api/v1/auth/`);
|
||||
const hasAuthHeader = req.headers.has('Authorization');
|
||||
|
||||
const authReq = (token && isApiRequest) ? req.clone({ setHeaders: { Authorization: `Bearer ${token}` } }) : req;
|
||||
const authReq = (token && isApiRequest && !isAuthRequest && !hasAuthHeader) ? req.clone({ setHeaders: { Authorization: `Bearer ${token}` } }) : req;
|
||||
|
||||
return next(authReq).pipe(
|
||||
catchError((error: HttpErrorResponse) => {
|
||||
if (error.status === 401) {
|
||||
if (error.status === 401 && !isAuthRequest) {
|
||||
return handle401Error(authService, authReq, next);
|
||||
}
|
||||
return throwError(() => error);
|
||||
@@ -23,43 +28,42 @@ export const AuthInterceptorService: HttpInterceptorFn = (req, next: HttpHandler
|
||||
);
|
||||
};
|
||||
|
||||
let isRefreshing = false;
|
||||
const refreshTokenSubject = new BehaviorSubject<string | null>(null);
|
||||
|
||||
function handle401Error(authService: AuthService, request: HttpRequest<unknown>, next: HttpHandlerFn): Observable<HttpEvent<unknown>> {
|
||||
if (!isRefreshing) {
|
||||
isRefreshing = true;
|
||||
refreshTokenSubject.next(null);
|
||||
return defer(() => {
|
||||
if (!isRefreshing) {
|
||||
isRefreshing = true;
|
||||
refreshTokenSubject.next(null);
|
||||
|
||||
return authService.internalRefreshToken().pipe(
|
||||
switchMap(response => {
|
||||
isRefreshing = false;
|
||||
const { accessToken, refreshToken } = response;
|
||||
if (accessToken && refreshToken) {
|
||||
authService.saveInternalTokens(accessToken, refreshToken);
|
||||
refreshTokenSubject.next(accessToken);
|
||||
}
|
||||
return next(request.clone({
|
||||
setHeaders: { Authorization: `Bearer ${accessToken}` }
|
||||
}));
|
||||
}),
|
||||
catchError(err => {
|
||||
isRefreshing = false;
|
||||
forceLogout(authService);
|
||||
return throwError(() => err);
|
||||
})
|
||||
return authService.internalRefreshToken().pipe(
|
||||
switchMap(response => {
|
||||
isRefreshing = false;
|
||||
const { accessToken, refreshToken } = response;
|
||||
if (accessToken && refreshToken) {
|
||||
authService.saveInternalTokens(accessToken, refreshToken);
|
||||
refreshTokenSubject.next(accessToken);
|
||||
}
|
||||
return next(request.clone({
|
||||
setHeaders: { Authorization: `Bearer ${accessToken}` }
|
||||
}));
|
||||
}),
|
||||
catchError(err => {
|
||||
isRefreshing = false;
|
||||
forceLogout(authService);
|
||||
return throwError(() => err);
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
return refreshTokenSubject.pipe(
|
||||
filter(token => token !== null),
|
||||
take(1),
|
||||
switchMap(token =>
|
||||
next(request.clone({
|
||||
setHeaders: { Authorization: `Bearer ${token}` }
|
||||
}))
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
return refreshTokenSubject.pipe(
|
||||
filter(token => token !== null),
|
||||
take(1),
|
||||
switchMap(token =>
|
||||
next(request.clone({
|
||||
setHeaders: { Authorization: `Bearer ${token}` }
|
||||
}))
|
||||
)
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
function forceLogout(authService: AuthService): void {
|
||||
|
||||
Reference in New Issue
Block a user