@photonhealth/client 2.0.0-rc.2 → 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.
Files changed (42) hide show
  1. package/README.md +1 -1
  2. package/dist/analytics/lifecycleEvents.d.ts +5 -2
  3. package/dist/analytics/lifecycleEvents.d.ts.map +1 -1
  4. package/dist/analytics/lifecycleEvents.js +4 -1
  5. package/dist/analytics/lifecycleEvents.js.map +1 -1
  6. package/dist/{photon/analytics.d.ts → analytics/track.d.ts} +3 -3
  7. package/dist/analytics/track.d.ts.map +1 -0
  8. package/dist/{photon/analytics.js → analytics/track.js} +2 -2
  9. package/dist/analytics/track.js.map +1 -0
  10. package/dist/api/changes/applyOption.d.ts +4 -1
  11. package/dist/api/changes/applyOption.d.ts.map +1 -1
  12. package/dist/api/changes/applyOption.js +23 -2
  13. package/dist/api/changes/applyOption.js.map +1 -1
  14. package/dist/meta/version.js +1 -1
  15. package/dist/photon/index.d.ts +2 -2
  16. package/dist/photon/index.d.ts.map +1 -1
  17. package/dist/photon/index.js.map +1 -1
  18. package/dist/photon/order.d.ts +3 -4
  19. package/dist/photon/order.d.ts.map +1 -1
  20. package/dist/photon/order.js +6 -11
  21. package/dist/photon/order.js.map +1 -1
  22. package/dist/photon/photon.d.ts +1 -1
  23. package/dist/photon/photon.d.ts.map +1 -1
  24. package/dist/photon/photon.js +1 -1
  25. package/dist/photon/photon.js.map +1 -1
  26. package/dist/photon/session.d.ts +3 -2
  27. package/dist/photon/session.d.ts.map +1 -1
  28. package/dist/photon/session.js +22 -9
  29. package/dist/photon/session.js.map +1 -1
  30. package/package.json +1 -1
  31. package/src/analytics/lifecycleEvents.test.ts +25 -5
  32. package/src/analytics/lifecycleEvents.ts +20 -3
  33. package/src/analytics/track.test.ts +231 -0
  34. package/src/{photon/analytics.ts → analytics/track.ts} +2 -2
  35. package/src/api/changes/applyOption.ts +25 -2
  36. package/src/meta/version.ts +1 -1
  37. package/src/photon/index.ts +2 -1
  38. package/src/photon/order.ts +7 -12
  39. package/src/photon/photon.ts +1 -1
  40. package/src/photon/session.ts +23 -9
  41. package/dist/photon/analytics.d.ts.map +0 -1
  42. package/dist/photon/analytics.js.map +0 -1
@@ -21,7 +21,7 @@ import { PhotonError } from "./errors.ts";
21
21
  * stored. No refresh token from Auth0: nothing is kept, and the session lasts as long as the page.
22
22
  *
23
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.
24
+ * a session ending mid-work swaps the cards for Sign in (signing in again goes back to its organization).
25
25
  */
26
26
  export type SessionStatus = "starting" | "signed-out" | "signing-in" | "signed-in" | "error";
27
27
 
