1(function () { 2 /** 3 * Logs messages for debugging 4 * @param {string} message 5 */ 6 function logMessage(message) { 7 console.log("[Verify ID Sync]: " + message); 8 } 9 10 /** 11 * Sends an AJAX request to update the user meta in WordPress. 12 * @param {string} metaKey - The key to be updated in the user's meta. 13 * @param {string} metaValue - The value to be stored. 14 */ 15 function updateUserMeta(metaKey, metaValue) { 16 logMessage("Updating user meta: " + metaKey); 17 18 var xhr = new XMLHttpRequest(); 19 var params = 20 "action=update_user_meta&meta_key=" + 21 encodeURIComponent(metaKey) + 22 "&meta_value=" + 23 encodeURIComponent(metaValue) + 24 "&nonce=" + 25 encodeURIComponent(ageVerificationDataUserSync.nonce); 26 27 xhr.open("POST", ageVerificationDataUserSync.ajax_url, true); 28 xhr.setRequestHeader( 29 "Content-Type", 30 "application/x-www-form-urlencoded; charset=UTF-8" 31 ); 32 33 xhr.onload = function () { 34 if (xhr.status >= 200 && xhr.status < 400) { 35 var response = JSON.parse(xhr.responseText); 36 if (response.success) { 37 logMessage("User meta updated successfully for " + metaKey); 38 } else { 39 logMessage("Failed to update user meta for " + metaKey); 40 } 41 } else { 42 logMessage("Server responded with an error for " + metaKey); 43 } 44 }; 45 46 xhr.onerror = function () { 47 logMessage("AJAX request failed while updating " + metaKey); 48 }; 49 50 xhr.send(params); 51 } 52 53 /** 54 * Removes DeviceID and Token from localStorage and User Meta. 55 */ 56 function removeDeviceIDAndToken() { 57 logMessage("Removing DeviceID and Token from localStorage and user meta."); 58 localStorage.removeItem("device_id"); 59 localStorage.removeItem("x_token_age_verified"); 60 updateUserMeta("device_id", ""); 61 updateUserMeta("token_age_verified", ""); 62 } 63 64 /** 65 * Validates the device and token with the auth check API to ensure data validity. 66 * If the age is returned as null, it indicates the data is invalid. 67 * @param {string} deviceID 68 * @param {string} token 69 * @param {Function} callback 70 */ 71 function validateWithAuthCheckAPI(deviceID, token, callback) { 72 logMessage("Validating DeviceID and Token with the auth check API."); 73 var xhr = new XMLHttpRequest(); 74 var apiURL = 75 "https://app.verifyid.dk/api/auth_check/" + 76 encodeURIComponent(token) + 77 "/" + 78 encodeURIComponent(deviceID); 79 80 xhr.open("POST", apiURL, true); 81 xhr.setRequestHeader("Content-Type", "application/json"); 82 83 xhr.onload = function () { 84 if (xhr.status >= 200 && xhr.status < 400) { 85 var response = JSON.parse(xhr.responseText); 86 logMessage("Auth Check API Response: " + JSON.stringify(response)); 87 88 // If {age: null}, the data is invalid 89 if (response.age === null) { 90 logMessage( 91 "Invalid data returned from auth check API. Removing DeviceID and Token." 92 ); 93 removeDeviceIDAndToken(); 94 } else { 95 logMessage("Valid age data received: " + response.age); 96 } 97 98 if (typeof callback === "function") { 99 callback(response); 100 } 101 } else { 102 logMessage("Error in response from auth check API."); 103 } 104 }; 105 106 xhr.onerror = function () { 107 logMessage("Auth check API request failed."); 108 }; 109 110 xhr.send(); 111 } 112 113 /** 114 * Initializes synchronization between localStorage and user meta. 115 */ 116 function initializeSync() { 117 var deviceID = localStorage.getItem("device_id"); 118 var token = localStorage.getItem("x_token_age_verified"); 119 120 // Check if the user is logged in and sync is needed 121 if (ageVerificationDataUserSync.is_user_logged_in) { 122 logMessage("User is logged in. Starting sync."); 123 124 // Case 1: If user has a device ID in user meta, use that and override localStorage 125 if (ageVerificationDataUserSync.device_id) { 126 logMessage("User meta contains device ID. Syncing to localStorage."); 127 localStorage.setItem( 128 "device_id", 129 ageVerificationDataUserSync.device_id 130 ); 131 document.cookie = `device_id=${ageVerificationDataUserSync.device_id}; path=/; expires=Wed, 24 Oct 2025 02:02:02 GMT`; 132 deviceID = ageVerificationDataUserSync.device_id; // Use this for further processing 133 134 // Case 2: If thereâs no device ID in user meta, check if token is present in localStorage 135 } else if (!ageVerificationDataUserSync.device_id && token) { 136 // If token exists but no device ID, remove both 137 logMessage( 138 "Token exists but no device ID. Removing both from localStorage and user meta." 139 ); 140 removeDeviceIDAndToken(); 141 return; // Exit the function since no further sync is needed 142 143 // Case 3: If no device ID in user meta and token is not set, generate a new device ID 144 } else if (!deviceID) { 145 logMessage("No device ID found in localStorage. Generating a new one."); 146 deviceID = generateUUID(); 147 localStorage.setItem("device_id", deviceID); 148 document.cookie = `device_id=${deviceID}; path=/; expires=Wed, 24 Oct 2025 02:02:02 GMT`; 149 } 150 151 // Sync token from user meta if not present in localStorage 152 if (!token && ageVerificationDataUserSync.token) { 153 logMessage("Setting token from user meta to localStorage."); 154 localStorage.setItem( 155 "x_token_age_verified", 156 ageVerificationDataUserSync.token 157 ); 158 document.cookie = `x_token_age_verified=${ageVerificationDataUserSync.token}; path=/; expires=Wed, 24 Oct 2025 02:02:02 GMT`; 159 token = ageVerificationDataUserSync.token; 160 } 161 162 // Sync any changes in localStorage back to user meta 163 if (deviceID !== ageVerificationDataUserSync.device_id) { 164 updateUserMeta("device_id", deviceID); 165 } 166 if (token !== ageVerificationDataUserSync.token) { 167 updateUserMeta("token_age_verified", token); 168 } 169 170 // Perform an auth check to validate the data 171 if (deviceID && token) { 172 validateWithAuthCheckAPI(deviceID, token); 173 } 174 } else { 175 logMessage("User is not logged in. No sync needed."); 176 } 177 } 178 179 /** 180 * Generates a new UUID for DeviceID. 181 * @returns {string} A new UUID. 182 */ 183 function generateUUID() { 184 return "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace( 185 /[xy]/g, 186 function (c) { 187 var r = (Math.random() * 16) | 0, 188 v = c === "x" ? r : (r & 0x3) | 0x8; 189 return v.toString(16); 190 } 191 ); 192 } 193 194 // Trigger synchronization when the document is ready 195 window.onload = function () { 196 logMessage("User sync script initialized."); 197 198 // Initialize the sync process 199 initializeSync(); 200 201 // Listen for changes in localStorage and sync to user meta if logged in 202 window.addEventListener("storage", function (event) { 203 if ( 204 ageVerificationDataUserSync.is_user_logged_in && 205 (event.key === "device_id" || event.key === "x_token_age_verified") 206 ) { 207 logMessage("LocalStorage changed: " + event.key); 208 updateUserMeta(event.key, event.newValue); 209 } 210 }); 211 }; 212})();
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.