mirror of
https://github.com/paperless-ngx/paperless-ngx.git
synced 2026-09-25 02:40:32 +00:00
# Conflicts: # docs/setup.md # src-ui/src/main.ts # src/documents/tests/test_api_bulk_edit.py # src/documents/tests/test_api_custom_fields.py # src/documents/tests/test_api_search.py # src/documents/tests/test_api_status.py # src/documents/tests/test_workflows.py # src/paperless_mail/tests/test_api.py
38 lines
977 B
TypeScript
38 lines
977 B
TypeScript
import {
|
|
HttpErrorResponse,
|
|
HttpEvent,
|
|
HttpHandlerFn,
|
|
HttpInterceptorFn,
|
|
HttpRequest,
|
|
} from '@angular/common/http'
|
|
import { catchError, Observable, throwError } from 'rxjs'
|
|
import { locationReload } from '../utils/navigation'
|
|
|
|
export const createAuthExpiryInterceptor = (): HttpInterceptorFn => {
|
|
let lastReloadAttempt = Number.NEGATIVE_INFINITY
|
|
|
|
return (
|
|
request: HttpRequest<unknown>,
|
|
next: HttpHandlerFn
|
|
): Observable<HttpEvent<unknown>> =>
|
|
next(request).pipe(
|
|
catchError((error: unknown) => {
|
|
if (
|
|
error instanceof HttpErrorResponse &&
|
|
error.status === 401 &&
|
|
request.url.includes('/api/')
|
|
) {
|
|
const now = Date.now()
|
|
if (now - lastReloadAttempt >= 2000) {
|
|
lastReloadAttempt = now
|
|
locationReload()
|
|
}
|
|
}
|
|
|
|
return throwError(() => error)
|
|
})
|
|
)
|
|
}
|
|
|
|
export const withAuthExpiryInterceptor = createAuthExpiryInterceptor()
|