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