| 1 | // OAuth redirect landing (docs/connected-services.md). The broker relays the |
| 2 | // provider's one-time ?code (+ ?state) here — same origin as the app. We hand it |
| 3 | // back to the app to complete the exchange (the app holds the session/keys, the |
| 4 | // bot does the exchange via the broker), then return to it. |
| 5 | // |
| 6 | // Same-tab flow (the one we use): merge the code into the pending sessionStorage |
| 7 | // entry (verifying state), then navigate back to the app, which finishes on boot. |
| 8 | // Popup flow (kept for a possible future/native path): postMessage to the opener. |
| 9 | (function () { |
| 10 | var q = new URLSearchParams(location.search); |
| 11 | var code = q.get("code"); |
| 12 | var state = q.get("state"); |
| 13 | var error = q.get("error"); |
| 14 | // Billing flow (paid access levels): the broker already exchanged the code |
| 15 | // and hands back only the connected-account id — no code, no token. |
| 16 | var billingAccount = q.get("billing_account"); |
| 17 | if (window.opener) { |
| 18 | try { |
| 19 | window.opener.postMessage({ source: "lchat-oauth", code: code, state: state, error: error }, location.origin); |
| 20 | } catch (e) {} |
| 21 | window.close(); |
| 22 | return; |
| 23 | } |
| 24 | try { |
| 25 | var pend = JSON.parse(sessionStorage.getItem("lchat_oauth") || "null"); |
| 26 | if (error || (!code && !billingAccount)) { |
| 27 | // Keep the billing flag so the completion can word the failure right. |
| 28 | sessionStorage.setItem("lchat_oauth", JSON.stringify({ service: pend && pend.service, billing: pend && pend.billing, error: error || "no code" })); |
| 29 | } else if (pend && pend.state === state) { |
| 30 | if (code) pend.code = code; |
| 31 | if (billingAccount) pend.billingAccount = billingAccount; |
| 32 | sessionStorage.setItem("lchat_oauth", JSON.stringify(pend)); |
| 33 | } |
| 34 | } catch (e) {} |
| 35 | location.replace("/"); |
| 36 | })(); |
| 37 | |