1window.onload = function () { 2 let isUserLoggedIn = googleLogin.isUserLoggedIn; 3 const isHomepage = googleLogin.isHomePage; 4 const nonce = googleLogin.nonce; 5 const ajaxUrl = googleLogin.ajaxUrl; 6 // Get the backend URL from localized variable 7 const backendUrl = googleLogin.backend_url; 8 9 google.accounts.id.initialize({ 10 client_id: googleLogin.clientId, 11 callback: handleCredentialResponse 12 }); 13 14 // If the user isn't logged in and isn't on the homepage, show One Tap prompt 15 if (!isUserLoggedIn && !isHomepage) { 16 console.log('login', isUserLoggedIn); 17 google.accounts.id.prompt(); 18 } 19 20 // Function to trigger the Google One Tap prompt 21 function showGooglePrompt() { 22 // Add login check before showing prompt 23 if (!isUserLoggedIn && !isHomepage) { 24 google.accounts.id.prompt(); 25 } 26 } 27 28 // Add event listeners to specific buttons or elements to show Google prompt 29 addEventListenersToElements('prepshow-btn', showGooglePrompt); 30 addEventListenersToElements('overly-home', showGooglePrompt); 31}; 32 33function navigateWithCacheBust(targetUrl, paramKey) { 34 var key = paramKey || 'refresh'; 35 try { 36 var url = new URL(targetUrl, window.location.origin); 37 if (url.origin === window.location.origin) { 38 url.searchParams.set(key, Date.now()); 39 } 40 window.location.assign(url.toString()); 41 } catch (error) { 42 var separator = targetUrl && targetUrl.indexOf('?') !== -1 ? '&' : '?'; 43 window.location.href = (targetUrl || window.location.href) + separator + key + '=' + Date.now(); 44 } 45} 46 47// Function to handle the response from the One Tap prompt 48function handleCredentialResponse(response) { 49 sendIdTokenToServer(response, googleLogin.ajaxUrl); 50} 51 52// Function to send ID token to the server 53function sendIdTokenToServer(response, ajaxUrl) { 54 jQuery.ajax({ 55 url: ajaxUrl, 56 type: 'POST', 57 data: { 58 action: 'handle_google_one_tap_login', 59 id_token: response.credential, 60 user_location: response.state, 61 security: googleLogin.nonce // Added nonce for extra security 62 }, 63 success: function(result) { 64 if (result.success) { 65 const user = result.data; 66 const redirectUrl = user.redirectUrl; 67 68 // Handle login or user creation based on the data returned 69 if (user.email && user.prep_id) { 70 loginUserGoogle(user.email, user.prep_id, user.phoneNumber, user.yearOfPass, redirectUrl); 71 } else { 72 createUserGoogle(user.email, user.user_location, redirectUrl); 73 } 74 } else { 75 console.error('Error:', result.data.message); 76 } 77 }, 78 error: function(error) { 79 console.error('Error during AJAX request:', error); 80 } 81 }); 82} 83 84// Helper function to add event listeners to multiple elements 85function addEventListenersToElements(className, callback) { 86 const elements = document.getElementsByClassName(className); 87 for (let i = 0; i < elements.length; i++) { 88 elements[i].addEventListener('click', callback); 89 } 90} 91 92// Function to log the user in via the socialLogin API 93function loginUserGoogle(email, prep_id, phoneNumber, yearOfPass, redirectUrl) { 94 const backendUrl = googleLogin.backend_url; 95 fetch(`${backendUrl}api/v2/user/web/socialLogin`, { 96 method: 'POST', 97 credentials: 'include', 98 headers: { 99 'Content-Type': 'application/json', 100 'Authorization': 'Basic cHJlcEluc3RhOnByZXAxMjNASW5zdGE=' 101 }, 102 body: JSON.stringify({ 103 user_email: email, 104 prep_id: prep_id, 105 year_of_passing: yearOfPass, 106 user_acquisition_source: 'From Prepinsta google Button', 107 phone: phoneNumber, 108 user_type: 3 109 }) 110 }) 111 .then(response => response.json()) 112 .then(data => { 113 if (data.status === 200) { 114 createdUserToken= data.res.accessToken; 115 var prepinstaId = data.res.user.PrepId;
116 setAccessTokenCookie(createdUserToken); 117 isUserLoggedIn = true; 118 google.accounts.id.cancel(); 119 loginUserAfterCreation(prepinstaId, redirectUrl); 120 Moengage.identifyUser({ u_em: data.res.user.user_email }); 121 } else { 122 alert('Login failed. Please try again.'); 123 } 124 }) 125 .catch(error => { 126 console.error('Error during social login:', error); 127 alert('There was an error during the login process.'); 128 }); 129} 130 131// Function to create a new user via the socialSignup API 132function createUserGoogle(email, user_location, redirectUrl) { 133 const backendUrl = googleLogin.backend_url; 134 const apiUrl = `${backendUrl}api/v2/user/web/socialSignup`; 135 136 const payload = { 137 user_email: email, 138 user_acquisition_source: user_location, 139 user_type: 3 // Assuming user type is '3' 140 }; 141 142 fetch(apiUrl, { 143 method: 'POST', 144 credentials: 'include', 145 headers: { 146 'Content-Type': 'application/json', 147 'Authorization': 'Basic cHJlcEluc3RhOnByZXAxMjNASW5zdGE=' 148 }, 149 body: JSON.stringify(payload) 150 }) 151 .then(response => response.json()) 152 .then(data => { 153 if (data.status === 200) { 154 const prepinstaId = data.res.user.PrepId;
155 setAccessTokenCookie(data.res.accessToken); 156 isUserLoggedIn = true; 157 google.accounts.id.cancel(); 158 loginUserAfterCreation(prepinstaId, redirectUrl); 159 // Track the event in MoEngage 160 userData = data.res.user; 161 Moengage.add_unique_user_id(userData.user_email); 162 Moengage.add_email(userData.user_email); 163 Moengage.add_mobile(userData.phone); 164 Moengage.add_user_attribute("year_of_passing", userData.year_of_passing); 165 Moengage.track_event("signup", { 166 user_id: userData.PrepId, 167 user_email: userData.user_email, 168 user_phone: userData.phone, 169 }); 170 } else { 171 alert('Error creating user.'); 172 } 173 }) 174 .catch(error => { 175 console.error('Error during user creation:', error); 176 alert('There was an error during user creation'); 177 }); 178} 179 180// Function to handle login after user creation 181function loginUserAfterCreation(prepinstaId, redirectUrl) { 182 const prepinstaIds = { userid: prepinstaId }; 183 jQuery.ajax({ 184 url: googleLogin.ajaxUrl, 185 type: 'POST', 186 data: { 187 action: 'process_user_login', 188 security: googleLogin.nonce, 189 action_type: 'loginUser', 190 data: prepinstaIds 191 }, 192 success: function(response) { 193 if (response.success) { 194 navigateWithCacheBust(redirectUrl, 'loggedin'); 195 } else { 196 console.error('Login failed:', response.data.message); 197 } 198 }, 199 error: function(err) { 200 console.error('Login error:', err); 201 } 202 }); 203} 204 205// Function to set the access token in cookies 206function setAccessTokenCookie(token) { 207 const days = 180; 208 const date = new Date(); 209 date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000)); 210 const expires = "expires=" + date.toUTCString(); 211 212 document.cookie = "accessToken=" + encodeURIComponent(token) + ";" + expires + "; path=/"; 213}
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.