@tstdl/base 0.93.113 → 0.93.114
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -7,6 +7,12 @@ import type { AuthenticationClientService } from './authentication.service.js';
|
|
|
7
7
|
* @returns A http client middleware.
|
|
8
8
|
*/
|
|
9
9
|
export declare function waitForAuthenticationCredentialsMiddleware(authenticationServiceOrProvider: ValueOrAsyncProvider<AuthenticationClientService>): HttpClientMiddleware;
|
|
10
|
+
/**
|
|
11
|
+
* A http client middleware that logs out the user if a request fails with a 401 Unauthorized error.
|
|
12
|
+
* @param authenticationServiceOrProvider The authentication service or a provider for it.
|
|
13
|
+
* @returns A http client middleware.
|
|
14
|
+
*/
|
|
15
|
+
export declare function logoutOnUnauthorizedMiddleware(authenticationServiceOrProvider: ValueOrAsyncProvider<AuthenticationClientService>): HttpClientMiddleware;
|
|
10
16
|
/**
|
|
11
17
|
* A http client middleware that adds authentication tokens to outgoing requests and extracts them from incoming responses.
|
|
12
18
|
* @param authenticationServiceOrProvider The authentication service or a provider for it.
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { firstValueFrom, timeout } from 'rxjs';
|
|
2
|
+
import { HttpError } from '../../http/index.js';
|
|
2
3
|
import { isDefined } from '../../utils/type-guards.js';
|
|
3
4
|
import { cacheValueOrAsyncProvider } from '../../utils/value-or-provider.js';
|
|
4
5
|
import { dontWaitForValidToken } from '../authentication.api.js';
|
|
@@ -21,6 +22,27 @@ export function waitForAuthenticationCredentialsMiddleware(authenticationService
|
|
|
21
22
|
}
|
|
22
23
|
return waitForAuthenticationCredentialsMiddleware;
|
|
23
24
|
}
|
|
25
|
+
/**
|
|
26
|
+
* A http client middleware that logs out the user if a request fails with a 401 Unauthorized error.
|
|
27
|
+
* @param authenticationServiceOrProvider The authentication service or a provider for it.
|
|
28
|
+
* @returns A http client middleware.
|
|
29
|
+
*/
|
|
30
|
+
export function logoutOnUnauthorizedMiddleware(authenticationServiceOrProvider) {
|
|
31
|
+
const getAuthenticationService = cacheValueOrAsyncProvider(authenticationServiceOrProvider);
|
|
32
|
+
async function logoutOnUnauthorizedMiddleware(_context, next) {
|
|
33
|
+
try {
|
|
34
|
+
await next();
|
|
35
|
+
}
|
|
36
|
+
catch (error) {
|
|
37
|
+
if ((error instanceof HttpError) && (error.response?.statusCode == 401)) {
|
|
38
|
+
const authenticationService = await getAuthenticationService();
|
|
39
|
+
await authenticationService.logout();
|
|
40
|
+
}
|
|
41
|
+
throw error;
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
return logoutOnUnauthorizedMiddleware;
|
|
45
|
+
}
|
|
24
46
|
/**
|
|
25
47
|
* A http client middleware that adds authentication tokens to outgoing requests and extracts them from incoming responses.
|
|
26
48
|
* @param authenticationServiceOrProvider The authentication service or a provider for it.
|
|
@@ -3,7 +3,7 @@ import { getCurrentInjector } from '../../injector/inject.js';
|
|
|
3
3
|
import { Injector } from '../../injector/injector.js';
|
|
4
4
|
import { isDefined } from '../../utils/type-guards.js';
|
|
5
5
|
import { AuthenticationClientService } from './authentication.service.js';
|
|
6
|
-
import { authenticationMiddleware, waitForAuthenticationCredentialsMiddleware } from './http-client.middleware.js';
|
|
6
|
+
import { authenticationMiddleware, logoutOnUnauthorizedMiddleware, waitForAuthenticationCredentialsMiddleware } from './http-client.middleware.js';
|
|
7
7
|
import { AUTHENTICATION_API_CLIENT, INITIAL_AUTHENTICATION_DATA } from './tokens.js';
|
|
8
8
|
/**
|
|
9
9
|
* Configures authentication client services.
|
|
@@ -24,6 +24,12 @@ export function configureAuthenticationClient(config, injector = getCurrentInjec
|
|
|
24
24
|
return waitForAuthenticationCredentialsMiddleware(authenticationService);
|
|
25
25
|
},
|
|
26
26
|
}, { multi: true });
|
|
27
|
+
(injector ?? Injector).register(HTTP_CLIENT_MIDDLEWARE, {
|
|
28
|
+
useFactory(_, context) {
|
|
29
|
+
const authenticationService = context.resolve(AuthenticationClientService, undefined, { forwardRef: true, forwardRefTypeHint: 'object' });
|
|
30
|
+
return logoutOnUnauthorizedMiddleware(authenticationService);
|
|
31
|
+
},
|
|
32
|
+
}, { multi: true });
|
|
27
33
|
(injector ?? Injector).register(HTTP_CLIENT_MIDDLEWARE, {
|
|
28
34
|
useFactory(_, context) {
|
|
29
35
|
const authenticationService = context.resolve(AuthenticationClientService, undefined, { forwardRef: true, forwardRefTypeHint: 'object' });
|