MCPcopy
hub / github.com/authts/oidc-client-ts / SessionMonitor

Class SessionMonitor

src/SessionMonitor.ts:12–179  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

10 * @public
11 */
12export class SessionMonitor {
13 private readonly _logger = new Logger("SessionMonitor");
14
15 private _sub: string | undefined;
16 private _checkSessionIFrame?: CheckSessionIFrame;
17
18 public constructor(private readonly _userManager: UserManager) {
19 if (!_userManager) {
20 this._logger.throw(new Error("No user manager passed"));
21 }
22
23 this._userManager.events.addUserLoaded(this._start);
24 this._userManager.events.addUserUnloaded(this._stop);
25
26 this._init().catch((err: unknown) => {
27 // catch to suppress errors since we're in a ctor
28 this._logger.error(err);
29 });
30 }
31
32 protected async _init(): Promise<void> {
33 this._logger.create("_init");
34 const user = await this._userManager.getUser();
35 // doing this manually here since calling getUser
36 // doesn't trigger load event.
37 if (user) {
38 void this._start(user);
39 }
40 else if (this._userManager.settings.monitorAnonymousSession) {
41 const session = await this._userManager.querySessionStatus();
42 if (session) {
43 const tmpUser = {
44 session_state: session.session_state,
45 profile: session.sub ? {
46 sub: session.sub,
47 } : null,
48 };
49 void this._start(tmpUser);
50 }
51 }
52 }
53
54 protected _start = async (
55 user: User | {
56 session_state: string;
57 profile: { sub: string } | null;
58 },
59 ): Promise<void> => {
60 const session_state = user.session_state;
61 if (!session_state) {
62 return;
63 }
64 const logger = this._logger.create("_start");
65
66 if (user.profile) {
67 this._sub = user.profile.sub;
68 logger.debug("session_state", session_state, ", sub", this._sub);
69 }

Callers

nothing calls this directly

Calls 12

getCheckSessionIframeMethod · 0.80
querySessionStatusMethod · 0.80
_raiseUserSignedOutMethod · 0.80
_raiseUserSignedInMethod · 0.80
debugMethod · 0.65
warnMethod · 0.65
errorMethod · 0.65
createMethod · 0.45
startMethod · 0.45
loadMethod · 0.45
stopMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…