@hanzo/app 0.2.41 → 0.2.42

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/dist/Setup.js CHANGED
@@ -31,6 +31,7 @@ import { enter } from './lib/destination.js';
31
31
  import { brand, site } from './where.js';
32
32
  import { GOOGLE_ACCOUNTS_KEY, GOOGLE_KEY, accounts, connect, drop, keep } from './lib/gmail.js';
33
33
  import { GOOD } from './lib/mix.js';
34
+ import { RETURN, awaitAnswer } from './lib/oauth.js';
34
35
  export const LATER_KEY = 'hanzo.onboarding.later';
35
36
  export const GITHUB_KEY = 'hanzo.onboarding.github';
36
37
  export const SLACK_KEY = 'hanzo.onboarding.slack';
@@ -199,20 +200,21 @@ export function Setup({ go } = {}) {
199
200
  const activeStep = step === 1 || step === 2 || step === 3 ? step : 1;
200
201
  if (later && !modalOpen)
201
202
  return null;
202
- const openOAuthPopup = (authorizeUrl, provider, onDone) => {
203
+ const openOAuthPopup = (authorizeUrl, provider, label, onDone) => {
203
204
  const popup = window.open(authorizeUrl, `hanzo-oauth-${provider}`, 'width=600,height=700');
204
205
  if (!popup) {
205
206
  window.open(authorizeUrl, '_blank', 'noopener,noreferrer');
207
+ return;
206
208
  }
207
- else {
208
- const timer = window.setInterval(() => {
209
- if (popup.closed) {
210
- window.clearInterval(timer);
211
- setSyncTurn((n) => n + 1);
212
- return;
213
- }
214
- }, 250);
215
- }
209
+ // The return page hands the answer back and closes the popup (lib/oauth);
210
+ // a popup closed without one still re-reads what is connected.
211
+ awaitAnswer(popup, provider, (answer) => {
212
+ if (answer?.ok)
213
+ onDone();
214
+ else if (answer)
215
+ setErrorMsg(t('Could not connect {provider} right now. Please try again.', { provider: label }));
216
+ setSyncTurn((n) => n + 1);
217
+ });
216
218
  };
217
219
  const connectOAuthProvider = async (provider, label, onDone) => {
218
220
  if (!signed) {
@@ -238,7 +240,9 @@ export function Setup({ go } = {}) {
238
240
  const res = await fetch(`${api()}/v1/provider/${provider}/connect`, {
239
241
  method: 'POST',
240
242
  headers: { ...scope(), 'Content-Type': 'application/json' },
241
- body: '{}',
243
+ // Without a return the callback lands on the console's /connectors,
244
+ // where the popup stays open; RETURN is the page that closes it.
245
+ body: JSON.stringify({ return: RETURN }),
242
246
  });
243
247
  if (!res.ok) {
244
248
  let detail = '';
@@ -257,7 +261,7 @@ export function Setup({ go } = {}) {
257
261
  }
258
262
  const j = (await res.json());
259
263
  if (j.authorizeUrl) {
260
- openOAuthPopup(j.authorizeUrl, provider, onDone);
264
+ openOAuthPopup(j.authorizeUrl, provider, label, onDone);
261
265
  }
262
266
  else if (j.connected) {
263
267
  onDone();
@@ -0,0 +1,43 @@
1
+ /**
2
+ * A provider's OAuth in a popup, and its answer back in the window that opened it.
3
+ *
4
+ * `/v1/provider/:provider/connect` answers an authorize URL; the provider sends the
5
+ * browser to api.hanzo.ai's callback, which 302s to the return page the connect
6
+ * named with `connected=<provider>&account=<label>` or `error=<provider>&reason=<why>`
7
+ * on its query. A connect that names no return lands on the console's /connectors,
8
+ * a whole other app on another origin: the popup stayed open there and the opener
9
+ * never heard. So the connect names `RETURN`, this site's registered return page
10
+ * (cloud brand.Info.Returns), where the site's first script (hanzo.ai app/page.tsx
11
+ * `RETURNED`) hands the answer to the opener and closes the popup.
12
+ *
13
+ * The answer arrives three ways, whichever reaches first: a `message` from the
14
+ * popup (same origin only), a `BroadcastChannel` post or a `storage` write for an
15
+ * opener the popup lost (`window.opener` is null after a cross-origin-isolated
16
+ * page). The opener also reads the popup's address itself once it is back on
17
+ * this origin, and closes it, so a popup that cannot close itself still does.
18
+ */
19
+ /** The return page the connect names: hanzo.ai's Settings → Integrations, registered with the API. */
20
+ export declare const RETURN = "https://hanzo.ai/?at=-/settings/integrations";
21
+ /** The message, channel and storage key one answer travels by. */
22
+ export declare const RETURNED = "hanzo-oauth-return";
23
+ /** What the provider answered. */
24
+ export type Answer = {
25
+ type: typeof RETURNED;
26
+ provider: string;
27
+ ok: true;
28
+ account: string;
29
+ } | {
30
+ type: typeof RETURNED;
31
+ provider: string;
32
+ ok: false;
33
+ reason: string;
34
+ };
35
+ /** The answer a return page's query carries, or null when it carries none. */
36
+ export declare function answered(search: string): Answer | null;
37
+ /** `data` as an answer for `provider`, or null when it is anything else. */
38
+ export declare function heard(data: unknown, provider: string): Answer | null;
39
+ /**
40
+ * Wait for `provider`'s answer to the popup. `done` is called once: with the
41
+ * answer, or with null when the popup closed without one. Answers the cleanup.
42
+ */
43
+ export declare function awaitAnswer(popup: Window, provider: string, done: (answer: Answer | null) => void): () => void;
@@ -0,0 +1,121 @@
1
+ /**
2
+ * A provider's OAuth in a popup, and its answer back in the window that opened it.
3
+ *
4
+ * `/v1/provider/:provider/connect` answers an authorize URL; the provider sends the
5
+ * browser to api.hanzo.ai's callback, which 302s to the return page the connect
6
+ * named with `connected=<provider>&account=<label>` or `error=<provider>&reason=<why>`
7
+ * on its query. A connect that names no return lands on the console's /connectors,
8
+ * a whole other app on another origin: the popup stayed open there and the opener
9
+ * never heard. So the connect names `RETURN`, this site's registered return page
10
+ * (cloud brand.Info.Returns), where the site's first script (hanzo.ai app/page.tsx
11
+ * `RETURNED`) hands the answer to the opener and closes the popup.
12
+ *
13
+ * The answer arrives three ways, whichever reaches first: a `message` from the
14
+ * popup (same origin only), a `BroadcastChannel` post or a `storage` write for an
15
+ * opener the popup lost (`window.opener` is null after a cross-origin-isolated
16
+ * page). The opener also reads the popup's address itself once it is back on
17
+ * this origin, and closes it, so a popup that cannot close itself still does.
18
+ */
19
+ /** The return page the connect names: hanzo.ai's Settings → Integrations, registered with the API. */
20
+ export const RETURN = 'https://hanzo.ai/?at=-/settings/integrations';
21
+ /** The message, channel and storage key one answer travels by. */
22
+ export const RETURNED = 'hanzo-oauth-return';
23
+ /** The answer a return page's query carries, or null when it carries none. */
24
+ export function answered(search) {
25
+ const q = new URLSearchParams(search);
26
+ const ok = q.get('connected');
27
+ if (ok)
28
+ return { type: RETURNED, provider: ok, ok: true, account: q.get('account') ?? '' };
29
+ const failed = q.get('error');
30
+ if (failed && q.has('reason'))
31
+ return { type: RETURNED, provider: failed, ok: false, reason: q.get('reason') ?? '' };
32
+ return null;
33
+ }
34
+ /** `data` as an answer for `provider`, or null when it is anything else. */
35
+ export function heard(data, provider) {
36
+ if (!data || typeof data !== 'object')
37
+ return null;
38
+ const a = data;
39
+ if (a.type !== RETURNED || a.provider !== provider || typeof a.ok !== 'boolean')
40
+ return null;
41
+ const text = (v) => (typeof v === 'string' ? v : '');
42
+ return a.ok ? { type: RETURNED, provider, ok: true, account: text(a.account) } : { type: RETURNED, provider, ok: false, reason: text(a.reason) };
43
+ }
44
+ /**
45
+ * Wait for `provider`'s answer to the popup. `done` is called once: with the
46
+ * answer, or with null when the popup closed without one. Answers the cleanup.
47
+ */
48
+ export function awaitAnswer(popup, provider, done) {
49
+ let over = false;
50
+ let channel = null;
51
+ const finish = (answer) => {
52
+ if (over)
53
+ return;
54
+ over = true;
55
+ stop();
56
+ try {
57
+ if (!popup.closed)
58
+ popup.close();
59
+ }
60
+ catch {
61
+ /* already gone */
62
+ }
63
+ done(answer);
64
+ };
65
+ const onMessage = (ev) => {
66
+ if (ev.origin !== window.location.origin)
67
+ return;
68
+ const a = heard(ev.data, provider);
69
+ if (a)
70
+ finish(a);
71
+ };
72
+ const onStorage = (ev) => {
73
+ if (ev.key !== RETURNED || !ev.newValue)
74
+ return;
75
+ try {
76
+ const a = heard(JSON.parse(ev.newValue), provider);
77
+ if (a)
78
+ finish(a);
79
+ }
80
+ catch {
81
+ /* not an answer */
82
+ }
83
+ };
84
+ const timer = window.setInterval(() => {
85
+ if (popup.closed) {
86
+ finish(null);
87
+ return;
88
+ }
89
+ try {
90
+ // Cross-origin (the provider, api.hanzo.ai) until the return page: reading throws.
91
+ if (popup.location.origin !== window.location.origin)
92
+ return;
93
+ const a = answered(popup.location.search);
94
+ if (a && a.provider === provider)
95
+ finish(a);
96
+ }
97
+ catch {
98
+ /* still away */
99
+ }
100
+ }, 250);
101
+ const stop = () => {
102
+ window.clearInterval(timer);
103
+ window.removeEventListener('message', onMessage);
104
+ window.removeEventListener('storage', onStorage);
105
+ channel?.close();
106
+ };
107
+ window.addEventListener('message', onMessage);
108
+ window.addEventListener('storage', onStorage);
109
+ if (typeof BroadcastChannel !== 'undefined') {
110
+ channel = new BroadcastChannel(RETURNED);
111
+ channel.onmessage = (ev) => {
112
+ const a = heard(ev.data, provider);
113
+ if (a)
114
+ finish(a);
115
+ };
116
+ }
117
+ return () => {
118
+ over = true;
119
+ stop();
120
+ };
121
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hanzo/app",
3
- "version": "0.2.41",
3
+ "version": "0.2.42",
4
4
  "description": "Hanzo's app: the shell and its apps (Home, Chat, Inbox, Contacts, Meet, Calendar, Drive, Board, Work, Bots), once, for every Hanzo surface.",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",