@photonhealth/client 2.0.0-rc.1 → 2.0.0-rc.3
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/README.md +150 -5
- package/dist/analytics/lifecycleEvents.d.ts +32 -0
- package/dist/analytics/lifecycleEvents.d.ts.map +1 -0
- package/dist/analytics/lifecycleEvents.js +75 -0
- package/dist/analytics/lifecycleEvents.js.map +1 -0
- package/dist/analytics/track.d.ts +39 -0
- package/dist/analytics/track.d.ts.map +1 -0
- package/dist/analytics/track.js +91 -0
- package/dist/analytics/track.js.map +1 -0
- package/dist/api/changes/applyOption.d.ts +4 -1
- package/dist/api/changes/applyOption.d.ts.map +1 -1
- package/dist/api/changes/applyOption.js +23 -2
- package/dist/api/changes/applyOption.js.map +1 -1
- package/dist/api/client.d.ts +6 -1
- package/dist/api/client.d.ts.map +1 -1
- package/dist/api/client.js +47 -33
- package/dist/api/client.js.map +1 -1
- package/dist/api/index.d.ts +2 -0
- package/dist/api/index.d.ts.map +1 -1
- package/dist/api/index.js +2 -0
- package/dist/api/index.js.map +1 -1
- package/dist/api/me.d.ts +37 -0
- package/dist/api/me.d.ts.map +1 -0
- package/dist/api/me.js +6 -0
- package/dist/api/me.js.map +1 -0
- package/dist/api/orders.d.ts +1047 -0
- package/dist/api/orders.d.ts.map +1 -0
- package/dist/api/orders.js +7 -0
- package/dist/api/orders.js.map +1 -0
- package/dist/api/patient.d.ts +5 -2
- package/dist/api/patient.d.ts.map +1 -1
- package/dist/api/patient.js +1 -1
- package/dist/api/patient.js.map +1 -1
- package/dist/api/prescription.d.ts +7 -1
- package/dist/api/prescription.d.ts.map +1 -1
- package/dist/api/prescription.js +10 -1
- package/dist/api/prescription.js.map +1 -1
- package/dist/gql/gql.d.ts +11 -3
- package/dist/gql/gql.d.ts.map +1 -1
- package/dist/gql/gql.js +5 -3
- package/dist/gql/gql.js.map +1 -1
- package/dist/gql/graphql.d.ts +10507 -1563
- package/dist/gql/graphql.d.ts.map +1 -1
- package/dist/gql/graphql.js +2984 -435
- package/dist/gql/graphql.js.map +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.js +1 -1
- package/dist/meta/version.d.ts +2 -0
- package/dist/meta/version.d.ts.map +1 -0
- package/dist/meta/version.js +2 -0
- package/dist/meta/version.js.map +1 -0
- package/dist/photon/auth.d.ts +9 -5
- package/dist/photon/auth.d.ts.map +1 -1
- package/dist/photon/auth.js +2 -6
- package/dist/photon/auth.js.map +1 -1
- package/dist/photon/choices.d.ts +1 -1
- package/dist/photon/choices.js +2 -2
- package/dist/photon/choices.js.map +1 -1
- package/dist/photon/environments.d.ts +2 -1
- package/dist/photon/environments.d.ts.map +1 -1
- package/dist/photon/environments.js +2 -1
- package/dist/photon/environments.js.map +1 -1
- package/dist/photon/index.d.ts +6 -3
- package/dist/photon/index.d.ts.map +1 -1
- package/dist/photon/index.js +2 -1
- package/dist/photon/index.js.map +1 -1
- package/dist/photon/machine.d.ts +28 -0
- package/dist/photon/machine.d.ts.map +1 -0
- package/dist/photon/machine.js +53 -0
- package/dist/photon/machine.js.map +1 -0
- package/dist/photon/map.d.ts +6 -2
- package/dist/photon/map.d.ts.map +1 -1
- package/dist/photon/map.js +133 -33
- package/dist/photon/map.js.map +1 -1
- package/dist/photon/order.d.ts +24 -5
- package/dist/photon/order.d.ts.map +1 -1
- package/dist/photon/order.js +85 -23
- package/dist/photon/order.js.map +1 -1
- package/dist/photon/orders.d.ts +5 -0
- package/dist/photon/orders.d.ts.map +1 -0
- package/dist/photon/orders.js +39 -0
- package/dist/photon/orders.js.map +1 -0
- package/dist/photon/patient.d.ts +8 -6
- package/dist/photon/patient.d.ts.map +1 -1
- package/dist/photon/patient.js +61 -30
- package/dist/photon/patient.js.map +1 -1
- package/dist/photon/photon.d.ts +39 -1
- package/dist/photon/photon.d.ts.map +1 -1
- package/dist/photon/photon.js +85 -5
- package/dist/photon/photon.js.map +1 -1
- package/dist/photon/prescription.d.ts +17 -4
- package/dist/photon/prescription.d.ts.map +1 -1
- package/dist/photon/prescription.js +48 -11
- package/dist/photon/prescription.js.map +1 -1
- package/dist/photon/session.d.ts +16 -10
- package/dist/photon/session.d.ts.map +1 -1
- package/dist/photon/session.js +26 -11
- package/dist/photon/session.js.map +1 -1
- package/dist/photon/types.d.ts +278 -46
- package/dist/photon/types.d.ts.map +1 -1
- package/dist/test-utils/msw.d.ts +2 -2
- package/dist/test-utils/msw.d.ts.map +1 -1
- package/dist/test-utils/msw.js +5 -5
- package/dist/test-utils/msw.js.map +1 -1
- package/dist/testing/{scriptedClient.d.ts → fakeNetworkApiClient.d.ts} +5 -4
- package/dist/testing/fakeNetworkApiClient.d.ts.map +1 -0
- package/dist/testing/{scriptedClient.js → fakeNetworkApiClient.js} +8 -4
- package/dist/testing/fakeNetworkApiClient.js.map +1 -0
- package/dist/testing/fixtures.d.ts +1047 -0
- package/dist/testing/fixtures.d.ts.map +1 -1
- package/dist/testing/fixtures.js +47 -13
- package/dist/testing/fixtures.js.map +1 -1
- package/dist/testing.d.ts +4 -4
- package/dist/testing.d.ts.map +1 -1
- package/dist/testing.js +4 -4
- package/dist/testing.js.map +1 -1
- package/package.json +2 -2
- package/src/analytics/lifecycleEvents.test.ts +437 -0
- package/src/analytics/lifecycleEvents.ts +172 -0
- package/src/analytics/track.test.ts +231 -0
- package/src/analytics/track.ts +129 -0
- package/src/api/changes/applyOption.ts +25 -2
- package/src/api/client.ts +71 -46
- package/src/api/index.ts +2 -0
- package/src/api/me.ts +7 -0
- package/src/api/orders.ts +8 -0
- package/src/api/patient.ts +11 -2
- package/src/api/prescription.ts +18 -0
- package/src/gql/gql.ts +21 -9
- package/src/gql/graphql.ts +4280 -597
- package/src/index.ts +1 -1
- package/src/meta/version.test.ts +10 -0
- package/src/meta/version.ts +1 -0
- package/src/operations/fragments.graphql +166 -20
- package/src/operations/me.graphql +38 -0
- package/src/operations/orders.graphql +35 -0
- package/src/operations/patient.graphql +29 -3
- package/src/operations/prescription.graphql +20 -0
- package/src/photon/auth.ts +9 -10
- package/src/photon/choices.ts +2 -2
- package/src/photon/environments.ts +3 -1
- package/src/photon/index.ts +17 -2
- package/src/photon/machine.ts +90 -0
- package/src/photon/map.ts +151 -35
- package/src/photon/order.test.ts +142 -0
- package/src/photon/order.ts +99 -24
- package/src/photon/orders.ts +42 -0
- package/src/photon/patient.ts +73 -33
- package/src/photon/photon.ts +139 -6
- package/src/photon/prescription.ts +71 -12
- package/src/photon/session.ts +39 -16
- package/src/photon/types.ts +280 -26
- package/src/test-utils/msw.ts +16 -5
- package/src/testing/{scriptedClient.ts → fakeNetworkApiClient.ts} +8 -4
- package/src/testing/fixtures.ts +55 -13
- package/src/testing.ts +6 -3
- package/dist/testing/scriptedClient.d.ts.map +0 -1
- package/dist/testing/scriptedClient.js.map +0 -1
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import {
|
|
2
2
|
getPrescription,
|
|
3
|
+
getPrescriptionTemplates,
|
|
3
4
|
syncPrescription,
|
|
4
5
|
updatePrescription,
|
|
5
6
|
type ChangeSet,
|
|
@@ -12,14 +13,21 @@ import { classify, declaredChoice, optionKey, withChoice } from "./choices.ts";
|
|
|
12
13
|
import { PhotonError, refuse } from "./errors.ts";
|
|
13
14
|
import { filled } from "./inputs.ts";
|
|
14
15
|
import { LiveRecord } from "./live.ts";
|
|
15
|
-
import { noChanges, toChanges, toPrescription } from "./map.ts";
|
|
16
|
-
import type {
|
|
16
|
+
import { noChanges, toChanges, toPrescription, toPrescriptionTemplate } from "./map.ts";
|
|
17
|
+
import type {
|
|
18
|
+
ChangeOption,
|
|
19
|
+
Changes,
|
|
20
|
+
PrescriptionEdits,
|
|
21
|
+
PrescriptionRecord,
|
|
22
|
+
PrescriptionTemplate,
|
|
23
|
+
Treatment,
|
|
24
|
+
} from "./types.ts";
|
|
17
25
|
|
|
18
26
|
export interface PrescriptionExtra {
|
|
19
27
|
/**
|
|
20
28
|
* The drug for this line before its record exists — picked by the prescriber, or declared by the server
|
|
21
29
|
* (an exact match) — and, once it does, the name the prescriber saw: network-api's treatment name can drop
|
|
22
|
-
* the strength and form
|
|
30
|
+
* the strength and form. `treatment`: the picked option's drug, in parts (null when the
|
|
23
31
|
* server declared it, or it was given by id).
|
|
24
32
|
*/
|
|
25
33
|
pick: { id: string; name: string; treatment: Treatment | null } | null;
|
|
@@ -28,6 +36,13 @@ export interface PrescriptionExtra {
|
|
|
28
36
|
* Pick one with `continueDraft(id)`. null when not asked.
|
|
29
37
|
*/
|
|
30
38
|
drafts: PrescriptionRecord[] | null;
|
|
39
|
+
/**
|
|
40
|
+
* This draft's templates, once `loadTemplates()` asked for them: kept current as its drug or patient changes.
|
|
41
|
+
* The caller's catalog templates come first, then recommended ones (sigs worded for the patient, then units).
|
|
42
|
+
* null until then, while loading, before the draft has a record, on a signed prescription, and when the lookup
|
|
43
|
+
* fails.
|
|
44
|
+
*/
|
|
45
|
+
templates: PrescriptionTemplate[] | null;
|
|
31
46
|
}
|
|
32
47
|
|
|
33
48
|
/** The prescription mutation on a live object: its fields, and who it's for. */
|
|
@@ -97,7 +112,7 @@ export class Prescription extends LiveRecord<PrescriptionRecord, PrescriptionExt
|
|
|
97
112
|
#lookup: AbortController | null = null;
|
|
98
113
|
|
|
99
114
|
constructor(ctx: Context, record: PrescriptionRecord | null, options: PrescriptionOptions = {}) {
|
|
100
|
-
super(record, { pick: null, drafts: null }, options.changes);
|
|
115
|
+
super(record, { pick: null, drafts: null, templates: null }, options.changes);
|
|
101
116
|
this.#ctx = ctx;
|
|
102
117
|
this.#patientId = options.patientId ?? record?.patient?.id ?? null;
|
|
103
118
|
this.#templateId = options.templateId ?? null;
|
|
@@ -119,6 +134,42 @@ export class Prescription extends LiveRecord<PrescriptionRecord, PrescriptionExt
|
|
|
119
134
|
this.#ctx.register(this);
|
|
120
135
|
}
|
|
121
136
|
|
|
137
|
+
/** Whether anything asked for templates (`loadTemplates()`); until then, saves never trigger the lookup. */
|
|
138
|
+
#templatesWanted = false;
|
|
139
|
+
/** The record, drug and patient the current `templates` are for: a change to any of them fetches again. */
|
|
140
|
+
#templatesFor: string | null = null;
|
|
141
|
+
|
|
142
|
+
/**
|
|
143
|
+
* Fetch this draft's templates into `templates`, and refetch whenever its drug or patient changes. Separate from
|
|
144
|
+
* every other call so saves don't wait on the Medi-Span sig lookup. A failed lookup leaves `templates` null.
|
|
145
|
+
*/
|
|
146
|
+
loadTemplates(): void {
|
|
147
|
+
this.#templatesWanted = true;
|
|
148
|
+
this.#refreshTemplates();
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
protected override patch(patch: Parameters<LiveRecord<PrescriptionRecord, PrescriptionExtra>["patch"]>[0]) {
|
|
152
|
+
super.patch(patch);
|
|
153
|
+
if (this.#templatesWanted) this.#refreshTemplates();
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
#refreshTemplates() {
|
|
157
|
+
const rx = this.state;
|
|
158
|
+
const templatesFor = rx?.patient && !this.signed ? `${rx.id}:${rx.treatment.id}:${rx.patient.id}` : null;
|
|
159
|
+
if (templatesFor === this.#templatesFor) return;
|
|
160
|
+
this.#templatesFor = templatesFor;
|
|
161
|
+
super.patch({ templates: null });
|
|
162
|
+
if (!rx || !templatesFor) return;
|
|
163
|
+
getPrescriptionTemplates(this.#ctx.client, rx.id).then(
|
|
164
|
+
(templates) => {
|
|
165
|
+
if (this.#templatesFor === templatesFor) {
|
|
166
|
+
super.patch({ templates: templates && templates.map(toPrescriptionTemplate) });
|
|
167
|
+
}
|
|
168
|
+
},
|
|
169
|
+
() => {},
|
|
170
|
+
);
|
|
171
|
+
}
|
|
172
|
+
|
|
122
173
|
get signed(): boolean {
|
|
123
174
|
return this.state?.signing.state === "SIGNED";
|
|
124
175
|
}
|
|
@@ -126,7 +177,7 @@ export class Prescription extends LiveRecord<PrescriptionRecord, PrescriptionExt
|
|
|
126
177
|
/**
|
|
127
178
|
* The drug's name as the prescriber saw it (the pick), while that pick is the record's drug; else the server's
|
|
128
179
|
* product name (`prescribableName` — its `name` can be just the drug). Never an id: a drug given by id alone (e.g.
|
|
129
|
-
* from a link) has no name until network-api gives one — once it's drafted for a patient
|
|
180
|
+
* from a link) has no name until network-api gives one — once it's drafted for a patient.
|
|
130
181
|
*/
|
|
131
182
|
get name(): string | null {
|
|
132
183
|
const pick = this.snapshot.pick;
|
|
@@ -255,14 +306,22 @@ export class Prescription extends LiveRecord<PrescriptionRecord, PrescriptionExt
|
|
|
255
306
|
/**
|
|
256
307
|
* The prescriber's approval. Waits for updates already sent, then signs what was stored — bound to its
|
|
257
308
|
* contentHash. Screening alerts don't stop it: signing is the prescriber's acknowledgement of
|
|
258
|
-
* what screening showed (network-api has no way to acknowledge an alert yet
|
|
309
|
+
* what screening showed (network-api has no way to acknowledge an alert yet). Refused if
|
|
259
310
|
* the server's last answer left something open (a choice to make, a rejected field), or if an update in
|
|
260
311
|
* flight changed the screening result the prescriber saw (they review it first). A prescription marked changed-since-signing (INVALIDATED_SIGN) can be signed
|
|
261
312
|
* again: that's the prescriber approving what it says now. Sends only the signing, never content.
|
|
262
313
|
*/
|
|
263
314
|
sign(message?: string): Promise<{ changes: Changes }> {
|
|
264
315
|
const seen = this.state;
|
|
265
|
-
|
|
316
|
+
// Before its record, refused on the object (`error`), as a failed save is: a drug picked waits for its patient.
|
|
317
|
+
if (!seen)
|
|
318
|
+
return this.run("saving", async () => {
|
|
319
|
+
throw refuse(
|
|
320
|
+
this.snapshot.pick && !this.#patientId
|
|
321
|
+
? "A patient is required for signing"
|
|
322
|
+
: "There's nothing to sign yet: pick the drug first.",
|
|
323
|
+
);
|
|
324
|
+
});
|
|
266
325
|
return this.run("saving", async () => {
|
|
267
326
|
const current = this.state!;
|
|
268
327
|
if (current.signing.state === "SIGNED") return { changes: noChanges };
|
|
@@ -321,7 +380,7 @@ export class Prescription extends LiveRecord<PrescriptionRecord, PrescriptionExt
|
|
|
321
380
|
}
|
|
322
381
|
if (!this.#patientId) {
|
|
323
382
|
// A drug given by id, no patient yet: it's the line's drug — the pick, waiting for the patient. The lookup
|
|
324
|
-
// checks the id; network-api doesn't name it until there's a draft
|
|
383
|
+
// checks the id; network-api doesn't name it until there's a draft, so the name is the
|
|
325
384
|
// one given with it, or the id.
|
|
326
385
|
const given = this.#entered.treatment;
|
|
327
386
|
if (!pick && given?.id) {
|
|
@@ -340,7 +399,7 @@ export class Prescription extends LiveRecord<PrescriptionRecord, PrescriptionExt
|
|
|
340
399
|
* SyncPrescription without a patient: a drug lookup — it can never create anything. network-api also rejects
|
|
341
400
|
* the patient and every field it would have filled ("treatment.instructions", … — blocked by the missing
|
|
342
401
|
* patient); only the drug's own change answers it: its choices, or why nothing matched. (A template isn't looked
|
|
343
|
-
* up: network-api doesn't expand one without a patient
|
|
402
|
+
* up: network-api doesn't expand one without a patient.) null when newer text aborted it.
|
|
344
403
|
*/
|
|
345
404
|
async #lookUp(
|
|
346
405
|
treatment: NonNullable<PrescriptionEdits["treatment"]>,
|
|
@@ -374,7 +433,7 @@ export class Prescription extends LiveRecord<PrescriptionRecord, PrescriptionExt
|
|
|
374
433
|
const changes = toChanges(outcome.changes);
|
|
375
434
|
// A new line only takes a record once its drug is clear, and never a signed one or another line's:
|
|
376
435
|
// while the drug is still ambiguous, network-api hands back the patient's latest prescription — an old
|
|
377
|
-
// draft, one already signed and sent, or another line's (boson, 2026-09-29
|
|
436
|
+
// draft, one already signed and sent, or another line's (boson, 2026-09-29).
|
|
378
437
|
const drugStillOpen = classify(changes).needsChoice.some((c) => c.key.startsWith("treatment"));
|
|
379
438
|
const rx = outcome.prescription;
|
|
380
439
|
if (!rx || drugStillOpen || !this.#canTake(rx)) {
|
|
@@ -392,7 +451,7 @@ export class Prescription extends LiveRecord<PrescriptionRecord, PrescriptionExt
|
|
|
392
451
|
/**
|
|
393
452
|
* The patient already has several in-progress prescriptions and the server asks which this continues.
|
|
394
453
|
* Candidates carry no drug or signing state, so read each; offer only unsigned drafts of the same drug that
|
|
395
|
-
* no other line holds. network-api counts signed (even sent) ones as in progress
|
|
454
|
+
* no other line holds. network-api counts signed (even sent) ones as in progress.
|
|
396
455
|
*/
|
|
397
456
|
async #offerDrafts(ids: string[]): Promise<{ changes: Changes }> {
|
|
398
457
|
const read = await Promise.all(ids.map((id) => getPrescription(this.#ctx.client, id)));
|
|
@@ -408,7 +467,7 @@ export class Prescription extends LiveRecord<PrescriptionRecord, PrescriptionExt
|
|
|
408
467
|
return { changes: this.changes };
|
|
409
468
|
}
|
|
410
469
|
const names = [...new Set(open.map((rx) => rx.treatment.name))].join(", ");
|
|
411
|
-
// network-api won't start another prescription while ≥2 are open
|
|
470
|
+
// network-api won't start another prescription while ≥2 are open.
|
|
412
471
|
throw refuse(
|
|
413
472
|
open.every((rx) => rx.signing.state === "UNSIGNED")
|
|
414
473
|
? `This patient has ${open.length} unsent prescriptions (${names}). A new one can't be started until they're sent.`
|
package/src/photon/session.ts
CHANGED
|
@@ -20,9 +20,8 @@ import { PhotonError } from "./errors.ts";
|
|
|
20
20
|
* uses a token the other already has. So the tabs of a site take turns (Web Locks) and each uses the newest one
|
|
21
21
|
* stored. No refresh token from Auth0: nothing is kept, and the session lasts as long as the page.
|
|
22
22
|
*
|
|
23
|
-
* Known risks
|
|
24
|
-
*
|
|
25
|
-
* SDK and awaits an auth review (#39).
|
|
23
|
+
* Known risks: a script on the page can still read the refresh token (the posture chosen, Auth0's own for SPAs);
|
|
24
|
+
* a session ending mid-work swaps the cards for Sign in (signing in again goes back to its organization).
|
|
26
25
|
*/
|
|
27
26
|
export type SessionStatus = "starting" | "signed-out" | "signing-in" | "signed-in" | "error";
|
|
28
27
|
|
|
@@ -39,6 +38,14 @@ export interface SessionSnapshot {
|
|
|
39
38
|
readonly error: PhotonError | null;
|
|
40
39
|
}
|
|
41
40
|
|
|
41
|
+
/** Where `signIn` goes at Auth0. */
|
|
42
|
+
export interface SignInOptions {
|
|
43
|
+
/** The Photon organization to sign in to, past Auth0's picker. */
|
|
44
|
+
organization?: string;
|
|
45
|
+
/** An Auth0 connection (e.g. `"google-oauth2"`): straight to that identity provider, past Auth0's own page. */
|
|
46
|
+
connection?: string;
|
|
47
|
+
}
|
|
48
|
+
|
|
42
49
|
export interface SessionConfig {
|
|
43
50
|
/** The Auth0 tenant (`auth.<env>.health`). */
|
|
44
51
|
domain: string;
|
|
@@ -91,6 +98,7 @@ export class Session {
|
|
|
91
98
|
#refreshToken: string | null = null;
|
|
92
99
|
/** Whether storage holds the refresh token — then the newest one is read from there (another tab may rotate it). */
|
|
93
100
|
#kept = false;
|
|
101
|
+
#lastOrganization: string | null = null;
|
|
94
102
|
#renewing: Promise<string> | null = null;
|
|
95
103
|
#started: Promise<void> | null = null;
|
|
96
104
|
readonly #config: SessionConfig;
|
|
@@ -120,9 +128,11 @@ export class Session {
|
|
|
120
128
|
|
|
121
129
|
/**
|
|
122
130
|
* Sign in — from a click. Leaves the page for Auth0 (its organization picker; `organization`, or the page's
|
|
123
|
-
* `?organization=`, goes straight in
|
|
131
|
+
* `?organization=`, goes straight in; `connection`, e.g. `"google-oauth2"`, goes straight to that identity provider,
|
|
132
|
+
* past Auth0's page) and comes back here. A session that ended without signing out (a refused refresh) signs in
|
|
133
|
+
* again to its organization, unless another is named. Inside an iframe: a popup, then signed in here.
|
|
124
134
|
*/
|
|
125
|
-
async signIn(options:
|
|
135
|
+
async signIn(options: SignInOptions = {}): Promise<void> {
|
|
126
136
|
this.#set({ status: "signing-in", error: null });
|
|
127
137
|
if (window.top !== window) {
|
|
128
138
|
const { params, state, pending } = await this.#authorize(options);
|
|
@@ -135,7 +145,7 @@ export class Session {
|
|
|
135
145
|
* Auth0's sign-in page for this session — to link to, or go to yourself. It's ready to come back to: the next
|
|
136
146
|
* `start()` on this page finishes the sign-in (PKCE: the verifier is kept for it, in this tab).
|
|
137
147
|
*/
|
|
138
|
-
async signInUrl(options:
|
|
148
|
+
async signInUrl(options: SignInOptions = {}): Promise<string> {
|
|
139
149
|
const { params, state, pending } = await this.#authorize(options);
|
|
140
150
|
write(sessionStorage, pendingKey(state), pending);
|
|
141
151
|
return this.#authorizeUrl(params);
|
|
@@ -150,6 +160,7 @@ export class Session {
|
|
|
150
160
|
/** Sign out: forget the session here, and end Auth0's (`signOutUrl`). Not something the cards offer. */
|
|
151
161
|
signOut(options: { returnTo?: string } = {}): void {
|
|
152
162
|
this.#forget();
|
|
163
|
+
this.#lastOrganization = null;
|
|
153
164
|
this.#set({ status: "signed-out", user: null, error: null });
|
|
154
165
|
this.#navigate(this.signOutUrl(options));
|
|
155
166
|
}
|
|
@@ -165,11 +176,12 @@ export class Session {
|
|
|
165
176
|
// ---- inside ----
|
|
166
177
|
|
|
167
178
|
/** A sign-in request: Auth0's parameters (PKCE), and what coming back needs. */
|
|
168
|
-
async #authorize(options:
|
|
179
|
+
async #authorize(options: SignInOptions) {
|
|
169
180
|
const state = randomString(16);
|
|
170
181
|
const verifier = randomString(32);
|
|
171
182
|
const redirectUri = window.location.origin;
|
|
172
|
-
const organization =
|
|
183
|
+
const organization =
|
|
184
|
+
options.organization ?? new URLSearchParams(window.location.search).get("organization") ?? this.#lastOrganization;
|
|
173
185
|
const params = new URLSearchParams({
|
|
174
186
|
response_type: "code",
|
|
175
187
|
client_id: this.#config.clientId,
|
|
@@ -180,6 +192,7 @@ export class Session {
|
|
|
180
192
|
code_challenge: await challengeOf(verifier),
|
|
181
193
|
code_challenge_method: "S256",
|
|
182
194
|
...(organization ? { organization } : {}),
|
|
195
|
+
...(options.connection ? { connection: options.connection } : {}),
|
|
183
196
|
});
|
|
184
197
|
const pending: Pending = { verifier, returnTo: returnTo(), redirectUri };
|
|
185
198
|
return { params, state, pending };
|
|
@@ -204,10 +217,18 @@ export class Session {
|
|
|
204
217
|
const stored = this.#readStored();
|
|
205
218
|
if (!stored) return this.#set({ status: "signed-out" });
|
|
206
219
|
this.#user = stored.user;
|
|
207
|
-
await this.#
|
|
220
|
+
await this.#renewStoredSession(); // the access token was never kept: every load starts with a refresh
|
|
221
|
+
} catch (e) {
|
|
222
|
+
this.#set({ status: "error", user: null, error: asError(e) });
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
async #renewStoredSession(): Promise<void> {
|
|
227
|
+
try {
|
|
228
|
+
await this.#renew();
|
|
208
229
|
} catch (e) {
|
|
209
|
-
|
|
210
|
-
|
|
230
|
+
if (wasSignedOutByRenewal(e)) return;
|
|
231
|
+
throw e;
|
|
211
232
|
}
|
|
212
233
|
}
|
|
213
234
|
|
|
@@ -218,14 +239,14 @@ export class Session {
|
|
|
218
239
|
#renew(): Promise<string> {
|
|
219
240
|
return this.#oneTabAtATime(async () => {
|
|
220
241
|
const refreshToken = this.#kept ? (this.#readStored()?.refreshToken ?? null) : this.#refreshToken;
|
|
221
|
-
if (!refreshToken) return this.#
|
|
242
|
+
if (!refreshToken) return this.#endSession(); // signed out here, or in another tab
|
|
222
243
|
try {
|
|
223
244
|
const tokens = await this.#tokenRequest({ grant_type: "refresh_token", refresh_token: refreshToken });
|
|
224
245
|
this.#keep(tokens, refreshToken);
|
|
225
246
|
return this.#access!.token;
|
|
226
247
|
} catch (e) {
|
|
227
248
|
// A refresh token Auth0 won't take (expired, revoked, rotated away): signed out — not an error.
|
|
228
|
-
if (e instanceof PhotonError && e.kind === "unauthorized") return this.#
|
|
249
|
+
if (e instanceof PhotonError && e.kind === "unauthorized") return this.#endSession();
|
|
229
250
|
throw e;
|
|
230
251
|
}
|
|
231
252
|
});
|
|
@@ -313,7 +334,7 @@ export class Session {
|
|
|
313
334
|
}
|
|
314
335
|
|
|
315
336
|
#keep(tokens: TokenResponse, previousRefresh: string | null) {
|
|
316
|
-
const claims = { ...
|
|
337
|
+
const claims = { ...claimsOf(tokens.id_token), ...claimsOf(tokens.access_token) };
|
|
317
338
|
this.#access = { token: tokens.access_token, expiresAt: Date.now() + (tokens.expires_in ?? 3600) * 1000 };
|
|
318
339
|
// The access token has the org; a refresh's id token may not come back, so the user carries over.
|
|
319
340
|
this.#user = {
|
|
@@ -329,7 +350,8 @@ export class Session {
|
|
|
329
350
|
this.#set({ status: "signed-in", user: this.#user, error: null });
|
|
330
351
|
}
|
|
331
352
|
|
|
332
|
-
#
|
|
353
|
+
#endSession(): never {
|
|
354
|
+
this.#lastOrganization = this.#user?.organization ?? this.#lastOrganization;
|
|
333
355
|
this.#forget();
|
|
334
356
|
this.#set({ status: "signed-out", user: null, error: null });
|
|
335
357
|
throw new PhotonError("unauthorized", "Sign in to Photon first.");
|
|
@@ -403,7 +425,7 @@ function base64Url(bytes: Uint8Array): string {
|
|
|
403
425
|
}
|
|
404
426
|
|
|
405
427
|
/** A JWT's claims (not verified — read only for display and the org; network-api checks the token). */
|
|
406
|
-
function
|
|
428
|
+
export function claimsOf(jwt: string | undefined): Record<string, unknown> {
|
|
407
429
|
const payload = jwt?.split(".")[1];
|
|
408
430
|
if (!payload) return {};
|
|
409
431
|
try {
|
|
@@ -418,6 +440,7 @@ function decode(jwt: string | undefined): Record<string, unknown> {
|
|
|
418
440
|
const str = (value: unknown) => (typeof value === "string" ? value : null);
|
|
419
441
|
const signInError = (message: string) => new PhotonError("unauthorized", message);
|
|
420
442
|
const asError = (e: unknown) => (e instanceof PhotonError ? e : new PhotonError("network", String(e), true));
|
|
443
|
+
const wasSignedOutByRenewal = (e: unknown) => e instanceof PhotonError && e.kind === "unauthorized";
|
|
421
444
|
|
|
422
445
|
function read<T>(storage: Storage, key: string): T | null {
|
|
423
446
|
try {
|