@@ -98,6 +98,7 @@ export class Session {
98
98
  #refreshToken: string | null = null;
99
99
  /** Whether storage holds the refresh token — then the newest one is read from there (another tab may rotate it). */
100
100
  #kept = false;
101
+ #lastOrganization: string | null = null;
101
102
  #renewing: Promise<string> | null = null;
102
103
  #started: Promise<void> | null = null;
103
104
  readonly #config: SessionConfig;
@@ -128,7 +129,8 @@ export class Session {
128
129
  /**
129
130
  * Sign in — from a click. Leaves the page for Auth0 (its organization picker; `organization`, or the page's
130
131
  * `?organization=`, goes straight in; `connection`, e.g. `"google-oauth2"`, goes straight to that identity provider,
131
- * past Auth0's page) and comes back here. Inside an iframe: a popup, then signed in here.
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.
132
134
  */
133
135
  async signIn(options: SignInOptions = {}): Promise<void> {
134
136
  this.#set({ status: "signing-in", error: null });
@@ -158,6 +160,7 @@ export class Session {
158
160
  /** Sign out: forget the session here, and end Auth0's (`signOutUrl`). Not something the cards offer. */
159
161
  signOut(options: { returnTo?: string } = {}): void {
160
162
  this.#forget();
163
+ this.#lastOrganization = null;
161
164
  this.#set({ status: "signed-out", user: null, error: null });
162
165
  this.#navigate(this.signOutUrl(options));
163
166
  }
@@ -177,7 +180,8 @@ export class Session {
177
180
  const state = randomString(16);
178
181
  const verifier = randomString(32);
179
182
  const redirectUri = window.location.origin;
180
- const organization = options.organization ?? new URLSearchParams(window.location.search).get("organization");
183
+ const organization =
184
+ options.organization ?? new URLSearchParams(window.location.search).get("organization") ?? this.#lastOrganization;
181
185
  const params = new URLSearchParams({
182
186
  response_type: "code",
183
187
  client_id: this.#config.clientId,
@@ -213,10 +217,18 @@ export class Session {
213
217
  const stored = this.#readStored();
214
218
  if (!stored) return this.#set({ status: "signed-out" });
215
219
  this.#user = stored.user;
216
- await this.#renew(); // the access token was never kept: every load starts with a refresh
220
+ await this.#renewStoredSession(); // the access token was never kept: every load starts with a refresh
217
221
  } catch (e) {
218
- // A stored session that couldn't be renewed is just signed out (#signedOut); anything else is an error.
219
- if (this.#snapshot.status !== "signed-out") this.#set({ status: "error", user: null, error: asError(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();
229
+ } catch (e) {
230
+ if (wasSignedOutByRenewal(e)) return;
231
+ throw e;
220
232
  }
221
233
  }
222
234
 
@@ -227,14 +239,14 @@ export class Session {
227
239
  #renew(): Promise<string> {
228
240
  return this.#oneTabAtATime(async () => {
229
241
  const refreshToken = this.#kept ? (this.#readStored()?.refreshToken ?? null) : this.#refreshToken;
230
- if (!refreshToken) return this.#signedOut(); // signed out here, or in another tab
242
+ if (!refreshToken) return this.#endSession(); // signed out here, or in another tab
231
243
  try {
232
244
  const tokens = await this.#tokenRequest({ grant_type: "refresh_token", refresh_token: refreshToken });
233
245
  this.#keep(tokens, refreshToken);
234
246
  return this.#access!.token;
235
247
  } catch (e) {
236
248
  // A refresh token Auth0 won't take (expired, revoked, rotated away): signed out — not an error.
237
- if (e instanceof PhotonError && e.kind === "unauthorized") return this.#signedOut();
249
+ if (e instanceof PhotonError && e.kind === "unauthorized") return this.#endSession();
238
250
  throw e;
239
251
  }
240
252
  });
@@ -338,7 +350,8 @@ export class Session {
338
350
  this.#set({ status: "signed-in", user: this.#user, error: null });
339
351
  }
340
352
 
341
- #signedOut(): never {
353
+ #endSession(): never {
354
+ this.#lastOrganization = this.#user?.organization ?? this.#lastOrganization;
342
355
  this.#forget();
343
356
  this.#set({ status: "signed-out", user: null, error: null });
344
357
  throw new PhotonError("unauthorized", "Sign in to Photon first.");
@@ -427,6 +440,7 @@ export function claimsOf(jwt: string | undefined): Record<string, unknown> {
427
440
  const str = (value: unknown) => (typeof value === "string" ? value : null);
428
441
  const signInError = (message: string) => new PhotonError("unauthorized", message);
429
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";
430
444
 
431
445
  function read<T>(storage: Storage, key: string): T | null {
432
446
  try {
@@ -1 +0,0 @@
1
- {"version":3,"file":"analytics.d.ts","sourceRoot":"","sources":["../../src/photon/analytics.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,mBAAmB,CAAC;AACzD,OAAO,EAA8B,KAAK,kBAAkB,EAAE,MAAM,iCAAiC,CAAC;AAEtG;;;GAGG;AACH,MAAM,MAAM,mBAAmB,GAAG,MAAM,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM,GAAG,OAAO,GAAG,IAAI,GAAG,MAAM,EAAE,CAAC,CAAC;AAE9F,oFAAoF;AACpF,MAAM,WAAW,eAAe;IAC9B,QAAQ,CAAC,GAAG,EAAE,MAAM,CAAC;IACrB,KAAK,IAAI,OAAO,CAAC,MAAM,CAAC,CAAC;IACzB,QAAQ,CAAC,UAAU,EAAE,MAAM,GAAG,IAAI,CAAC;IACnC;;;OAGG;IACH,QAAQ,CAAC,WAAW,CAAC,EAAE,MAAM,GAAG,IAAI,CAAC;CACtC;AAED,kGAAkG;AAClG,MAAM,WAAW,SAAU,SAAQ,eAAe,EAAE,k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