gutenberg
3 years ago
account.js
1 year ago
consent-mapping.js
1 year ago
cookiebot-admin-script.js
1 year ago
dashboard.js
1 year ago
debug-page.js
1 year ago
jquery.tipTip.js
3 years ago
multiple-page.js
3 years ago
network-settings-page.js
1 year ago
prior-consent-settings.js
3 years ago
settings-page.js
1 year ago
support-page.js
3 years ago
account.js
429 lines
| 1 | const API_BASE_URL = 'https://api.ea.prod.usercentrics.cloud/v1'; |
| 2 | let authToken = ''; |
| 3 | |
| 4 | // Helper function to create FormData for AJAX calls |
| 5 | const createFormData = (action, data = {}) => { |
| 6 | const formData = new FormData(); |
| 7 | formData.append('action', action); |
| 8 | formData.append('nonce', cookiebot_account.nonce); |
| 9 | |
| 10 | Object.entries(data).forEach(([key, value]) => { |
| 11 | formData.append(key, value); |
| 12 | }); |
| 13 | |
| 14 | return formData; |
| 15 | }; |
| 16 | |
| 17 | // Function to check scan status |
| 18 | async function checkScanStatus(scanId) { |
| 19 | try { |
| 20 | const response = await fetch(`${API_BASE_URL}/scan/${scanId}`, { |
| 21 | method: 'GET', |
| 22 | headers: { |
| 23 | 'Accept': 'application/json', |
| 24 | 'Content-Type': 'application/json', |
| 25 | 'Authorization': `Bearer ${authToken}` |
| 26 | } |
| 27 | }); |
| 28 | |
| 29 | const data = await response.json(); |
| 30 | |
| 31 | if (data.status === 'DONE') { |
| 32 | await fetch(cookiebot_account.ajax_url, { |
| 33 | method: 'POST', |
| 34 | body: createFormData('cookiebot_store_scan_details', { |
| 35 | scan_id: scanId, |
| 36 | scan_status: 'DONE' |
| 37 | }), |
| 38 | credentials: 'same-origin' |
| 39 | }); |
| 40 | return; |
| 41 | } |
| 42 | |
| 43 | // Store both scan ID and status |
| 44 | await fetch(cookiebot_account.ajax_url, { |
| 45 | method: 'POST', |
| 46 | body: createFormData('cookiebot_store_scan_details', { |
| 47 | scan_id: scanId, |
| 48 | scan_status: 'IN_PROGRESS' |
| 49 | }), |
| 50 | credentials: 'same-origin' |
| 51 | }); |
| 52 | |
| 53 | if (!response.ok) { |
| 54 | await fetch(cookiebot_account.ajax_url, { |
| 55 | method: 'POST', |
| 56 | body: createFormData('cookiebot_store_scan_details', { |
| 57 | scan_id: scanId, |
| 58 | scan_status: 'FAILED' |
| 59 | }), |
| 60 | credentials: 'same-origin' |
| 61 | }); |
| 62 | } |
| 63 | } catch (error) { |
| 64 | console.error('Error checking scan status:', error); |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | const checkUserData = async () => { |
| 69 | try { |
| 70 | const userData = await fetch(`${API_BASE_URL}/accounts`, { |
| 71 | headers: { |
| 72 | 'Accept': 'application/json', |
| 73 | 'Authorization': `Bearer ${authToken}`, |
| 74 | } |
| 75 | }).then(r => r.json()).then(data => data[0]); |
| 76 | |
| 77 | if (!userData) throw new Error('No user data received'); |
| 78 | |
| 79 | // Add onboarding flag to user data |
| 80 | const userDataWithFlag = { |
| 81 | ...userData, |
| 82 | onboarded_via_signup: true |
| 83 | }; |
| 84 | userResponseData = await fetch(cookiebot_account.ajax_url, { |
| 85 | method: 'POST', |
| 86 | body: createFormData('cookiebot_post_user_data', { |
| 87 | data: JSON.stringify(userDataWithFlag) |
| 88 | }), |
| 89 | credentials: 'same-origin' |
| 90 | }).then(r => r.json()); |
| 91 | |
| 92 | if (!userResponseData.success) { |
| 93 | throw new Error('Failed to store user data'); |
| 94 | } |
| 95 | } catch (error) { |
| 96 | console.error('Failed to fetch user data:', error); |
| 97 | return; |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | const isAuthenticated = async () => { |
| 102 | try { |
| 103 | const response = await fetch(`${API_BASE_URL}/accounts`, { |
| 104 | headers: { |
| 105 | 'Accept': 'application/json', |
| 106 | 'Authorization': `Bearer ${authToken}` |
| 107 | } |
| 108 | }); |
| 109 | return response.status !== 401; |
| 110 | } catch (error) { |
| 111 | console.error('Failed to check authentication:', error); |
| 112 | return false; |
| 113 | } |
| 114 | }; |
| 115 | |
| 116 | // Function to fetch configuration details |
| 117 | async function fetchConfigurationDetails(configId) { |
| 118 | try { |
| 119 | const response = await fetch(`${API_BASE_URL}/configurations/${configId}`, { |
| 120 | method: 'GET', |
| 121 | headers: { |
| 122 | 'Accept': 'application/json', |
| 123 | 'Content-Type': 'application/json', |
| 124 | 'Authorization': `Bearer ${authToken}` |
| 125 | } |
| 126 | }); |
| 127 | |
| 128 | if (!response.ok) { |
| 129 | throw new Error('Failed to fetch configuration details'); |
| 130 | } |
| 131 | |
| 132 | const data = await response.json(); |
| 133 | |
| 134 | // Store full configuration data |
| 135 | await fetch(cookiebot_account.ajax_url, { |
| 136 | method: 'POST', |
| 137 | body: createFormData('cookiebot_store_configuration', { |
| 138 | configuration: JSON.stringify(data) |
| 139 | }), |
| 140 | credentials: 'same-origin' |
| 141 | }); |
| 142 | |
| 143 | return data; |
| 144 | } catch (error) { |
| 145 | console.error('Error fetching configuration details:', error); |
| 146 | throw error; |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | document.addEventListener('DOMContentLoaded', async () => { |
| 151 | const urlParams = new URLSearchParams(window.location.search); |
| 152 | const ucApiCode = urlParams.get('uc_api_code'); |
| 153 | |
| 154 | // For existing users, no account registration workflow is needed |
| 155 | if (cookiebot_account.has_cbid && !cookiebot_account.has_user_data) { |
| 156 | return; |
| 157 | } |
| 158 | |
| 159 | try { |
| 160 | authToken = await fetch(cookiebot_account.ajax_url, { |
| 161 | method: 'POST', |
| 162 | body: createFormData('cookiebot_get_auth_token'), |
| 163 | credentials: 'same-origin' |
| 164 | }).then(r => r.json()).then(data => data.data); |
| 165 | |
| 166 | if (ucApiCode) { |
| 167 | |
| 168 | const isNewUser = urlParams.get('is_new_user'); |
| 169 | |
| 170 | if (cookiebot_account.has_cbid) { |
| 171 | try { |
| 172 | const response = await fetch(cookiebot_account.ajax_url, { |
| 173 | method: 'POST', |
| 174 | body: createFormData('cookiebot_process_auth_code', { code: ucApiCode }), |
| 175 | credentials: 'same-origin' |
| 176 | }); |
| 177 | |
| 178 | authToken = await fetch(cookiebot_account.ajax_url, { |
| 179 | method: 'POST', |
| 180 | body: createFormData('cookiebot_get_auth_token'), |
| 181 | credentials: 'same-origin' |
| 182 | }).then(r => r.json()).then(data => data.data); |
| 183 | |
| 184 | if (!response.ok) throw new Error(`Auth failed: ${response.status}`); |
| 185 | const newUrl = new URL(window.location.href); |
| 186 | newUrl.searchParams.delete('uc_api_code'); |
| 187 | newUrl.searchParams.delete('is_new_user'); |
| 188 | window.location.href = newUrl; |
| 189 | return; |
| 190 | } catch (error) { |
| 191 | console.error('Failed to process authentication:', error); |
| 192 | return; |
| 193 | } |
| 194 | } |
| 195 | |
| 196 | |
| 197 | if (isNewUser === 'false' && !cookiebot_account.has_user_data) { |
| 198 | const settingsUrl = window.location.protocol + '//' + window.location.hostname + '/wp-admin/admin.php?page=cookiebot_settings'; |
| 199 | window.location.href = settingsUrl; |
| 200 | return; |
| 201 | } |
| 202 | |
| 203 | // Add loading state |
| 204 | const loadingOverlay = document.createElement('div'); |
| 205 | loadingOverlay.className = 'loading-overlay'; |
| 206 | loadingOverlay.innerHTML = ` |
| 207 | <div class="loading-content"> |
| 208 | <div class="loading-spinner"></div> |
| 209 | <h2>Creating your account</h2> |
| 210 | <p>This should only take about a minute. Keep this window open while we set things up.</p> |
| 211 | </div> |
| 212 | `; |
| 213 | document.body.classList.add('has-loading-overlay'); |
| 214 | document.body.appendChild(loadingOverlay); |
| 215 | |
| 216 | try { |
| 217 | const response = await fetch(cookiebot_account.ajax_url, { |
| 218 | method: 'POST', |
| 219 | body: createFormData('cookiebot_process_auth_code', { code: ucApiCode }), |
| 220 | credentials: 'same-origin' |
| 221 | }); |
| 222 | |
| 223 | authToken = await fetch(cookiebot_account.ajax_url, { |
| 224 | method: 'POST', |
| 225 | body: createFormData('cookiebot_get_auth_token'), |
| 226 | credentials: 'same-origin' |
| 227 | }).then(r => r.json()).then(data => data.data); |
| 228 | |
| 229 | if (!response.ok) throw new Error(`Auth failed: ${response.status}`); |
| 230 | } catch (error) { |
| 231 | console.error('Failed to process authentication:', error); |
| 232 | // Remove loading state on error |
| 233 | document.body.classList.remove('has-loading-overlay'); |
| 234 | loadingOverlay.remove(); |
| 235 | return; |
| 236 | } |
| 237 | } |
| 238 | |
| 239 | // Check for existing CBID |
| 240 | const cbid = await fetch(cookiebot_account.ajax_url, { |
| 241 | method: 'POST', |
| 242 | body: createFormData('cookiebot_get_cbid'), |
| 243 | credentials: 'same-origin' |
| 244 | }).then(r => r.json()).then(data => data.success ? data.data : null); |
| 245 | |
| 246 | // Skip if user data exists and is authenticated |
| 247 | const isAuthenticatedCondition = await isAuthenticated(); |
| 248 | if (cookiebot_account.has_user_data && isAuthenticatedCondition) { |
| 249 | // Check scan status before returning |
| 250 | await checkScanStatus(cbid); |
| 251 | await checkUserData(); |
| 252 | return; |
| 253 | } |
| 254 | |
| 255 | if (!isAuthenticatedCondition && cookiebot_account.has_user_data) { |
| 256 | const callbackUrl = window.location.protocol + '//' + window.location.hostname + '/wp-admin/admin.php?page=cookiebot'; |
| 257 | window.location.href = `${API_BASE_URL}/auth/auth0/authorize?origin=wordpress_plugin&callback_domain=${encodeURIComponent(callbackUrl)}`; |
| 258 | |
| 259 | } |
| 260 | |
| 261 | if (!cbid) { |
| 262 | try { |
| 263 | // Create new configuration |
| 264 | const siteDomain = window.location.hostname; |
| 265 | const formattedDomain = siteDomain.startsWith('http') ? siteDomain : `https://${siteDomain}`; |
| 266 | |
| 267 | const configResponse = await fetch(`${API_BASE_URL}/configurations`, { |
| 268 | method: 'POST', |
| 269 | headers: { |
| 270 | 'Accept': 'application/json', |
| 271 | 'Content-Type': 'application/json', |
| 272 | 'Authorization': `Bearer ${authToken}` |
| 273 | }, |
| 274 | body: JSON.stringify({ |
| 275 | configuration_name: "My CMP Configuration", |
| 276 | data_controller: "Usercentrics Unipessoal, LDA", |
| 277 | legal_framework_template: "gdpr", |
| 278 | domains: [formattedDomain] |
| 279 | }) |
| 280 | }); |
| 281 | |
| 282 | if (!configResponse.ok) throw new Error(`Config creation failed: ${configResponse.status}`); |
| 283 | |
| 284 | const configData = await configResponse.json(); |
| 285 | if (!configData?.configuration_id) throw new Error('No configuration ID'); |
| 286 | |
| 287 | // Store CBID |
| 288 | await fetch(cookiebot_account.ajax_url, { |
| 289 | method: 'POST', |
| 290 | body: createFormData('cookiebot_store_cbid', { cbid: configData.configuration_id }), |
| 291 | credentials: 'same-origin' |
| 292 | }); |
| 293 | |
| 294 | // Now that we have a CBID, initiate the scan |
| 295 | const scanResponse = await fetch(`${API_BASE_URL}/scan`, { |
| 296 | method: 'POST', |
| 297 | headers: { |
| 298 | 'Accept': 'application/json', |
| 299 | 'Content-Type': 'application/json', |
| 300 | 'Authorization': `Bearer ${authToken}` |
| 301 | }, |
| 302 | body: JSON.stringify({ |
| 303 | domains: [formattedDomain], |
| 304 | configuration_id: configData.configuration_id |
| 305 | }) |
| 306 | }); |
| 307 | |
| 308 | if (!scanResponse.ok) throw new Error(`Scan initiation failed: ${scanResponse.status}`); |
| 309 | |
| 310 | const scanData = await scanResponse.json(); |
| 311 | |
| 312 | // Check for scan ID in different possible response structures |
| 313 | const scanId = scanData?.scan?.scan_id || scanData?.scan_id || scanData?.id; |
| 314 | if (!scanId) { |
| 315 | console.error('Scan response structure:', scanData); |
| 316 | throw new Error('No scan ID received in response'); |
| 317 | } |
| 318 | |
| 319 | // Store scan ID in WordPress without status initially |
| 320 | await fetch(cookiebot_account.ajax_url, { |
| 321 | method: 'POST', |
| 322 | body: createFormData('cookiebot_store_scan_details', { |
| 323 | scan_id: scanId |
| 324 | }), |
| 325 | credentials: 'same-origin' |
| 326 | }); |
| 327 | |
| 328 | // Start checking scan status |
| 329 | await checkScanStatus(scanId); |
| 330 | |
| 331 | // fetch configuration details |
| 332 | await fetchConfigurationDetails(configData.configuration_id); |
| 333 | |
| 334 | } catch (error) { |
| 335 | console.error('Failed to create configuration:', error); |
| 336 | return; |
| 337 | } |
| 338 | } else { |
| 339 | // If we already have a CBID, check if there's an ongoing scan |
| 340 | const scanDetails = await fetch(cookiebot_account.ajax_url, { |
| 341 | method: 'POST', |
| 342 | body: createFormData('cookiebot_get_scan_details'), |
| 343 | credentials: 'same-origin' |
| 344 | }).then(r => r.json()); |
| 345 | |
| 346 | if (scanDetails.success && scanDetails.data.scan_id) { |
| 347 | await checkScanStatus(scanDetails.data.scan_id); |
| 348 | } |
| 349 | // And fetch configuration details |
| 350 | await fetchConfigurationDetails(cbid); |
| 351 | } |
| 352 | |
| 353 | // Check and fetch user data if needed |
| 354 | let userResponseData = await fetch(cookiebot_account.ajax_url, { |
| 355 | method: 'POST', |
| 356 | body: createFormData('cookiebot_get_user_data'), |
| 357 | credentials: 'same-origin' |
| 358 | }).then(r => r.json()); |
| 359 | |
| 360 | try { |
| 361 | const userData = await fetch(`${API_BASE_URL}/accounts`, { |
| 362 | headers: { |
| 363 | 'Accept': 'application/json', |
| 364 | 'Authorization': `Bearer ${authToken}`, |
| 365 | } |
| 366 | }).then(r => r.json()).then(data => data[0]); |
| 367 | |
| 368 | if (!userData) throw new Error('No user data received'); |
| 369 | |
| 370 | // Add onboarding flag to user data |
| 371 | const userDataWithFlag = { |
| 372 | ...userData, |
| 373 | onboarded_via_signup: true |
| 374 | }; |
| 375 | userResponseData = await fetch(cookiebot_account.ajax_url, { |
| 376 | method: 'POST', |
| 377 | body: createFormData('cookiebot_post_user_data', { |
| 378 | data: JSON.stringify(userDataWithFlag) |
| 379 | }), |
| 380 | credentials: 'same-origin' |
| 381 | }).then(r => r.json()); |
| 382 | |
| 383 | if (!userResponseData.success) { |
| 384 | throw new Error('Failed to store user data'); |
| 385 | } |
| 386 | } catch (error) { |
| 387 | console.error('Failed to fetch user data:', error); |
| 388 | return; |
| 389 | } |
| 390 | |
| 391 | if (userResponseData.data) { |
| 392 | const newUrl = new URL(window.location.href); |
| 393 | newUrl.searchParams.delete('uc_api_code'); |
| 394 | window.location.href = newUrl; |
| 395 | } |
| 396 | } catch (error) { |
| 397 | console.error('An unexpected error occurred:', error); |
| 398 | } |
| 399 | }); |
| 400 | |
| 401 | // Event Listeners |
| 402 | document.getElementById('banner-close-btn')?.addEventListener('click', async () => { |
| 403 | try { |
| 404 | const response = await fetch(cookiebot_account.ajax_url, { |
| 405 | method: 'POST', |
| 406 | body: createFormData('cookiebot_dismiss_banner'), |
| 407 | credentials: 'same-origin' |
| 408 | }); |
| 409 | |
| 410 | if (!response.ok) throw new Error(`Failed to dismiss banner: ${response.status}`); |
| 411 | const banner = document.getElementById('banner-live-notice') |
| 412 | if (banner) banner.remove(); |
| 413 | } catch (error) { |
| 414 | console.error('Failed to dismiss banner:', error); |
| 415 | } |
| 416 | }); |
| 417 | |
| 418 | document.getElementById('get-started-button')?.addEventListener('click', async (e) => { |
| 419 | e.preventDefault(); |
| 420 | try { |
| 421 | // If multisite is enabled the url might include also the directory for the site |
| 422 | // e.g.: http://domain/site1/wp-admin/admin.php?page=cookiebot |
| 423 | const callbackUrl = window.location.href.substring(0, window.location.href.indexOf('/wp-admin')) + '/wp-admin/admin.php?page=cookiebot'; |
| 424 | window.location.href = `${API_BASE_URL}/auth/auth0/authorize?origin=wordpress_plugin&callback_domain=${encodeURIComponent(callbackUrl)}`; |
| 425 | } catch (error) { |
| 426 | console.error('Failed to start authentication process:', error); |
| 427 | } |
| 428 | }); |
| 429 |