-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathSession.ts
More file actions
104 lines (94 loc) · 3.91 KB
/
Session.ts
File metadata and controls
104 lines (94 loc) · 3.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
import { DereferencableIdClientDetails, DynamicRegistrationClientDetails } from '../core';
import { SessionOptions, SessionCore } from '../core/Session';
import { getWorkerUrl } from './RefreshWorkerUrl';
import { RefreshMessageTypes } from './RefreshMessageTypes';
import { SessionIDB } from './SessionDatabase';
// Any provided database via SessionOptions will be ignored.
// Database will be an IndexedDB.
export interface WebWorkerSessionOptions extends SessionOptions {
workerUrl?: string | URL;
}
/**
* This Session provides background token refreshing using a Web Worker.
*/
export class WebWorkerSession extends SessionCore {
private worker: SharedWorker;
constructor(
clientDetails?: DereferencableIdClientDetails | DynamicRegistrationClientDetails,
sessionOptions?: WebWorkerSessionOptions
) {
const database = new SessionIDB();
const options = { ...sessionOptions, database };
super(clientDetails, options);
// Allow consumer to provide worker URL, or use default
const workerUrl = sessionOptions?.workerUrl ?? getWorkerUrl()
this.worker = new SharedWorker(workerUrl, { type: 'module' });
this.worker.port.onmessage = (event) => {
this.handleWorkerMessage(event.data).catch(console.error);
};
window.addEventListener('beforeunload', () => {
this.worker.port.postMessage({ type: RefreshMessageTypes.DISCONNECT });
});
}
private async handleWorkerMessage(data: any) {
const { type, payload, error } = data;
switch (type) {
case RefreshMessageTypes.TOKEN_DETAILS:
const wasActive = this.isActive;
await this.setTokenDetails(payload.tokenDetails);
if (wasActive !== this.isActive)
this.dispatchStateChangeEvent();
if (this.refreshPromise && this.resolveRefresh) {
this.resolveRefresh();
this.clearRefreshPromise();
}
break;
case RefreshMessageTypes.ERROR_ON_REFRESH:
if (this.isActive)
this.dispatchExpirationWarningEvent();
if (this.refreshPromise && this.rejectRefresh) {
if (this.isActive) {
this.rejectRefresh(new Error(error || 'Token refresh failed'));
} else {
this.rejectRefresh(new Error("No session to restore"));
}
this.clearRefreshPromise();
}
break;
case RefreshMessageTypes.EXPIRED:
if (this.isActive) {
this.dispatchExpirationEvent();
await this.logout();
}
if (this.refreshPromise && this.rejectRefresh) {
this.rejectRefresh(new Error(error || 'Token refresh failed'));
this.clearRefreshPromise();
}
break;
}
};
async handleRedirectFromLogin() {
await super.handleRedirectFromLogin();
if (this.isActive) { // If login was successful, tell the worker to schedule refreshing
this.worker.port.postMessage({
type: RefreshMessageTypes.SCHEDULE,
payload: { ...this.getTokenDetails(), expires_in: this.getExpiresIn() }
});
}
}
async restore() {
if (this.refreshPromise) {
return this.refreshPromise;
}
this.refreshPromise = new Promise((resolve, reject) => {
this.resolveRefresh = resolve;
this.rejectRefresh = reject;
});
this.worker.port.postMessage({ type: RefreshMessageTypes.REFRESH });
return this.refreshPromise;
}
async logout() {
this.worker.port.postMessage({ type: RefreshMessageTypes.STOP });
await super.logout();
}
}