| @@ -13,9 +13,8 @@ | ||
| 13 | 13 | portalDomain, |
| 14 | 14 | portalEmail, |
| 15 | 15 | portalId, |
| 16 | 16 | reviewSkippedDate, |
| 17 | - refreshToken, | |
| 18 | 17 | impactLink, |
| 19 | 18 | theme, |
| 20 | 19 | lastAuthorizeTime, |
| 21 | 20 | lastDeauthorizeTime, |
| @@ -24,11 +23,12 @@ | ||
| 24 | 23 | phpVersion, |
| 25 | 24 | wpVersion, |
| 26 | 25 | contentEmbed, |
| 27 | 26 | requiresContentEmbedScope, |
| 28 | - refreshTokenError, | |
| 27 | + decryptError, | |
| 29 | 28 | LeadinConfig, |
| 30 | 29 | } from '../constants/leadinConfig'; |
| 30 | +import { fetchAccessToken } from '../api/wordpressApiClient'; | |
| 31 | 31 | import { App, AppIframe } from './constants'; |
| 32 | 32 | import { messageMiddleware } from './messageMiddleware'; |
| 33 | 33 | import { resizeWindow, useIframeNotRendered } from '../utils/iframe'; |
| 34 | 34 | |
| @@ -54,9 +54,9 @@ | ||
| 54 | 54 | | 'phpVersion' |
| 55 | 55 | | 'wpVersion' |
| 56 | 56 | | 'contentEmbed' |
| 57 | 57 | | 'requiresContentEmbedScope' |
| 58 | - | 'refreshTokenError' | |
| 58 | + | 'decryptError' | |
| 59 | 59 | >; |
| 60 | 60 | |
| 61 | 61 | type AppIntegrationConfig = Pick<LeadinConfig, 'adminUrl'>; |
| 62 | 62 | |
| @@ -127,9 +127,9 @@ | ||
| 127 | 127 | websiteName: leadinQueryParams.websiteName, |
| 128 | 128 | wpVersion, |
| 129 | 129 | contentEmbed, |
| 130 | 130 | requiresContentEmbedScope, |
| 131 | - refreshTokenError, | |
| 131 | + decryptError, | |
| 132 | 132 | ...utm_query_params, |
| 133 | 133 | }; |
| 134 | 134 | }; |
| 135 | 135 | |
| @@ -142,14 +142,12 @@ | ||
| 142 | 142 | }: any = window; |
| 143 | 143 | let options; |
| 144 | 144 | switch (app) { |
| 145 | 145 | case App.Plugin: |
| 146 | - options = new PluginAppOptions().setLeadinConfig(getLeadinConfig()); | |
| 146 | + options = new PluginAppOptions(); | |
| 147 | 147 | break; |
| 148 | 148 | case App.PluginSettings: |
| 149 | - options = new PluginAppOptions() | |
| 150 | - .setLeadinConfig(getLeadinConfig()) | |
| 151 | - .setPluginSettingsInit(); | |
| 149 | + options = new PluginAppOptions().setPluginSettingsInit(); | |
| 152 | 150 | break; |
| 153 | 151 | case App.Forms: |
| 154 | 152 | options = new FormsAppOptions().setIntegratedAppConfig( |
| 155 | 153 | getIntegrationConfig() |
| @@ -185,13 +183,18 @@ | ||
| 185 | 183 | |
| 186 | 184 | useEffect(() => { |
| 187 | 185 | const { IntegratedAppEmbedder }: any = window; |
| 188 | 186 | |
| 189 | - if (IntegratedAppEmbedder) { | |
| 187 | + if (!IntegratedAppEmbedder) { | |
| 188 | + return; | |
| 189 | + } | |
| 190 | + | |
| 191 | + const createEmbedder = (accessToken = '', expiresIn = 0) => { | |
| 190 | 192 | const options = getAppOptions(app, createRoute) |
| 191 | 193 | .setLocale(locale) |
| 192 | 194 | .setDeviceId(deviceId) |
| 193 | - .setRefreshToken(refreshToken); | |
| 195 | + .setAccessToken(accessToken, expiresIn) | |
| 196 | + .setLeadinConfig(getLeadinConfig()); | |
| 194 | 197 | |
| 195 | 198 | const embedder = new IntegratedAppEmbedder( |
| 196 | 199 | AppIframe[app], |
| 197 | 200 | portalId, |
| @@ -196,16 +199,38 @@ | ||
| 196 | 199 | AppIframe[app], |
| 197 | 200 | portalId, |
| 198 | 201 | hubspotBaseUrl, |
| 199 | 202 | resizeWindow, |
| 200 | - refreshToken ? '' : impactLink | |
| 203 | + accessToken ? '' : impactLink | |
| 201 | 204 | ).setOptions(options); |
| 202 | 205 | |
| 203 | - embedder.subscribe(messageMiddleware(embedder)); | |
| 206 | + embedder.subscribe((message: any) => { | |
| 207 | + messageMiddleware(embedder)(message); | |
| 208 | + }); | |
| 209 | + embedder.setTokenRenewalCallback(fetchAccessToken); | |
| 210 | + | |
| 204 | 211 | embedder.attachTo(container, true); |
| 205 | 212 | embedder.postStartAppMessage(); // lets the app know all all data has been passed to it |
| 206 | 213 | |
| 207 | 214 | (window as any).embedder = embedder; |
| 215 | + }; | |
| 216 | + | |
| 217 | + if (connectionStatus === 'Connected') { | |
| 218 | + fetchAccessToken() | |
| 219 | + .then( | |
| 220 | + ({ | |
| 221 | + accessToken, | |
| 222 | + expiresIn, | |
| 223 | + }: { | |
| 224 | + accessToken: string; | |
| 225 | + expiresIn: number; | |
| 226 | + }) => { | |
| 227 | + createEmbedder(accessToken, expiresIn); | |
| 228 | + } | |
| 229 | + ) | |
| 230 | + .catch(() => {}); | |
| 231 | + } else { | |
| 232 | + createEmbedder(); | |
| 208 | 233 | } |
| 209 | 234 | }, []); |
| 210 | 235 | |
| 211 | 236 | if (iframeNotRendered) { |
| @@ -223,9 +248,9 @@ | ||
| 223 | 248 | app, |
| 224 | 249 | hubspotBaseUrl, |
| 225 | 250 | impactLink, |
| 226 | 251 | appName: AppIframe[app], |
| 227 | - hasRefreshToken: !!refreshToken, | |
| 252 | + isConnected: connectionStatus === 'Connected', | |
| 228 | 253 | }, |
| 229 | 254 | }); |
| 230 | 255 | } |
| 231 | 256 | |