|
9 | 9 | <script> |
10 | 10 | console.log('Initializing example.'); |
11 | 11 |
|
12 | | - // Store the UID2 identity in localStorage |
13 | 12 | const UID2_STORAGE_KEY = '__uid2_advertising_token'; |
14 | 13 | let currentIdentity = null; |
15 | 14 |
|
|
35 | 34 | value: { |
36 | 35 | uid2: { |
37 | 36 | id: currentIdentity.advertising_token, |
38 | | - // Include additional fields for token refresh if available |
39 | 37 | ...(currentIdentity.refresh_token && { |
40 | 38 | refresh_token: currentIdentity.refresh_token, |
41 | 39 | refresh_from: currentIdentity.refresh_from, |
|
53 | 51 | }); |
54 | 52 | } |
55 | 53 |
|
56 | | - /** |
57 | | - * Update the UI elements based on current identity state |
58 | | - */ |
59 | 54 | function updateGuiElements() { |
60 | 55 | console.log('Updating displayed values.'); |
61 | 56 |
|
62 | | - // Check if user has opted out |
63 | 57 | const storedToken = localStorage.getItem(UID2_STORAGE_KEY); |
64 | 58 | let isOptedOut = false; |
65 | 59 |
|
|
90 | 84 | } |
91 | 85 | } |
92 | 86 |
|
93 | | - /** |
94 | | - * Handle login button click - call server to generate UID2 token |
95 | | - */ |
96 | 87 | async function handleLogin() { |
97 | 88 | const email = $('#email').val(); |
98 | 89 |
|
|
103 | 94 | console.log('Generating UID2 token for email:', email); |
104 | 95 |
|
105 | 96 | try { |
106 | | - // Call the server's /login endpoint |
107 | 97 | const response = await fetch('/login', { |
108 | 98 | method: 'POST', |
109 | 99 | headers: { |
|
114 | 104 |
|
115 | 105 | const data = await response.json(); |
116 | 106 |
|
117 | | - // Check for opt-out (server returns 200 with status: 'optout') |
118 | 107 | if (data.status === 'optout') { |
119 | 108 | console.log('UID2 status: optout'); |
120 | 109 | localStorage.setItem(UID2_STORAGE_KEY, JSON.stringify({ status: 'optout' })); |
|
123 | 112 | return; |
124 | 113 | } |
125 | 114 |
|
126 | | - // Check for other errors |
127 | 115 | if (!response.ok) { |
128 | 116 | console.error('Token generation failed:', data); |
129 | 117 | return; |
|
132 | 120 | console.log('UID2 token received:', data); |
133 | 121 |
|
134 | 122 | if (data.identity) { |
135 | | - // Store the identity |
136 | 123 | currentIdentity = data.identity; |
137 | 124 | localStorage.setItem(UID2_STORAGE_KEY, JSON.stringify(currentIdentity)); |
138 | 125 |
|
139 | | - // Configure Prebid with the new token |
140 | 126 | setPrebidConfig(); |
141 | 127 | } |
142 | 128 |
|
143 | | - // Update the UI |
144 | 129 | updateGuiElements(); |
145 | 130 |
|
146 | 131 | } catch (error) { |
147 | 132 | console.error('Error calling login endpoint:', error); |
148 | 133 | } |
149 | 134 | } |
150 | 135 |
|
151 | | - /** |
152 | | - * Handle clear storage button - clear UID2 token and refresh page |
153 | | - */ |
154 | 136 | function handleClearStorage() { |
155 | 137 | console.log('Clearing UID2 storage.'); |
156 | 138 | localStorage.removeItem(UID2_STORAGE_KEY); |
157 | 139 | currentIdentity = null; |
158 | 140 | location.reload(); |
159 | 141 | } |
160 | 142 |
|
161 | | - /** |
162 | | - * Load identity from localStorage on page load |
163 | | - */ |
164 | 143 | function loadStoredIdentity() { |
165 | 144 | const storedToken = localStorage.getItem(UID2_STORAGE_KEY); |
166 | 145 | if (storedToken) { |
|
178 | 157 | } |
179 | 158 | } |
180 | 159 |
|
181 | | - /** |
182 | | - * Initialize the page |
183 | | - */ |
184 | 160 | function onDocumentReady() { |
185 | 161 | console.log('Setting up interface handlers.'); |
186 | 162 |
|
187 | | - // Set up event handlers |
188 | 163 | $('#login').click(handleLogin); |
189 | 164 | $('#clear_storage').click(handleClearStorage); |
190 | 165 |
|
191 | | - // Allow Enter key to submit |
192 | | - $('#email').keypress(function(e) { |
193 | | - if (e.which === 13) { |
194 | | - handleLogin(); |
195 | | - } |
196 | | - }); |
197 | | - |
198 | | - // Load any stored identity |
199 | 166 | loadStoredIdentity(); |
200 | 167 |
|
201 | | - // Update UI |
202 | 168 | updateGuiElements(); |
203 | 169 | } |
204 | 170 |
|
|
0 commit comments