1 2(function ($, Drupal, drupalSettings) { 3 4 'use strict'; 5 6 7 // Avoid scope issues 8 let amgeeUpdatePassword; 9 10 // Drupal settings 11 let userPoolId, clientId, userPoolDomain, subDomain, domainName, amgee_benefits, benefits_login_settings, domain_id, production_value, config_cognito, env_value; 12 let cookieDomain; 13 14 Drupal.behaviors.amgee_membership = { 15 attach: function (context, settings) { 16 $(document).ready(function () { 17 // Your existing code that handles cookies goes here. 18 let cookies = checkCookies(); 19 if (!cookies) { 20 // Handle the case when cookies are blocked. 21 $('.signInButton').on('click', function (e) { 22 $('body').prepend('<div id="login-overlay"><p>Cookies are blocked in your browser.<br>You must allow cookies in order to sign in.</p></div>'); 23 }); 24 25 // Dismiss message 26 $('body').on('click', '#login-overlay', function (e) { 27 $(this).remove(); 28 }); 29 30 return; // Cookies are off 31 } 32 33 // Process Drupal settings and other logic... 34 }); 35 36 37 // Process Drupal settings 38 if (typeof drupalSettings !== 'undefined' && drupalSettings.amgee_membership) { 39 40 amgee_benefits = drupalSettings.amgee_membership.amgee_benefits; 41 benefits_login_settings = drupalSettings.amgee_membership.benefits_login_settings; 42 domain_id = drupalSettings.amgee_membership.domain_id; 43 production_value = drupalSettings.amgee_membership.production_value; 44 config_cognito = drupalSettings.amgee_membership.config_cognito; 45 userPoolId = drupalSettings.amgee_membership.userPoolId; 46 clientId = drupalSettings.amgee_membership.clientId; 47 userPoolDomain = drupalSettings.amgee_membership.userPoolDomain; 48 env_value = drupalSettings.amgee_membership.env_value; 49 50 // // Use test credentials if set 51 // if (amgee_benefits['is_production'] === "") { 52 // userPoolId = drupalSettings.amgee_membership.aws_cognito.test_userPoolId; 53 // clientId = drupalSettings.amgee_membership.aws_cognito.test_clientId; 54 // userPoolDomain = drupalSettings.amgee_membership.aws_cognito.test_userPoolDomain; 55 // } 56 } 57 58 // Don't run this script if the membership account does not have settings configured 59 if (userPoolId === "" || clientId === "" || userPoolDomain === "") { 60 return; 61 } 62 63 // If login benefit is configured 64 if (benefits_login_settings === 1) { 65 // Domain and subdomain for this site 66 let originalSubDomain = window.location.hostname; 67 let domainArray = originalSubDomain.split("."); 68 let subDomainWhole = domainArray.shift(); // Subdomain part 69 70 domainName = domainArray.join("."); // Domain part 71 72 if (subDomainWhole === "admin") { 73 subDomain = "www"; // Use www instead of admin 74 // } else if (subDomainWhole.indexOf("-") > -1 ) { 75 // // Strip off -admin part 76 // subDomain = subDomainWhole.slice(0, subDomainWhole.indexOf("-")); 77 } else { 78 subDomain = subDomainWhole; 79 } 80 81 // console.log("env value: " + env_value); 82 // Determine if cookie is for dev or production account 83 cookieDomain = ""; 84 if (env_value === null || env_value === "") { 85 cookieDomain = "dev-"; 86 } 87 // console.log("cookieDomain: " + cookieDomain);
88 // This cookie has been purged. Log out Cognito user! 89 let membershipCookie = {}; 90 let cid = null; 91 let email = null; 92 let loggedin = null; 93 membershipCookie = getMembershipCookie(membershipCookie); 94 cid = membershipCookie[cookieDomain + 'cid']; 95 email = membershipCookie[cookieDomain + 'email']; 96 loggedin = membershipCookie[cookieDomain + 'loggedin']; 97 // if ((cookieDomain === "" && 98 // (typeof membershipCookie["dev-cid"] !== 'undefined' && 99 // membershipCookie["cid"] !== null)) || 100 // (cookieDomain === "dev-" && 101 // (typeof membershipCookie["cid"] !== 'undefined' && 102 // membershipCookie["cid"] !== null))) { 103 // resetCognitoCookies(false); 104 // } 105 106 // // Destroy related Cognito cookies if not logged in 107 // if (loggedin === false) { 108 // resetCognitoCookies(true); 109 // } 110 111 let logout = false; 112 let hasCode = false; 113 let pathName = window.location.pathname; 114 if (pathName.slice(0, 11) === "/membership") { 115 116 let urlParams = new URLSearchParams(window.location.search); 117 if (urlParams.has('code')) { 118 hasCode = true; 119 } 120 } 121 122 if (typeof cid === 'undefined' && typeof email === 'undefined' && 123 hasCode === false) { 124 logout = true; 125 } 126 127 // Check if this is in the admin section of Drupal 128 let pathArr = pathName.split("/"); 129 // If on the admin side of the site, don't authenticate 130 // TODO : Need to remove later, temporarily check user should not be logged in, to solve homepage redirection issue by initCognitoSDK function 131 let userIsLoggedIn = drupalSettings.amgee_membership.userIsLoggedIn; 132 // console.log(pathArr.indexOf("admin") === -1 && userIsLoggedIn != 1); 133 if (pathArr.indexOf("admin") === -1 && userIsLoggedIn != true && pathArr.indexOf("amgee_classified") === -1 && originalSubDomain != 'd3ajohiefr89o1.cloudfront.net') { 134 135 // if(pathArr.indexOf("/node/add/amgee-classified")) 136 // Initialize CognitoAuth object 137 let auth = initCognitoSDK(logout); 138 139 $(once('amgee_membership', '#amgee_membership_login_block_home_page', context)).each(function () { 140 // $('#amgee_membership_login_block_home_page', context).once('amgee_membership').each(function () { 141 142 if ($('#amgee_membership_login_block_home_page').length > 0) { 143 144 amgee_membership_onLoad(auth); 145 } 146 147 // $('#amgee_membership_login_block_home_page').on('click', function() { 148 // let auth = initCognitoSDK(logout); 149 // amgee_membership_onLoad(auth); 150 // }); 151 }); 152 153 // plan page membership login link 154 $(once('amgee_membership', '#amgee_membership_login_block_plan_page', context)).each(function () { 155 // $('#amgee_membership_login_block_plan_page', context).once('amgee_membership').each(function () { 156 if ($('#amgee_membership_login_block_plan_page').length > 0) { 157 amgee_membership_onLoad(auth); 158 } 159 }); 160 161 162 // Pre-filled values for cognito/account edit page 163 $(once('amgee_membership', '#amgee-membership-account-form', context)).each(function () { 164 // $('#amgee-membership-account-form' , context).once('amgee_membership').each(function () { 165 // let auth = initCognitoSDK(logout); 166 amgee_membership_populate_form(auth, hasCode); 167 }); 168 169 // Pre-filled values for cognito/profile edit page 170 $(once('amgee_membership', '#amgee-membership-account-profile-form', context)).each(function () { 171 // $('#amgee-membership-account-profile-form' , context).once('amgee_membership').each(function () { 172 // let auth = initCognitoSDK(logout); 173 amgee_membership_populate_profile_form(auth, hasCode); 174 }); 175 176 $(once('amgee_membership', '#amgee-membership-account-password-form', context)).each(function () { 177 // $('#amgee-membership-account-password-form' , context).once('amgee_membership').each(function () { 178 // let auth = initCognitoSDK(logout); 179 amgee_membership_populate_password_form(auth, hasCode); 180 }); 181 182 183 184 // Redirect non-members to homepage if they hit member pages 185 if ((pathArr.indexOf("membership") === 1) && hasCode === false && !window.location.pathname.includes("/membership/sub
185scription/add")) { 186 amgee_membership_redirect_non_members(auth); 187 } 188 } 189 } 190 } // end attach 191 }; 192 193 // Update password form 194 amgeeUpdatePassword = function amgeeUpdatePassword() { 195 let oldPassword = $("#amgee-membership-password-form").find("input[name='old_password']").val(); 196 let newPassword = $("#amgee-membership-password-form").find("input[name='password[pass1]']").val(); 197 let poolData = { 198 UserPoolId: userPoolId, 199 ClientId: clientId, 200 Storage: new AmazonCognitoIdentity.CookieStorage({ 201 domain: "." + domainName 202 }), 203 }; 204 205 // Initialize CognitoAuth object 206 let auth = initCognitoSDK(false); 207 // Set user if signed in 208 let username = ""; 209 let idToken = auth.getSignInUserSession().getIdToken().getJwtToken(); 210 211 if (idToken) { 212 let payload = idToken.split('.')[1]; 213 let tokenobj = JSON.parse(atob(payload)); 214 215 if (tokenobj["cognito:username"] !== undefined) { 216 username = tokenobj["cognito:username"]; 217 } 218 } 219 let userPool = new AmazonCognitoIdentity.CognitoUserPool(poolData); 220 let userData = { 221 Username: username, 222 Pool: userPool, 223 Storage: new AmazonCognitoIdentity.CookieStorage({ 224 domain: "." + domainName 225 }), 226 }; 227 228 let cognitoUser = new AmazonCognitoIdentity.CognitoUser(userData); 229 230 if (cognitoUser != null) { 231 cognitoUser.getSession(function (err, session) { 232 if (err) { 233 if ($("#messages").length) { 234 $("#messages").append('<div class="messages error"><ul><li>Password update failed.</li></ul></div>'); 235 } else { 236 $("#breadcrumb").after('<div id="messages"><div class="messages error"><ul><li>Password update failed.</li></ul></div></div>'); 237 } 238 return; 239 } 240 }); 241 } 242 243 cognitoUser.changePassword(oldPassword, newPassword, function (err, result) { 244 if (err) { 245 if ($("#messages").length) { 246 $("#messages").append('<div class="messages error"><ul><li>Password update failed.</li></ul></div>'); 247 } else { 248 $("#breadcrumb").after('<div id="messages"><div class="messages error"><ul><li>Password update failed.</li></ul></div></div>'); 249 } 250 return; 251 } 252 if (result === "SUCCESS") { 253 if ($("#messages").length) { 254 $("#messages").append('<div class="messages status"><ul><li>Password update succeeded.</li></ul></div>'); 255 } else { 256 $("#breadcrumb").after('<div id="messages"><div class="messages status"><ul><li>Password update succeeded.</li></ul></div></div>'); 257 } 258 } 259 }); 260 261 }; 262 263 // Operations when the web page is loaded. 264 function amgee_membership_onLoad(auth) {
265 let curUrl = window.location.href; 266 auth.parseCognitoWebResponse(curUrl); 267 let i, signInButton; 268 269 signInButton = document.getElementsByClassName("signInButton"); 270 for (i = 0; i < signInButton.length; i++) { 271 signInButton[i].addEventListener("click", function () { 272 userButton(auth); 273 }); 274 } 275 } 276 277 // Kick non-members off membership pages 278 function amgee_membership_redirect_non_members(auth) { 279 let idToken = ""; 280 if (auth.getSignInUserSession()) { 281 idToken = auth.getSignInUserSession().getIdToken().getJwtToken(); 282 } 283 // No user logged in, redirect to homepage. 284 if (!idToken) { 285 // Popup some overlay thing 286 jQuery("body").prepend("<div id='login-overlay'><p>Account login error." + "</p></div>"); 287 // Redirect to homepage 288 window.location.href = "/"; 289 } 290 } 291 292 // Populate the edit membership form 293 function amgee_membership_populate_form(auth, hasCode) { 294 let idToken = ""; 295 // Set user if signed in 296 if (auth.getSignInUserSession()) { 297 idToken = auth.getSignInUserSession().getIdToken().getJwtToken(); 298 } 299 if (idToken) { 300 let payload = idToken.split('.')[1]; 301 let tokenobj = JSON.parse(atob(payload)); 302 console.log(tokenobj); 303 let nickname = ""; 304 if (tokenobj.nickname !== undefined) { 305 nickname = tokenobj.nickname; 306 } 307 let cid = ""; 308 if (tokenobj['custom:cid'] !== undefined) { 309 cid = tokenobj['custom:cid']; 310 } 311 312 // Populate edit membership form fields 313 if (document.getElementById('edit-id')) { 314 if (cid.length === 0) { 315 // There is no stripe customer data for this record so force to the 316 // Amgeee subscription process 317 window.location.href = "/membership-member-product?email=" + 318 encodeURIComponent(tokenobj.email); 319 } 320 document.getElementById('display-id').innerHTML = cid; 321 322 if (document.getElementsByName('cid')) { 323 let cidElm = document.getElementsByName("cid"); 324 for (let i = 0; i < cidElm.length; i++) { 325 cidElm[i].value = cid; 326 } 327 } 328 } 329 if (document.getElementById('edit-email')) { 330 document.getElementById('edit-email').value = tokenobj.email; 331 } 332 if (document.getElementById('edit-nickname')) { 333 document.getElementById('edit-nickname').value = nickname; 334 } 335 if (document.getElementsByName('username')) { 336 let username = document.getElementsByName("username"); 337 for (let i = 0; i < username.length; i++) { 338 username[i].value = tokenobj['cognito:username']; 339 } 340 } 341 } 342 } 343 344 // Populate the edit membership profile form 345 function amgee_membership_populate_profile_form(auth, hasCode) { 346 let idToken = ""; 347 // Set user if signed in 348 if (auth.getSignInUserSession()) { 349 idToken = auth.getSignInUserSession().getIdToken().getJwtToken(); 350 } 351 if (idToken) { 352 let payload = idToken.split('.')[1]; 353 let tokenobj = JSON.parse(atob(payload)); 354 console.log(tokenobj); 355 let nickname = ""; 356 if (tokenobj.nickname !== undefined) { 357 nickname = tokenobj.nickname; 358 } 359 let cid = ""; 360 if (tokenobj['custom:cid'] !== undefined) { 361 cid = tokenobj['custom:cid']; 362 } 363 364 // Populate edit membership form fields 365 if (document.getElementById('edit-id')) { 366 367 document.getElementById('display-id').innerHTML = cid; 368 document.getElementsByName('member_id')[0].value = cid; 369 370 if (document.getElementsByName('cid')) { 371 let cidElm = document.getElementsByName("cid"); 372 for (let i = 0; i < cidElm.length; i++) { 373 cidElm[i].value = cid; 374 } 375 } 376 } 377 } 378 } 379 380 // Populate the edit membership profile form 381 function amgee_membership_populate_password_form(auth, hasCode) { 382 let idToken = ""; 383 // Set user if signed in 384 if (auth.getSignInUserSession()) { 385 idToken = auth.getSignInUserSession().getIdToken().getJwtToken(); 386 } 387 if (idToken) { 388 let payload = idToken.split('.')[1]; 389 let tokenobj = JSON.parse(atob(payload)); 390 console.log(tokenobj); 391 let nickname = ""; 392 if (tokenobj.nickname !== undefined) { 393 nickname = tokenobj.nickname; 394 } 395 let cid = ""; 396 if (tokenobj['custom:cid'] !== undefined) { 397 cid = tokenobj['custom:cid']; 398 } 399 400 // Populate edit membership form fields 401 if (document.getElementsByName('cid')) { 402 403 document.getElementById('display-id').innerHTML = cid; 404 document.getElementsByName('cid')[0].value = cid; 405 406 407 if (document.getElementsByName('cid')) { 408 let cidElm = document.getElementsByName("cid"); 409 for (let i = 0; i < cidElm.length; i++) { 410 cidElm[i].value = cid; 411 } 412 } 413 } 414 415 if(document.getElementsByName('username')) { 416 document.getElementsByName('username')[0].value = tokenobj['cognito:username']; 417 } 418 } 419 } 420 421 422 // Perform user operations. 423 function userButton(auth) { 424 let signInButton = document.getElementsByClassName("signInButton"); 425 let state = signInButton[0].innerHTML; 426 427 for (let i = 0; i < signInButton.length; i++) { 428 signInButton[i].addEventListener("click", function () { 429 userButton(auth); 430 }); 431 } 432 433 if (state === "Sign Out") { 434 for (let i = 0; i < signInButton.length; i++) { 435 signInButton[i].innerHTML = "Sign In"; 436 } 437 auth.signOut(); 438 destroy_session(); 439 showSignedOut(); 440 // Redirect to homepage 441 let pathName = window.location.pathname; 442 if (pathName.slice(0, 11) === "/membership") { 443 window.location.href = "/"; 444 } 445 } else { 446 window.location.href = "https://" + userPoolDomain + 447 "/login?client_id=" + clientId + "&response_type=code" + 448 "&scope=aws.cognito.signin.user.admin+email+openid+phone+profile" + 449 "&redirect_uri=https://" + subDomain + "." + domainName + "/membership/"; 450 } 451 } 452 453 // Operations when signed in. 454 function showSignedIn(session) { 455 if (session) { 456 let idToken = session.getIdToken().getJwtToken(); 457 // Set logged in cookie and show nickname 458 updateMembershipCookie(idToken); 459 showNickname(idToken); 460 let payload = idToken.split('.')[1]; 461 let tokenobj = JSON.parse(atob(payload)); 462 signin_session_created(tokenobj['custom:cid']); 463 } 464 465 let statusNotAuth = document.getElementsByClassName("statusNotAuth"); 466 for (let i = 0; i < statusNotAuth.length; i++) { 467 statusNotAuth[i].style.display = 'none'; 468 } 469 let statusAuth = document.getElementsByClassName("statusAuth"); 470 for (let i = 0; i < statusAuth.length; i++) { 471 statusAuth[i].style.display = 'block'; 472 } 473 let signInButton = document.getElementsByClassName("signInButton"); 474 for (let i = 0; i < signInButton.length; i++) { 475 signInButton[i].innerHTML = "Sign Out"; 476 }
477 let createAccountButton = document.getElementsByClassName("createAccountWrapper"); 478 for (let i = 0; i < createAccountButton.length; i++) { 479 createAccountButton[i].style.display = 'none'; 480 } 481 482 // Display membership content 483 let member_content = document.getElementsByClassName("member_content"); 484 for (let i = 0; i < member_content.length; i++) { 485 member_content[i].style.visibility = 'visible'; 486 } 487 488 489 } 490 491 function signin_session_created(cid) { 492 $.ajax({ 493 url: '/set-cid', 494 type: 'GET', 495 data: { cid: cid }, // Replace with your desired CID. 496 dataType: 'json', 497 success: function (response) { 498 console.log(response.message); // Log the success message. 499 }, 500 error: function (xhr, status, error) { 501 console.log('AJAX error:', error); 502 } 503 }); 504 505 // Hide the "Become a Supporter" menu 506 $('.menu .become-supporter').hide(); 507 508 } 509 510 function destroy_session() { 511 $.ajax({ 512 url: '/destroy-session', // The endpoint for destroying the session 513 type: 'POST', // Using POST is more secure for destroying sessions 514 dataType: 'json', 515 success: function (response) { 516 console.log(response.message); // Log the success message 517 // You can also handle additional UI updates or redirects here 518 }, 519 error: function (xhr, status, error) { 520 console.log('AJAX error:', error); 521 } 522 }); 523 524 // Show the "Become a Supporter" menu 525 $('.menu .become-supporter').hide(); 526 } 527 528 // Operations when signed out. 529 function showSignedOut() { 530 let statusNotAuth = document.getElementsByClassName("statusNotAuth"); 531 for (let i = 0; i < statusNotAuth.length; i++) { 532 statusNotAuth[i].style.display = 'block'; 533 } 534 let statusAuth = document.getElementsByClassName("statusAuth"); 535 for (let i = 0; i < statusAuth.length; i++) { 536 statusAuth[i].style.display = 'none'; 537 } 538 updateMembershipCookie(); 539 } 540 541 // Initialize a Cognito auth object. 542 function initCognitoSDK(logoutUser = false) { 543 544 let storage = new AmazonCognitoIdentity.CookieStorage({ 545 domain: domainName 546 }); 547 let authData = { 548 ClientId: clientId, 549 AppWebDomain: userPoolDomain, 550 TokenScopesArray: ['phone', 'email', 'profile', 'openid', 'aws.cognito.signin.user.admin'], 551 RedirectUriSignIn: 'https://' + subDomain + "." + domainName + '/membership/', 552 RedirectUriSignOut: 'https://' + subDomain + "." + domainName, 553 UserPoolId: userPoolId, 554 Storage: storage, 555 }; 556 let auth = new AmazonCognitoIdentityAuth.CognitoAuth(authData); 557 auth.userhandler = { 558 onSuccess: function (result) { 559 showSignedIn(result); 560 }, 561 onFailure: function (err) { 562 console.log("Error!" + err); 563 // Popup some overlay thing 564 jQuery("body").prepend("<div id='login-overlay'><p>Account login error.</p></div>"); 565 auth.signOut(); 566 destroy_session(); 567 updateMembershipCookie(); 568 return auth; 569 } 570 }; 571 572 auth.useCodeGrantFlow(); 573 console.log('logoutUser', logoutUser); 574 575 let userCheck = auth.getCurrentUser(); 576 if (logoutUser && userCheck) { 577 auth.signOut(); 578 destroy_session(); 579 updateMembershipCookie(); 580 } 581 if (!logoutUser && userCheck) { 582 auth.getSession(); 583 } 584 585 return auth; 586 587 } 588 589 function showNickname(idToken) { 590 if (idToken) { 591 let payload = idToken.split('.')[1]; 592 let tokenobj = JSON.parse(atob(payload)); 593 let tokNick = tokenobj.nickname; 594 if (tokNick === undefined) { 595 tokNick = "member"; 596 } 597 598 // Display user's nickname 599 let nickname = document.getElementsByClassName("membership_nickname"); 600 for (let i = 0; i < nickname.length; i++) { 601 nickname[i].innerHTML = tokNick; 602 } 603 } 604 } 605 606 function updateMembershipCookie(idToken) {
607 let cookies = checkCookies(); initCognitoSDK 608 let cid = null; 609 if (!cookies) { 610 return; // Cookies are off. Can't update 611 } 612 let membershipCookie = {}; 613 membershipCookie = getMembershipCookie(membershipCookie); 614 // let expires = setCookieExpiry(365); 615 let expires = setCookieExpiry(10); 616 let reset = false; 617 if (idToken) { // Set cid and email to logged in user 618 let email; 619 let payload = idToken.split('.')[1]; 620 let tokenobj = JSON.parse(atob(payload)); 621 622 if (tokenobj['custom:cid'] !== undefined) { 623 cid = tokenobj['custom:cid']; 624 } 625 if (tokenobj["email"] !== undefined) { 626 email = tokenobj["email"]; 627 } 628 629 let membershipCid = membershipCookie[cookieDomain + "cid"]; 630 631 if (cid && cid !== membershipCid) { 632 reset = true; 633 } 634 // Overwrite cid and email 635 membershipCookie[cookieDomain + "cid"] = cid; 636 membershipCookie[cookieDomain + "email"] = email; 637 membershipCookie[cookieDomain + "loggedin"] = true; 638 639 // Update cookie 640 document.cookie = "amgeeMembership=" + encodeURIComponent(JSON.stringify(membershipCookie)) + "; expires=" + expires + "; path=/; domain=." + domainName + ";"; 641 642 } else { 643 console.log("click on logout. deleteing cookie"); 644 reset = true; 645 // Default logged out values 646 membershipCookie[cookieDomain + "cid"] = null; 647 membershipCookie[cookieDomain + "email"] = null; 648 membershipCookie[cookieDomain + "loggedin"] = false; 649 membershipCookie[cookieDomain + "auth"] = false; // Immediately end auth 650 651 document.cookie = "amgeeMembership=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=/; domain=." + domainName + ";"; 652 } 653 654 // // Update cookie 655 // document.cookie = "amgeeMembership=" + 656 // encodeURIComponent(JSON.stringify(membershipCookie)) + 657 // "; expires=" + expires + "; path=/; domain=." + domainName + ";"; 658 659 660 // Reload so cookies (this and AWS) are set 661 if (reset === true) { 662 resetCookies(cid); 663 } 664 } 665 666 function getMembershipCookie(existingCookie) { 667 if (getCookie("amgeeMembership")) { 668 existingCookie = decodeURIComponent(getCookie("amgeeMembership")); 669 existingCookie = JSON.parse(existingCookie); 670 } 671 return existingCookie; 672 } 673 674 function setCookieExpiry(days) { 675 let d = new Date(); 676 d.setTime(d.getTime() + (days * 24 * 60 * 60 * 1000)); 677 // d.setTime(d.getTime() + (minutes * 60 * 1000)); // Convert minutes to milliseconds 678 return d.toUTCString(); 679 } 680 681 function getCookie(cname) { 682 let name = cname + "=";
683 let ca = document.cookie.split(';'); 684 for (let i = 0; i < ca.length; i++) { 685 let c = ca[i]; 686 while (c.charAt(0) === ' ') { 687 c = c.substring(1); 688 } 689 if (c.indexOf(name) === 0) { 690 return c.substring(name.length, c.length); 691 } 692 } 693 return ""; 694 } 695 696 function resetCognitoCookies(all) { 697 let cookies = document.cookie.split(";"); 698 let reset = false; 699 for (let i = 0; i < cookies.length; i++) { 700 let cookie = cookies[i]; 701 let eqPos = cookie.indexOf("="); 702 let name = eqPos > -1 ? cookie.slice(0, eqPos) : cookie; 703 if (name.indexOf('CognitoIdentityServiceProvider') === 1) { 704 if (all === false) { 705 if (name.indexOf(clientId) === -1) { // Test for dev vs prod 706 reset = true; 707 document.cookie = name + 708 "=;expires=Thu, 01 Jan 1970 00:00:00 GMT;path=/;domain=." + 709 domainName + ";"; 710 } 711 } else { // Reset all cognito cookies 712 reset = true; 713 document.cookie = name + 714 "=;expires=Thu, 01 Jan 1970 00:00:00 GMT;path=/;domain=." + 715 domainName + ";"; 716 } 717 } 718 } 719 720 if (reset === true) { 721 let membershipCookie = {}; 722 membershipCookie = getMembershipCookie(membershipCookie); 723 let membershipCid = membershipCookie[cookieDomain + "cid"]; 724 let expires = setCookieExpiry(365); 725 // let expires = setCookieExpiry(10); 726 727 if (cookieDomain === "dev-") { 728 // Default logged out values 729 membershipCookie["cid"] = null; 730 membershipCookie["email"] = null; 731 membershipCookie["loggedin"] = false; 732 membershipCookie["auth"] = false; // Immediately end auth 733 } else { 734 // Default logged out values 735 membershipCookie["dev-cid"] = null; 736 membershipCookie["dev-email"] = null; 737 membershipCookie["dev-loggedin"] = false; 738 membershipCookie["dev-auth"] = false; // Immediately end auth 739 } 740 741 // Update cookie 742 document.cookie = "amgeeMembership=" + 743 encodeURIComponent(JSON.stringify(membershipCookie)) + 744 "; expires=" + expires + "; path=/; domain=." + domainName + ";"; 745 746 resetCookies(membershipCid); 747 } 748 } 749 750 function resetCookies(cid = null) { 751 // Popup some overlay thing 752 753 let pathName = window.location.pathname; 754 // console.log(pathName); 755 if (pathName.slice(0, 11) === "/membership") { 756 jQuery("body").prepend("<div id='login-overlay'><p>Please wait...</p></div>"); 757 if (cid !== undefined) { 758 window.location = "/membership"; 759 } else { 760 // window.location = "/"; 761 } 762 } else { // todo: is this necessary on any other page than membership? 763 // window.location.reload(); 764 } 765 } 766 767 // Check to see if cookies are on - memberships can't function without 768 function checkCookies() { 769 // Check to see if user accepts cookies and test it. 770 let cookies = true; 771 if (!navigator.cookieEnabled) { 772 return false; 773 } 774 var expires = setCookieExpiry(1); 775 document.cookie = "testcookie=yes; expires=" + expires + "; path=/"; 776 777 document.cookie = "testcookie";
778 if (document.cookie.indexOf("testcookie") === -1) { 779 cookies = false; 780 } else { 781 var expires = setCookieExpiry(-1); 782 document.cookie = "testcookie=yes; expires=" + expires + "; path=/"; 783 } 784 return cookies; 785 } 786 787})(jQuery, Drupal, drupalSettings);
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.