@edsis/angular 21.3.13 → 21.3.15
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.
- package/fesm2022/edsis-angular-block-shell.mjs +32 -27
- package/fesm2022/edsis-angular-block-shell.mjs.map +1 -1
- package/fesm2022/edsis-angular-module-auth.mjs +278 -35
- package/fesm2022/edsis-angular-module-auth.mjs.map +1 -1
- package/package.json +2 -2
- package/types/edsis-angular-block-shell.d.ts +4 -1
- package/types/edsis-angular-module-auth.d.ts +14 -6
|
@@ -1,12 +1,13 @@
|
|
|
1
1
|
import * as i0 from '@angular/core';
|
|
2
|
-
import { InjectionToken, inject, makeEnvironmentProviders, ChangeDetectionStrategy, Component, DestroyRef, signal, computed, input, effect } from '@angular/core';
|
|
3
|
-
import { HttpContextToken, HttpErrorResponse, provideHttpClient, withFetch, withInterceptors } from '@angular/common/http';
|
|
4
|
-
import { ApiConfiguration, provideApiConfiguration } from '@edsis/sdk/shared';
|
|
2
|
+
import { InjectionToken, inject, makeEnvironmentProviders, provideEnvironmentInitializer, ChangeDetectionStrategy, Component, DestroyRef, signal, computed, input, effect } from '@angular/core';
|
|
5
3
|
import { DOCUMENT, Location } from '@angular/common';
|
|
6
|
-
import {
|
|
4
|
+
import { HttpErrorResponse, HttpContextToken, provideHttpClient, withFetch, withInterceptors, HttpContext } from '@angular/common/http';
|
|
5
|
+
import { Router, RouterOutlet, RouterLink, ActivatedRoute } from '@angular/router';
|
|
6
|
+
import { AuthSessionService, AuthMagicLinkService } from '@edsis/sdk/auth';
|
|
7
|
+
import { ApiConfiguration, provideApiConfiguration } from '@edsis/sdk/shared';
|
|
8
|
+
import { UserProfileService } from '@edsis/sdk/user';
|
|
7
9
|
import { firstValueFrom, throwError, from, startWith, finalize } from 'rxjs';
|
|
8
10
|
import { switchMap, catchError } from 'rxjs/operators';
|
|
9
|
-
import { Router, RouterOutlet, RouterLink, ActivatedRoute } from '@angular/router';
|
|
10
11
|
import { UiLayoutComponent, UiLayoutFluidComponent } from '@edsis/ui/layout';
|
|
11
12
|
import { toSignal, takeUntilDestroyed } from '@angular/core/rxjs-interop';
|
|
12
13
|
import * as i1 from '@angular/forms';
|
|
@@ -55,22 +56,135 @@ function normalizePath(value, fallback) {
|
|
|
55
56
|
|
|
56
57
|
const AUTH_SESSION_STORAGE_KEYS = ['access_token', 'user_id', 'role', 'name'];
|
|
57
58
|
const AUTH_SESSION_METADATA_STORAGE_KEYS = ['access_token_trusted'];
|
|
59
|
+
const AUTH_SESSION_PERSISTENT_HINT_KEY = 'edsis_auth_refresh_hint';
|
|
58
60
|
const AUTH_SESSION_CHANGE_EVENT = 'edsis-auth-session-change';
|
|
61
|
+
const AUTH_SESSION_PEER_CHANNEL_NAME = 'edsis-auth-session-peer';
|
|
59
62
|
const EMPTY_SESSION_FINGERPRINT = '__empty_session__';
|
|
60
63
|
const JWT_PART_COUNT = 3;
|
|
61
64
|
const TOKEN_EXPIRY_SKEW_MS = 30_000;
|
|
65
|
+
const PEER_SESSION_REQUEST_TIMEOUT_MS = 150;
|
|
66
|
+
const TRUSTED_OPAQUE_TOKEN_REVALIDATION_MS = 90_000;
|
|
67
|
+
const AUTH_SESSION_MONITOR_INTERVAL_MS = 10_000;
|
|
62
68
|
let validatedAccessToken = '';
|
|
63
69
|
let validatedUntilEpochMs = 0;
|
|
64
70
|
let failedSessionFingerprint = null;
|
|
65
71
|
let pendingSessionValidation = null;
|
|
72
|
+
let sessionPeerChannel = null;
|
|
73
|
+
let sessionPeerChannelOwner = null;
|
|
74
|
+
let sessionPeerId = '';
|
|
75
|
+
let sessionMonitorCleanup = null;
|
|
76
|
+
let sessionMonitorOwner = null;
|
|
77
|
+
function clearSessionMonitor() {
|
|
78
|
+
sessionMonitorCleanup?.();
|
|
79
|
+
sessionMonitorCleanup = null;
|
|
80
|
+
sessionMonitorOwner = null;
|
|
81
|
+
}
|
|
66
82
|
function ɵresetAuthSessionModuleState() {
|
|
67
83
|
clearValidatedSession();
|
|
68
84
|
clearFailedSessionFingerprint();
|
|
69
85
|
pendingSessionValidation = null;
|
|
86
|
+
clearSessionMonitor();
|
|
87
|
+
sessionPeerChannel?.close();
|
|
88
|
+
sessionPeerChannel = null;
|
|
89
|
+
sessionPeerChannelOwner = null;
|
|
90
|
+
sessionPeerId = '';
|
|
70
91
|
}
|
|
71
92
|
function getSessionStorage(documentRef) {
|
|
72
93
|
return documentRef.defaultView?.sessionStorage ?? null;
|
|
73
94
|
}
|
|
95
|
+
function getLocalStorage(documentRef) {
|
|
96
|
+
return documentRef.defaultView?.localStorage ?? null;
|
|
97
|
+
}
|
|
98
|
+
function createSessionPeerId(windowRef) {
|
|
99
|
+
return windowRef.crypto?.randomUUID?.() ?? `${Date.now()}-${Math.random().toString(16).slice(2)}`;
|
|
100
|
+
}
|
|
101
|
+
function isSessionPeerResponseMessage(message) {
|
|
102
|
+
return message.type === 'session-response';
|
|
103
|
+
}
|
|
104
|
+
function buildStoredSessionData(documentRef) {
|
|
105
|
+
const session = readStoredSession(documentRef);
|
|
106
|
+
if (!session) {
|
|
107
|
+
return null;
|
|
108
|
+
}
|
|
109
|
+
return {
|
|
110
|
+
access_token: session.accessToken,
|
|
111
|
+
name: session.name,
|
|
112
|
+
role: session.role,
|
|
113
|
+
user_id: session.userId,
|
|
114
|
+
};
|
|
115
|
+
}
|
|
116
|
+
function ensureSessionPeerChannel(documentRef) {
|
|
117
|
+
const windowRef = documentRef.defaultView;
|
|
118
|
+
if (!windowRef?.BroadcastChannel) {
|
|
119
|
+
return null;
|
|
120
|
+
}
|
|
121
|
+
if (sessionPeerChannel && sessionPeerChannelOwner === windowRef) {
|
|
122
|
+
return sessionPeerChannel;
|
|
123
|
+
}
|
|
124
|
+
sessionPeerChannel?.close();
|
|
125
|
+
sessionPeerId = createSessionPeerId(windowRef);
|
|
126
|
+
sessionPeerChannelOwner = windowRef;
|
|
127
|
+
sessionPeerChannel = new windowRef.BroadcastChannel(AUTH_SESSION_PEER_CHANNEL_NAME);
|
|
128
|
+
sessionPeerChannel.addEventListener('message', (event) => {
|
|
129
|
+
const message = event.data;
|
|
130
|
+
if (message.type !== 'session-request' || message.senderId === sessionPeerId) {
|
|
131
|
+
return;
|
|
132
|
+
}
|
|
133
|
+
const sessionData = buildStoredSessionData(documentRef);
|
|
134
|
+
if (!sessionData) {
|
|
135
|
+
return;
|
|
136
|
+
}
|
|
137
|
+
sessionPeerChannel?.postMessage({
|
|
138
|
+
type: 'session-response',
|
|
139
|
+
requestId: message.requestId,
|
|
140
|
+
senderId: sessionPeerId,
|
|
141
|
+
sessionData,
|
|
142
|
+
});
|
|
143
|
+
});
|
|
144
|
+
return sessionPeerChannel;
|
|
145
|
+
}
|
|
146
|
+
async function restoreSessionFromPeer(documentRef) {
|
|
147
|
+
const windowRef = documentRef.defaultView;
|
|
148
|
+
const channel = ensureSessionPeerChannel(documentRef);
|
|
149
|
+
if (!windowRef || !channel || !sessionPeerId) {
|
|
150
|
+
return false;
|
|
151
|
+
}
|
|
152
|
+
const requestId = createSessionPeerId(windowRef);
|
|
153
|
+
return new Promise((resolve) => {
|
|
154
|
+
let settled = false;
|
|
155
|
+
let timeoutId = 0;
|
|
156
|
+
const finish = (result) => {
|
|
157
|
+
if (settled) {
|
|
158
|
+
return;
|
|
159
|
+
}
|
|
160
|
+
settled = true;
|
|
161
|
+
windowRef.clearTimeout(timeoutId);
|
|
162
|
+
channel.removeEventListener('message', handleMessage);
|
|
163
|
+
resolve(result);
|
|
164
|
+
};
|
|
165
|
+
const handleMessage = (event) => {
|
|
166
|
+
const message = event.data;
|
|
167
|
+
if (!isSessionPeerResponseMessage(message) ||
|
|
168
|
+
message.senderId === sessionPeerId ||
|
|
169
|
+
message.requestId !== requestId) {
|
|
170
|
+
return;
|
|
171
|
+
}
|
|
172
|
+
if (!readSessionDataValue(message.sessionData, 'access_token')) {
|
|
173
|
+
finish(false);
|
|
174
|
+
return;
|
|
175
|
+
}
|
|
176
|
+
persistSession(documentRef, message.sessionData);
|
|
177
|
+
finish(true);
|
|
178
|
+
};
|
|
179
|
+
timeoutId = windowRef.setTimeout(() => finish(false), PEER_SESSION_REQUEST_TIMEOUT_MS);
|
|
180
|
+
channel.addEventListener('message', handleMessage);
|
|
181
|
+
channel.postMessage({
|
|
182
|
+
type: 'session-request',
|
|
183
|
+
requestId,
|
|
184
|
+
senderId: sessionPeerId,
|
|
185
|
+
});
|
|
186
|
+
});
|
|
187
|
+
}
|
|
74
188
|
function readSessionStorageValue(documentRef, storageKey) {
|
|
75
189
|
return getSessionStorage(documentRef)?.getItem(storageKey)?.trim() ?? '';
|
|
76
190
|
}
|
|
@@ -100,6 +214,20 @@ function markFailedSessionFingerprint(documentRef) {
|
|
|
100
214
|
function readTrustedAccessToken(documentRef) {
|
|
101
215
|
return readSessionStorageValue(documentRef, 'access_token_trusted');
|
|
102
216
|
}
|
|
217
|
+
function hasRefreshCandidate(documentRef) {
|
|
218
|
+
return Boolean(readStoredAccessToken(documentRef) ||
|
|
219
|
+
readTrustedAccessToken(documentRef) ||
|
|
220
|
+
getLocalStorage(documentRef)?.getItem(AUTH_SESSION_PERSISTENT_HINT_KEY));
|
|
221
|
+
}
|
|
222
|
+
function shouldClearSessionAfterRefreshFailure(error) {
|
|
223
|
+
if (error instanceof HttpErrorResponse) {
|
|
224
|
+
return error.status === 401 || error.status === 403;
|
|
225
|
+
}
|
|
226
|
+
if (error instanceof Error) {
|
|
227
|
+
return /invalid refresh token|unauthorized|forbidden/iu.test(error.message);
|
|
228
|
+
}
|
|
229
|
+
return false;
|
|
230
|
+
}
|
|
103
231
|
function getTokenExpiryEpochMs(documentRef, accessToken) {
|
|
104
232
|
const windowRef = documentRef.defaultView;
|
|
105
233
|
const tokenParts = accessToken.split('.');
|
|
@@ -129,7 +257,7 @@ function markValidatedSession(documentRef) {
|
|
|
129
257
|
return;
|
|
130
258
|
}
|
|
131
259
|
validatedAccessToken = accessToken;
|
|
132
|
-
validatedUntilEpochMs =
|
|
260
|
+
validatedUntilEpochMs = Date.now() + TRUSTED_OPAQUE_TOKEN_REVALIDATION_MS;
|
|
133
261
|
return;
|
|
134
262
|
}
|
|
135
263
|
validatedAccessToken = accessToken;
|
|
@@ -172,6 +300,7 @@ function readStoredSession(documentRef) {
|
|
|
172
300
|
};
|
|
173
301
|
}
|
|
174
302
|
function persistSession(documentRef, sessionData) {
|
|
303
|
+
ensureSessionPeerChannel(documentRef);
|
|
175
304
|
const sessionStorageRef = getSessionStorage(documentRef);
|
|
176
305
|
if (!sessionStorageRef) {
|
|
177
306
|
clearValidatedSession();
|
|
@@ -186,6 +315,7 @@ function persistSession(documentRef, sessionData) {
|
|
|
186
315
|
for (const storageKey of AUTH_SESSION_METADATA_STORAGE_KEYS) {
|
|
187
316
|
sessionStorageRef.removeItem(storageKey);
|
|
188
317
|
}
|
|
318
|
+
getLocalStorage(documentRef)?.removeItem(AUTH_SESSION_PERSISTENT_HINT_KEY);
|
|
189
319
|
clearValidatedSession();
|
|
190
320
|
clearFailedSessionFingerprint();
|
|
191
321
|
notifySessionChange(documentRef);
|
|
@@ -193,6 +323,7 @@ function persistSession(documentRef, sessionData) {
|
|
|
193
323
|
}
|
|
194
324
|
sessionStorageRef.setItem('access_token', nextAccessToken);
|
|
195
325
|
sessionStorageRef.setItem('access_token_trusted', nextAccessToken);
|
|
326
|
+
getLocalStorage(documentRef)?.setItem(AUTH_SESSION_PERSISTENT_HINT_KEY, '1');
|
|
196
327
|
for (const storageKey of AUTH_SESSION_STORAGE_KEYS.slice(1)) {
|
|
197
328
|
if (!sessionData || !Object.hasOwn(sessionData, storageKey)) {
|
|
198
329
|
continue;
|
|
@@ -217,29 +348,86 @@ function clearStoredSession(documentRef, options) {
|
|
|
217
348
|
for (const storageKey of AUTH_SESSION_METADATA_STORAGE_KEYS) {
|
|
218
349
|
sessionStorageRef?.removeItem(storageKey);
|
|
219
350
|
}
|
|
351
|
+
getLocalStorage(documentRef)?.removeItem(AUTH_SESSION_PERSISTENT_HINT_KEY);
|
|
220
352
|
clearValidatedSession();
|
|
221
353
|
if (!options?.preserveFailedSessionFingerprint) {
|
|
222
354
|
clearFailedSessionFingerprint();
|
|
223
355
|
}
|
|
224
356
|
notifySessionChange(documentRef);
|
|
225
357
|
}
|
|
226
|
-
|
|
358
|
+
function startAuthSessionMonitor(documentRef, authSessionService, options) {
|
|
359
|
+
const windowRef = documentRef.defaultView;
|
|
360
|
+
if (!windowRef) {
|
|
361
|
+
return;
|
|
362
|
+
}
|
|
363
|
+
if (sessionMonitorCleanup && sessionMonitorOwner === windowRef) {
|
|
364
|
+
return;
|
|
365
|
+
}
|
|
366
|
+
clearSessionMonitor();
|
|
367
|
+
let disposed = false;
|
|
368
|
+
const runValidation = async () => {
|
|
369
|
+
const hadRefreshCandidate = hasRefreshCandidate(documentRef);
|
|
370
|
+
if (!hadRefreshCandidate) {
|
|
371
|
+
return;
|
|
372
|
+
}
|
|
373
|
+
const isSessionValid = options?.validateSession
|
|
374
|
+
? await options.validateSession()
|
|
375
|
+
: await validateStoredSession(documentRef, authSessionService, { forceRefresh: true });
|
|
376
|
+
if (!disposed && !isSessionValid && hadRefreshCandidate) {
|
|
377
|
+
options?.onSessionInvalid?.();
|
|
378
|
+
}
|
|
379
|
+
};
|
|
380
|
+
const handleFocus = () => {
|
|
381
|
+
void runValidation();
|
|
382
|
+
};
|
|
383
|
+
const handleVisibilityChange = () => {
|
|
384
|
+
if (documentRef.visibilityState === 'visible') {
|
|
385
|
+
void runValidation();
|
|
386
|
+
}
|
|
387
|
+
};
|
|
388
|
+
const intervalId = windowRef.setInterval(handleFocus, AUTH_SESSION_MONITOR_INTERVAL_MS);
|
|
389
|
+
windowRef.addEventListener('focus', handleFocus);
|
|
390
|
+
documentRef.addEventListener('visibilitychange', handleVisibilityChange);
|
|
391
|
+
sessionMonitorOwner = windowRef;
|
|
392
|
+
sessionMonitorCleanup = () => {
|
|
393
|
+
disposed = true;
|
|
394
|
+
windowRef.clearInterval(intervalId);
|
|
395
|
+
windowRef.removeEventListener('focus', handleFocus);
|
|
396
|
+
documentRef.removeEventListener('visibilitychange', handleVisibilityChange);
|
|
397
|
+
};
|
|
398
|
+
}
|
|
399
|
+
async function validateStoredSession(documentRef, authSessionService, options) {
|
|
400
|
+
ensureSessionPeerChannel(documentRef);
|
|
227
401
|
if (!options?.forceRefresh && hasValidatedSession(documentRef)) {
|
|
228
402
|
return true;
|
|
229
403
|
}
|
|
230
|
-
if (hasFailedSessionFingerprint(documentRef)) {
|
|
231
|
-
return false;
|
|
232
|
-
}
|
|
233
404
|
if (pendingSessionValidation) {
|
|
234
405
|
return pendingSessionValidation;
|
|
235
406
|
}
|
|
236
407
|
pendingSessionValidation = (async () => {
|
|
237
408
|
try {
|
|
238
|
-
|
|
409
|
+
if (!options?.forceRefresh && !hasStoredSession(documentRef) && (await restoreSessionFromPeer(documentRef))) {
|
|
410
|
+
return true;
|
|
411
|
+
}
|
|
412
|
+
if (!options?.forceRefresh && hasValidatedSession(documentRef)) {
|
|
413
|
+
return true;
|
|
414
|
+
}
|
|
415
|
+
if (!options?.forceRefresh && !hasRefreshCandidate(documentRef)) {
|
|
416
|
+
clearValidatedSession();
|
|
417
|
+
return false;
|
|
418
|
+
}
|
|
419
|
+
if (hasFailedSessionFingerprint(documentRef)) {
|
|
420
|
+
return false;
|
|
421
|
+
}
|
|
422
|
+
const response = await firstValueFrom(authSessionService.refreshAuthToken());
|
|
239
423
|
persistSession(documentRef, response.data);
|
|
240
424
|
return hasValidatedSession(documentRef);
|
|
241
425
|
}
|
|
242
|
-
catch {
|
|
426
|
+
catch (error) {
|
|
427
|
+
if (!shouldClearSessionAfterRefreshFailure(error)) {
|
|
428
|
+
clearValidatedSession();
|
|
429
|
+
return hasStoredSession(documentRef);
|
|
430
|
+
}
|
|
243
431
|
clearStoredSession(documentRef, { preserveFailedSessionFingerprint: true });
|
|
244
432
|
markFailedSessionFingerprint(documentRef);
|
|
245
433
|
return false;
|
|
@@ -252,13 +440,13 @@ async function validateStoredSession(documentRef, authService, options) {
|
|
|
252
440
|
}
|
|
253
441
|
function injectSessionState() {
|
|
254
442
|
const documentRef = inject(DOCUMENT);
|
|
255
|
-
const
|
|
256
|
-
return validateStoredSession(documentRef,
|
|
443
|
+
const authSessionService = inject(AuthSessionService);
|
|
444
|
+
return validateStoredSession(documentRef, authSessionService);
|
|
257
445
|
}
|
|
258
446
|
function injectSessionRefresh() {
|
|
259
447
|
const documentRef = inject(DOCUMENT);
|
|
260
|
-
const
|
|
261
|
-
return validateStoredSession(documentRef,
|
|
448
|
+
const authSessionService = inject(AuthSessionService);
|
|
449
|
+
return validateStoredSession(documentRef, authSessionService, { forceRefresh: true });
|
|
262
450
|
}
|
|
263
451
|
function injectSessionClear() {
|
|
264
452
|
clearStoredSession(inject(DOCUMENT));
|
|
@@ -268,6 +456,7 @@ const PROTECTED_API_PATH_PREFIX = '/v1/';
|
|
|
268
456
|
const AUTH_PATH_PREFIX = '/v1/auth/';
|
|
269
457
|
const HEALTH_PATH = '/health';
|
|
270
458
|
const EDSIS_SKIP_AUTH = new HttpContextToken(() => false);
|
|
459
|
+
const EDSIS_SKIP_SESSION_REFRESH = new HttpContextToken(() => false);
|
|
271
460
|
const EDSIS_AUTH_RETRY = new HttpContextToken(() => false);
|
|
272
461
|
function normalizeUrl(url) {
|
|
273
462
|
try {
|
|
@@ -322,29 +511,34 @@ function applyBearerToken(request, accessToken, isRetry = false) {
|
|
|
322
511
|
},
|
|
323
512
|
});
|
|
324
513
|
}
|
|
325
|
-
async function resolveAccessToken(documentRef,
|
|
326
|
-
const isSessionValid = await validateStoredSession(documentRef,
|
|
514
|
+
async function resolveAccessToken(documentRef, authSessionService, options) {
|
|
515
|
+
const isSessionValid = await validateStoredSession(documentRef, authSessionService, options);
|
|
327
516
|
return isSessionValid ? readStoredAccessToken(documentRef) : '';
|
|
328
517
|
}
|
|
329
|
-
function handleUnauthorizedError(error, request, next, documentRef,
|
|
330
|
-
if (!(error instanceof HttpErrorResponse) ||
|
|
518
|
+
function handleUnauthorizedError(error, request, next, documentRef, authSessionService) {
|
|
519
|
+
if (!(error instanceof HttpErrorResponse) ||
|
|
520
|
+
error.status !== 401 ||
|
|
521
|
+
request.context.get(EDSIS_AUTH_RETRY) ||
|
|
522
|
+
request.context.get(EDSIS_SKIP_SESSION_REFRESH)) {
|
|
331
523
|
return throwError(() => error);
|
|
332
524
|
}
|
|
333
|
-
return from(resolveAccessToken(documentRef,
|
|
525
|
+
return from(resolveAccessToken(documentRef, authSessionService, { forceRefresh: true })).pipe(switchMap((refreshedAccessToken) => {
|
|
334
526
|
if (!refreshedAccessToken) {
|
|
335
527
|
clearStoredSession(documentRef);
|
|
336
528
|
return throwError(() => error);
|
|
337
529
|
}
|
|
338
530
|
return next(applyBearerToken(request, refreshedAccessToken, true));
|
|
339
|
-
}), catchError(() => {
|
|
340
|
-
|
|
531
|
+
}), catchError((refreshError) => {
|
|
532
|
+
if (shouldClearSessionAfterRefreshFailure(refreshError)) {
|
|
533
|
+
clearStoredSession(documentRef);
|
|
534
|
+
}
|
|
341
535
|
return throwError(() => error);
|
|
342
536
|
}));
|
|
343
537
|
}
|
|
344
538
|
const edsisAuthInterceptor = (request, next) => {
|
|
345
539
|
const documentRef = inject(DOCUMENT);
|
|
346
540
|
const apiConfiguration = inject(ApiConfiguration);
|
|
347
|
-
const
|
|
541
|
+
const authSessionService = inject(AuthSessionService);
|
|
348
542
|
if (request.context.get(EDSIS_SKIP_AUTH)) {
|
|
349
543
|
return next(request);
|
|
350
544
|
}
|
|
@@ -356,9 +550,12 @@ const edsisAuthInterceptor = (request, next) => {
|
|
|
356
550
|
if (isAuthRequest(apiPath) || isHealthRequest(apiPath)) {
|
|
357
551
|
return next(preparedRequest);
|
|
358
552
|
}
|
|
359
|
-
|
|
553
|
+
const skipSessionRefresh = request.context.get(EDSIS_SKIP_SESSION_REFRESH);
|
|
554
|
+
return from(skipSessionRefresh
|
|
555
|
+
? Promise.resolve(readStoredAccessToken(documentRef))
|
|
556
|
+
: resolveAccessToken(documentRef, authSessionService)).pipe(switchMap((accessToken) => {
|
|
360
557
|
const authorizedRequest = accessToken ? applyBearerToken(preparedRequest, accessToken) : preparedRequest;
|
|
361
|
-
return next(authorizedRequest).pipe(catchError((error) => handleUnauthorizedError(error, preparedRequest, next, documentRef,
|
|
558
|
+
return next(authorizedRequest).pipe(catchError((error) => handleUnauthorizedError(error, preparedRequest, next, documentRef, authSessionService)));
|
|
362
559
|
}));
|
|
363
560
|
};
|
|
364
561
|
|
|
@@ -367,6 +564,44 @@ function provideEdsisAuth(config) {
|
|
|
367
564
|
return makeEnvironmentProviders([
|
|
368
565
|
provideApiConfiguration(resolvedConfig.apiUrl),
|
|
369
566
|
provideHttpClient(withFetch(), withInterceptors([edsisAuthInterceptor])),
|
|
567
|
+
provideEnvironmentInitializer(() => {
|
|
568
|
+
const documentRef = inject(DOCUMENT);
|
|
569
|
+
const authSessionService = inject(AuthSessionService);
|
|
570
|
+
const router = inject(Router, { optional: true });
|
|
571
|
+
const userProfileService = inject(UserProfileService);
|
|
572
|
+
startAuthSessionMonitor(documentRef, authSessionService, {
|
|
573
|
+
validateSession: async () => {
|
|
574
|
+
if (!readStoredAccessToken(documentRef)) {
|
|
575
|
+
return true;
|
|
576
|
+
}
|
|
577
|
+
try {
|
|
578
|
+
await firstValueFrom(userProfileService.getCurrentUser(undefined, new HttpContext().set(EDSIS_SKIP_SESSION_REFRESH, true)));
|
|
579
|
+
return true;
|
|
580
|
+
}
|
|
581
|
+
catch (error) {
|
|
582
|
+
if (error instanceof HttpErrorResponse && error.status === 401) {
|
|
583
|
+
clearStoredSession(documentRef);
|
|
584
|
+
return false;
|
|
585
|
+
}
|
|
586
|
+
return true;
|
|
587
|
+
}
|
|
588
|
+
},
|
|
589
|
+
onSessionInvalid: () => {
|
|
590
|
+
if (router) {
|
|
591
|
+
void router.navigateByUrl(resolvedConfig.guestHomePath, { replaceUrl: true });
|
|
592
|
+
return;
|
|
593
|
+
}
|
|
594
|
+
const windowRef = documentRef.defaultView;
|
|
595
|
+
if (!windowRef) {
|
|
596
|
+
return;
|
|
597
|
+
}
|
|
598
|
+
const guestUrl = new URL(resolvedConfig.guestHomePath, windowRef.location.origin).toString();
|
|
599
|
+
if (windowRef.location.href !== guestUrl) {
|
|
600
|
+
windowRef.location.replace(guestUrl);
|
|
601
|
+
}
|
|
602
|
+
},
|
|
603
|
+
});
|
|
604
|
+
}),
|
|
370
605
|
{
|
|
371
606
|
provide: EDSIS_AUTH_CONFIG,
|
|
372
607
|
useValue: resolvedConfig,
|
|
@@ -429,10 +664,16 @@ i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "21.2.15", ngImpo
|
|
|
429
664
|
}]
|
|
430
665
|
}] });
|
|
431
666
|
|
|
432
|
-
function buildVerifyUrl(config) {
|
|
433
|
-
const baseUrl =
|
|
667
|
+
function buildVerifyUrl(config, runtimeOrigin = readRuntimeOrigin()) {
|
|
668
|
+
const baseUrl = normalizeText(runtimeOrigin) ?? config.appUrl;
|
|
434
669
|
return new URL(config.verifyPath, `${baseUrl}/`).toString();
|
|
435
670
|
}
|
|
671
|
+
function readRuntimeOrigin() {
|
|
672
|
+
if (typeof window === 'undefined') {
|
|
673
|
+
return null;
|
|
674
|
+
}
|
|
675
|
+
return normalizeText(window.location.origin);
|
|
676
|
+
}
|
|
436
677
|
function extractMagicToken(queryToken, fragment) {
|
|
437
678
|
const tokenFromQuery = normalizeText(queryToken);
|
|
438
679
|
if (tokenFromQuery) {
|
|
@@ -487,7 +728,8 @@ function normalizeText(value) {
|
|
|
487
728
|
class AuthLoginPage {
|
|
488
729
|
destroyRef = inject(DestroyRef);
|
|
489
730
|
formBuilder = inject(FormBuilder);
|
|
490
|
-
|
|
731
|
+
authMagicLinkService = inject(AuthMagicLinkService);
|
|
732
|
+
authConfig = inject(EDSIS_AUTH_CONFIG);
|
|
491
733
|
form = this.formBuilder.nonNullable.group({
|
|
492
734
|
email: ['', [Validators.required, Validators.email, Validators.maxLength(255)]],
|
|
493
735
|
});
|
|
@@ -551,10 +793,11 @@ class AuthLoginPage {
|
|
|
551
793
|
this.errorMessage.set(null);
|
|
552
794
|
const email = this.emailControl.getRawValue().trim().toLowerCase();
|
|
553
795
|
this.emailControl.setValue(email);
|
|
554
|
-
this.
|
|
796
|
+
this.authMagicLinkService
|
|
555
797
|
.requestMagicLoginLink({
|
|
556
798
|
body: {
|
|
557
799
|
email,
|
|
800
|
+
frontend_url: buildVerifyUrl(this.authConfig),
|
|
558
801
|
},
|
|
559
802
|
})
|
|
560
803
|
.pipe(takeUntilDestroyed(this.destroyRef), finalize(() => this.isSubmitting.set(false)))
|
|
@@ -917,7 +1160,7 @@ class AuthLogoutPage {
|
|
|
917
1160
|
destroyRef = inject(DestroyRef);
|
|
918
1161
|
documentRef = inject(DOCUMENT);
|
|
919
1162
|
router = inject(Router);
|
|
920
|
-
|
|
1163
|
+
authSessionService = inject(AuthSessionService);
|
|
921
1164
|
authConfig = inject(EDSIS_AUTH_CONFIG);
|
|
922
1165
|
guestHomePath = this.authConfig.guestHomePath;
|
|
923
1166
|
authenticatedHomePath = this.authConfig.authenticatedHomePath;
|
|
@@ -925,7 +1168,7 @@ class AuthLogoutPage {
|
|
|
925
1168
|
message = signal('Mengakhiri sesi...', ...(ngDevMode ? [{ debugName: "message" }] : /* istanbul ignore next */ []));
|
|
926
1169
|
ngOnInit() {
|
|
927
1170
|
clearStoredSession(this.documentRef);
|
|
928
|
-
this.
|
|
1171
|
+
this.authSessionService
|
|
929
1172
|
.logoutAuthSession()
|
|
930
1173
|
.pipe(takeUntilDestroyed(this.destroyRef), finalize(() => {
|
|
931
1174
|
if (this.state() === 'pending') {
|
|
@@ -1082,7 +1325,7 @@ class AuthVerifyPage {
|
|
|
1082
1325
|
route = inject(ActivatedRoute);
|
|
1083
1326
|
router = inject(Router);
|
|
1084
1327
|
location = inject(Location);
|
|
1085
|
-
|
|
1328
|
+
authMagicLinkService = inject(AuthMagicLinkService);
|
|
1086
1329
|
authConfig = inject(EDSIS_AUTH_CONFIG);
|
|
1087
1330
|
queryParamMap = toSignal(this.route.queryParamMap, {
|
|
1088
1331
|
initialValue: this.route.snapshot.queryParamMap,
|
|
@@ -1126,7 +1369,7 @@ class AuthVerifyPage {
|
|
|
1126
1369
|
}
|
|
1127
1370
|
verify(token) {
|
|
1128
1371
|
this.verifySubscription?.unsubscribe();
|
|
1129
|
-
this.verifySubscription = this.
|
|
1372
|
+
this.verifySubscription = this.authMagicLinkService
|
|
1130
1373
|
.verifyMagicLink({
|
|
1131
1374
|
body: {
|
|
1132
1375
|
token,
|
|
@@ -1313,5 +1556,5 @@ const authRoutes = [
|
|
|
1313
1556
|
* Generated bundle index. Do not edit.
|
|
1314
1557
|
*/
|
|
1315
1558
|
|
|
1316
|
-
export { AUTH_SESSION_CHANGE_EVENT, AuthLoginPage, AuthLogoutPage, AuthShellPage, AuthVerifyPage, EDSIS_AUTH_CONFIG, EDSIS_SKIP_AUTH, authRoutes, authenticatedGuard, clearStoredSession, edsisAuthInterceptor, guestOnlyGuard, hasStoredSession, homeRedirectGuard, injectSessionClear, injectSessionRefresh, injectSessionState, persistSession, provideEdsisAuth, readStoredAccessToken, readStoredSession, resolveEdsisAuthConfig, validateStoredSession, ɵresetAuthSessionModuleState };
|
|
1559
|
+
export { AUTH_SESSION_CHANGE_EVENT, AuthLoginPage, AuthLogoutPage, AuthShellPage, AuthVerifyPage, EDSIS_AUTH_CONFIG, EDSIS_SKIP_AUTH, EDSIS_SKIP_SESSION_REFRESH, authRoutes, authenticatedGuard, clearStoredSession, edsisAuthInterceptor, guestOnlyGuard, hasStoredSession, homeRedirectGuard, injectSessionClear, injectSessionRefresh, injectSessionState, persistSession, provideEdsisAuth, readStoredAccessToken, readStoredSession, resolveEdsisAuthConfig, shouldClearSessionAfterRefreshFailure, startAuthSessionMonitor, validateStoredSession, ɵresetAuthSessionModuleState };
|
|
1317
1560
|
//# sourceMappingURL=edsis-angular-module-auth.mjs.map
|