gw-customer-accounts 1.3.8 → 1.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/sign-in.js CHANGED
@@ -1 +1 @@
1
- import{G as l,a as c}from"./assets/index-DkUQsudo.js";customElements.define("gw-customer-accounts-sign-in",class extends HTMLElement{constructor(){super(),this.signInType=this.getAttribute("type"),this.signInBlock=this.querySelector("[data-sign-in-block]"),this.signInType!=="password"&&(this.verifyCodeBlock=this.querySelector("[data-verify-code-block]"),this.tryAnotherEmailButton=this.querySelector("[data-try-another-email]"),this.sendAnotherCodeButton=this.querySelector("[data-send-another-code]"),this.inputtedEmail="",this.inputtedCode="")}connectedCallback(){this.signInType==="password"?this._setupPasswordBasedAuth():this._setupEmailBasedAuth()}_setupPasswordBasedAuth(){const r={signIn:this.querySelector("[data-sign-in-form]"),registration:this.querySelector("[data-registeration-form]"),forgotPassword:this.querySelector("[data-forgot-password-form]")},s={signIn:this.querySelectorAll("[data-sign-in-link]"),createAccount:this.querySelectorAll("[data-create-account-link]"),forgotPassword:this.querySelectorAll("[data-forgot-password-link]")};this._setupFormNavigation(s,r),this._setupFormSubmissions(Object.values(r)),this._handleFormVisibility(r)}_handleFormVisibility(r){const e=new URLSearchParams(window.location.search).get("form_id");e&&Object.values(r).forEach(t=>{const i=t.querySelector(".gw-error-message");e===t.getAttribute("id")?(t&&t.removeAttribute("hidden"),i&&i.removeAttribute("hidden"),e==="recover_customer"&&this._showPasswordResetMessage(t),e==="customer_login"&&this._showLoginErrorMessage(t)):(t&&t.setAttribute("hidden",""),i&&i.setAttribute("hidden",""))})}_showPasswordResetMessage(r){if(r.querySelector("[data-password-reset-message]"))return;const e=document.createElement("div");e.setAttribute("data-password-reset-message",""),e.className="gw-info-message",e.textContent="If we find your account in our system, you will receive a password reset link to your email address.";const t=r.querySelector('button[type="submit"]');r.insertBefore(e,t)}_showLoginErrorMessage(r){if(r.querySelector("[data-login-error-message]"))return;const e=document.createElement("div");e.setAttribute("data-login-error-message",""),e.className="gw-error-message",e.textContent="The email or password you entered is incorrect. Please try again.";const t=r.querySelector('button[type="submit"]');r.insertBefore(e,t)}_setupFormNavigation(r,s){const e=t=>{Object.values(s).forEach(i=>{i.setAttribute("hidden","")}),t.removeAttribute("hidden")};r.createAccount.forEach(t=>{t.addEventListener("click",()=>e(s.registration))}),r.forgotPassword.forEach(t=>{t.addEventListener("click",()=>e(s.forgotPassword))}),r.signIn.forEach(t=>{t.addEventListener("click",()=>e(s.signIn))})}_setupFormSubmissions(r){r.forEach(s=>{const e=new URL(window.location.href);e.searchParams.append("my-account-open",!0),e.searchParams.append("form_id",s.getAttribute("id"));const t=s.querySelector('input[name="return_to"]'),i=s.querySelector('input[name="return_url"]');t&&(t.value=e.pathname+e.search),i&&(i.value=e.pathname+e.search),s.addEventListener("submit",n=>{n.preventDefault(),s.querySelectorAll('button[type="submit"]').forEach(a=>{a.disabled=!0;const o=a.querySelector("[data-loader]");o&&(o.hidden=!1)}),Shopify.captcha.protect(s,()=>{s.submit()})})})}_setupEmailBasedAuth(){this._handleEmailSubmitForm(),this._handleInputCodeGroup(),this.tryAnotherEmailButton.addEventListener("click",()=>{this._resetToEmailInput()}),this.sendAnotherCodeButton.addEventListener("click",()=>{this._resendVerificationCode()})}_resetToEmailInput(){this.signInBlock.removeAttribute("hidden"),this.verifyCodeBlock.setAttribute("hidden","");const r=this.signInBlock.querySelector("sl-input");r.value="",r.focus(),this.inputtedEmail="",this.inputtedCode="",this.acceptsMarketing=!1}_resendVerificationCode(){this._submitEmailForm(this.querySelector("[data-sign-in-form]")),l.notify("Another code has been sent to your email.")}async _handleEmailSubmitForm(){await Promise.all([customElements.whenDefined("sl-input")]).then(()=>{const r=this.querySelector("[data-sign-in-form]"),s=this.querySelector("[data-email-error]");r.addEventListener("sl-invalid",e=>{e.preventDefault(),s.textContent=e.target.validationMessage,s.hidden=!1,e.target.focus()},{capture:!0}),r.addEventListener("submit",e=>{e.preventDefault(),s.hidden=!0,s.textContent="",this._submitEmailForm(r).then(()=>{const t=this.verifyCodeBlock.querySelector(".code-input");t&&t.focus()})})})}async _submitEmailForm(r){const s=r.querySelector('button[type="submit"]');s.disabled=!0,s.querySelector("[data-loader]").hidden=!1;const e=new FormData(r);this.inputtedEmail=e.get("email"),e.has("accepts_marketing")&&(this.acceptsMarketing=e.get("accepts_marketing"));let t="";const i=await this._fetchCaptchaToken();i&&(t=i);try{const n=await this._request({customerEmail:this.inputtedEmail,token:t},c.ENDPOINTS.AUTH.SIGN_IN);if(n.error){console.error(n.error);return}const a=this.querySelector("[data-code-sent-to]");return a&&(a.textContent=this.inputtedEmail),this.signInBlock.setAttribute("hidden",""),this.verifyCodeBlock.removeAttribute("hidden"),n}catch(n){console.error(n),alert("An error occurred. Please try again later.")}finally{s.disabled=!1,s.querySelector("[data-loader]").hidden=!0}}async _submitVerificationCode(){var n,a;const r=this.querySelector(".gw-verify-code__actions"),s=r.querySelectorAll("button, a"),e=r.querySelector("[data-verify-error]"),t=r.querySelector("[data-loader]"),i=this.querySelectorAll("input.code-input");try{i.forEach(d=>{d.disabled=!0}),s.forEach(d=>{d.disabled=!0,d.hidden=!0}),e.hidden=!0,t.hidden=!1;const o=await this._request({customerEmail:this.inputtedEmail,code:this.inputtedCode,is_marketing:this.acceptsMarketing},c.ENDPOINTS.AUTH.VERIFY_CODE);if(!o.url)throw new Error("Verification failed");localStorage.setItem(c.STORAGE_KEYS.TOKEN,o.token),localStorage.setItem(c.STORAGE_KEYS.EMAIL,this.inputtedEmail);const u=await fetch(o.url);if(u.error)throw new Error(u.error);const f=await(await fetch(window.location.href)).text(),h=new DOMParser().parseFromString(f,"text/html"),m=(a=(n=h.querySelector("#gw-customer-accounts-template").content)==null?void 0:n.querySelector("gw-integrations-loyalty-points"))==null?void 0:a.getAttribute("page-url");document.documentElement.innerHTML=h.documentElement.innerHTML,document.dispatchEvent(new CustomEvent("gw-customer-accounts:login"));const p=sessionStorage.getItem(c.STORAGE_KEYS.REDIRECT_URL);if(p&&(sessionStorage.removeItem(c.STORAGE_KEYS.REDIRECT_URL),window.location.href=p),!m)return;window.location.pathname.includes(m)&&window.location.reload()}catch(o){console.error(o),setTimeout(()=>{i[i.length-1].focus()})}finally{i.forEach(o=>{o.disabled=!1}),s.forEach(o=>{o.disabled=!1,o.hidden=!1}),e.hidden=!1,t.hidden=!0}}async _request(r,s,e="POST"){return await(await fetch(l.baseURL+s,{method:e,body:JSON.stringify(r),mode:"cors",cache:"no-cache",headers:{"Content-Type":"application/json"}})).json()}_handleInputCodeGroup(){const r=[...this.querySelectorAll("input.code-input")];r.forEach((s,e)=>{s.addEventListener("keydown",t=>{(t.ctrlKey||t.metaKey)&&t.keyCode===86||(t.keyCode!==8&&(t.keyCode<48||t.keyCode>57)&&t.preventDefault(),t.keyCode===8&&t.target.value===""&&r[Math.max(0,e-1)].focus())}),s.addEventListener("input",t=>{const[i,...n]=t.target.value;t.target.value=i??"";const a=e===r.length-1,o=i!==void 0;o&&!a&&(r[e+1].focus(),r[e+1].value=n.join(""),r[e+1].dispatchEvent(new Event("input"))),o&&a&&r.every(u=>u.value)&&(this.inputtedCode=r.map(u=>u.value).join(""),this._submitVerificationCode())})})}async _fetchCaptchaToken(){const s=await(await fetch(l.baseURL+c.ENDPOINTS.AUTH.GET_CAPTCHA_TOKEN)).json();return s==null?void 0:s.token}});
1
+ import{G as l,a as c}from"./assets/index-DkUQsudo.js";customElements.define("gw-customer-accounts-sign-in",class extends HTMLElement{constructor(){super(),this.signInType=this.getAttribute("type"),this.signInBlock=this.querySelector("[data-sign-in-block]"),this.signInType!=="password"&&(this.verifyCodeBlock=this.querySelector("[data-verify-code-block]"),this.tryAnotherEmailButton=this.querySelector("[data-try-another-email]"),this.sendAnotherCodeButton=this.querySelector("[data-send-another-code]"),this.inputtedEmail="",this.inputtedCode="")}connectedCallback(){this.signInType==="password"?this._setupPasswordBasedAuth():this._setupEmailBasedAuth()}_setupPasswordBasedAuth(){const t={signIn:this.querySelector("[data-sign-in-form]"),registration:this.querySelector("[data-registeration-form]"),forgotPassword:this.querySelector("[data-forgot-password-form]")},e={signIn:this.querySelectorAll("[data-sign-in-link]"),createAccount:this.querySelectorAll("[data-create-account-link]"),forgotPassword:this.querySelectorAll("[data-forgot-password-link]")};this._setupFormNavigation(e,t),this._setupFormSubmissions(Object.values(t)),this._handleFormVisibility(t)}_handleFormVisibility(t){const o=new URLSearchParams(window.location.search).get("form_id");o&&Object.values(t).forEach(r=>{const i=r.querySelector(".gw-error-message");o===r.getAttribute("id")?(r&&r.removeAttribute("hidden"),i&&i.removeAttribute("hidden"),o==="recover_customer"&&this._showPasswordResetMessage(r),o==="customer_login"&&this._showLoginErrorMessage(r)):(r&&r.setAttribute("hidden",""),i&&i.setAttribute("hidden",""))})}_showPasswordResetMessage(t){const e=t.querySelector("[data-forgot-password-error-message]");e&&(e.textContent="If we find your account in our system, you will receive a password reset link to your email address.")}_showLoginErrorMessage(t){const e=t.querySelector("[data-login-error-message]");e&&(e.textContent="The email or password you entered is incorrect. Please try again.")}_setupFormNavigation(t,e){const o=i=>{Object.values(e).forEach(n=>{n.setAttribute("hidden","")}),i.removeAttribute("hidden")},r=()=>{const i=new URL(window.location.href);i.searchParams.delete("form_id"),window.history.replaceState({},"",i.toString())};t.createAccount.forEach(i=>{i.addEventListener("click",()=>{o(e.registration),r()})}),t.forgotPassword.forEach(i=>{i.addEventListener("click",()=>{o(e.forgotPassword),r()})}),t.signIn.forEach(i=>{i.addEventListener("click",()=>{o(e.signIn),r()})})}_setupFormSubmissions(t){t.forEach(e=>{const o=new URL(window.location.href);o.searchParams.append("my-account-open",!0),o.searchParams.append("form_id",e.getAttribute("id"));const r=e.querySelector('input[name="return_to"]'),i=e.querySelector('input[name="return_url"]');r&&(r.value=o.pathname+o.search),i&&(i.value=o.pathname+o.search),e.addEventListener("submit",n=>{n.preventDefault(),e.querySelectorAll('button[type="submit"]').forEach(a=>{a.disabled=!0;const s=a.querySelector("[data-loader]");s&&(s.hidden=!1)}),Shopify.captcha.protect(e,()=>{e.submit()})})})}_setupEmailBasedAuth(){this._handleEmailSubmitForm(),this._handleInputCodeGroup(),this.tryAnotherEmailButton.addEventListener("click",()=>{this._resetToEmailInput()}),this.sendAnotherCodeButton.addEventListener("click",()=>{this._resendVerificationCode()})}_resetToEmailInput(){this.signInBlock.removeAttribute("hidden"),this.verifyCodeBlock.setAttribute("hidden","");const t=this.signInBlock.querySelector("sl-input");t.value="",t.focus(),this.inputtedEmail="",this.inputtedCode="",this.acceptsMarketing=!1}_resendVerificationCode(){this._submitEmailForm(this.querySelector("[data-sign-in-form]")),l.notify("Another code has been sent to your email.")}async _handleEmailSubmitForm(){await Promise.all([customElements.whenDefined("sl-input")]).then(()=>{const t=this.querySelector("[data-sign-in-form]"),e=this.querySelector("[data-email-error]");t.addEventListener("sl-invalid",o=>{o.preventDefault(),e.textContent=o.target.validationMessage,e.hidden=!1,o.target.focus()},{capture:!0}),t.addEventListener("submit",o=>{o.preventDefault(),e.hidden=!0,e.textContent="",this._submitEmailForm(t).then(()=>{const r=this.verifyCodeBlock.querySelector(".code-input");r&&r.focus()})})})}async _submitEmailForm(t){const e=t.querySelector('button[type="submit"]');e.disabled=!0,e.querySelector("[data-loader]").hidden=!1;const o=new FormData(t);this.inputtedEmail=o.get("email"),o.has("accepts_marketing")&&(this.acceptsMarketing=o.get("accepts_marketing"));let r="";const i=await this._fetchCaptchaToken();i&&(r=i);try{const n=await this._request({customerEmail:this.inputtedEmail,token:r},c.ENDPOINTS.AUTH.SIGN_IN);if(n.error){console.error(n.error);return}const a=this.querySelector("[data-code-sent-to]");return a&&(a.textContent=this.inputtedEmail),this.signInBlock.setAttribute("hidden",""),this.verifyCodeBlock.removeAttribute("hidden"),n}catch(n){console.error(n),alert("An error occurred. Please try again later.")}finally{e.disabled=!1,e.querySelector("[data-loader]").hidden=!0}}async _submitVerificationCode(){var n,a;const t=this.querySelector(".gw-verify-code__actions"),e=t.querySelectorAll("button, a"),o=t.querySelector("[data-verify-error]"),r=t.querySelector("[data-loader]"),i=this.querySelectorAll("input.code-input");try{i.forEach(u=>{u.disabled=!0}),e.forEach(u=>{u.disabled=!0,u.hidden=!0}),o.hidden=!0,r.hidden=!1;const s=await this._request({customerEmail:this.inputtedEmail,code:this.inputtedCode,is_marketing:this.acceptsMarketing},c.ENDPOINTS.AUTH.VERIFY_CODE);if(!s.url)throw new Error("Verification failed");localStorage.setItem(c.STORAGE_KEYS.TOKEN,s.token),localStorage.setItem(c.STORAGE_KEYS.EMAIL,this.inputtedEmail);const d=await fetch(s.url);if(d.error)throw new Error(d.error);const f=await(await fetch(window.location.href)).text(),h=new DOMParser().parseFromString(f,"text/html"),m=(a=(n=h.querySelector("#gw-customer-accounts-template").content)==null?void 0:n.querySelector("gw-integrations-loyalty-points"))==null?void 0:a.getAttribute("page-url");document.documentElement.innerHTML=h.documentElement.innerHTML,document.dispatchEvent(new CustomEvent("gw-customer-accounts:login"));const p=sessionStorage.getItem(c.STORAGE_KEYS.REDIRECT_URL);if(p&&(sessionStorage.removeItem(c.STORAGE_KEYS.REDIRECT_URL),window.location.href=p),!m)return;window.location.pathname.includes(m)&&window.location.reload()}catch(s){console.error(s),setTimeout(()=>{i[i.length-1].focus()})}finally{i.forEach(s=>{s.disabled=!1}),e.forEach(s=>{s.disabled=!1,s.hidden=!1}),o.hidden=!1,r.hidden=!0}}async _request(t,e,o="POST"){return await(await fetch(l.baseURL+e,{method:o,body:JSON.stringify(t),mode:"cors",cache:"no-cache",headers:{"Content-Type":"application/json"}})).json()}_handleInputCodeGroup(){const t=[...this.querySelectorAll("input.code-input")];t.forEach((e,o)=>{e.addEventListener("keydown",r=>{(r.ctrlKey||r.metaKey)&&r.keyCode===86||(r.keyCode!==8&&(r.keyCode<48||r.keyCode>57)&&r.preventDefault(),r.keyCode===8&&r.target.value===""&&t[Math.max(0,o-1)].focus())}),e.addEventListener("input",r=>{const[i,...n]=r.target.value;r.target.value=i??"";const a=o===t.length-1,s=i!==void 0;s&&!a&&(t[o+1].focus(),t[o+1].value=n.join(""),t[o+1].dispatchEvent(new Event("input"))),s&&a&&t.every(d=>d.value)&&(this.inputtedCode=t.map(d=>d.value).join(""),this._submitVerificationCode())})})}async _fetchCaptchaToken(){const e=await(await fetch(l.baseURL+c.ENDPOINTS.AUTH.GET_CAPTCHA_TOKEN)).json();return e==null?void 0:e.token}});
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "gw-customer-accounts",
3
- "version": "1.3.8",
3
+ "version": "1.4.0",
4
4
  "description": "",
5
5
  "main": "gw-customer-accounts.js",
6
6
  "type": "module",
@@ -75,13 +75,6 @@ customElements.define(
75
75
  if (formId === 'customer_login') {
76
76
  this._showLoginErrorMessage(form);
77
77
  }
78
-
79
- // Remove query params from URL
80
- // window.history.replaceState(
81
- // {},
82
- // document.title,
83
- // window.location.pathname
84
- // );
85
78
  } else {
86
79
  if (form) form.setAttribute('hidden', '');
87
80
  if (errorBox) errorBox.setAttribute('hidden', '');
@@ -90,35 +83,17 @@ customElements.define(
90
83
  }
91
84
 
92
85
  _showPasswordResetMessage(form) {
93
- // Check if message already exists to avoid duplicates
94
- const existingMessage = form.querySelector('[data-password-reset-message]');
95
- if (existingMessage) return;
96
-
97
- // Create the message element
98
- const messageElement = document.createElement('div');
99
- messageElement.setAttribute('data-password-reset-message', '');
100
- messageElement.className = 'gw-info-message';
101
- messageElement.textContent = 'If we find your account in our system, you will receive a password reset link to your email address.';
102
-
103
- // Insert the message at the beginning of the form
104
- const formSubmitButton = form.querySelector('button[type="submit"]');
105
- form.insertBefore(messageElement, formSubmitButton);
86
+ const errorMessageContainer = form.querySelector('[data-forgot-password-error-message]');
87
+ if (errorMessageContainer) {
88
+ errorMessageContainer.textContent = 'If we find your account in our system, you will receive a password reset link to your email address.';
89
+ };
106
90
  }
107
91
 
108
92
  _showLoginErrorMessage(form) {
109
- // Check if message already exists to avoid duplicates
110
- const existingMessage = form.querySelector('[data-login-error-message]');
111
- if (existingMessage) return;
112
-
113
- // Create the error message element
114
- const messageElement = document.createElement('div');
115
- messageElement.setAttribute('data-login-error-message', '');
116
- messageElement.className = 'gw-error-message';
117
- messageElement.textContent = 'The email or password you entered is incorrect. Please try again.';
118
-
119
- // Insert the message at the beginning of the form
120
- const formSubmitButton = form.querySelector('button[type="submit"]');
121
- form.insertBefore(messageElement, formSubmitButton);
93
+ const errorMessageContainer = form.querySelector('[data-login-error-message]');
94
+ if (errorMessageContainer) {
95
+ errorMessageContainer.textContent = 'The email or password you entered is incorrect. Please try again.';
96
+ };
122
97
  }
123
98
 
124
99
  _setupFormNavigation(links, forms) {
@@ -130,17 +105,32 @@ customElements.define(
130
105
  formToShow.removeAttribute('hidden');
131
106
  };
132
107
 
108
+ const trimParams = () => {
109
+ const url = new URL(window.location.href);
110
+ url.searchParams.delete('form_id');
111
+ window.history.replaceState({}, '', url.toString());
112
+ }
113
+
133
114
  // Add event listeners to navigation links
134
115
  links.createAccount.forEach((link) => {
135
- link.addEventListener('click', () => showForm(forms.registration));
116
+ link.addEventListener('click', () => {
117
+ showForm(forms.registration);
118
+ trimParams();
119
+ });
136
120
  });
137
121
 
138
122
  links.forgotPassword.forEach((link) => {
139
- link.addEventListener('click', () => showForm(forms.forgotPassword));
123
+ link.addEventListener('click', () => {
124
+ showForm(forms.forgotPassword);
125
+ trimParams();
126
+ });
140
127
  });
141
128
 
142
129
  links.signIn.forEach((link) => {
143
- link.addEventListener('click', () => showForm(forms.signIn));
130
+ link.addEventListener('click', () => {
131
+ showForm(forms.signIn);
132
+ trimParams();
133
+ });
144
134
  });
145
135
  }
146
136
 
@@ -124,7 +124,7 @@ customElements.define(
124
124
  if (
125
125
  !GWUtils.hasCustomer &&
126
126
  sessionStorage.getItem(GWCA.STORAGE_KEYS.TEMP_PRODUCTS_UPDATED) !==
127
- 'true'
127
+ 'true'
128
128
  ) {
129
129
  this._syncTempProducts();
130
130
  }
@@ -353,7 +353,7 @@ customElements.define(
353
353
  } else {
354
354
  const currentRecentlyViewed = JSON.parse(
355
355
  localStorage.getItem(GWCA.STORAGE_KEYS.TEMP_RECENTLY_VIEWED_ITEMS) ||
356
- '[]'
356
+ '[]'
357
357
  );
358
358
  if (currentRecentlyViewed.some((p) => p.id === prodID)) return;
359
359
 
@@ -406,8 +406,8 @@ customElements.define(
406
406
  _renderTab(tabId) {
407
407
  Promise.all([
408
408
  (customElements.whenDefined('sl-tab-group'),
409
- customElements.whenDefined('sl-tab-panel'),
410
- customElements.whenDefined('sl-tab')),
409
+ customElements.whenDefined('sl-tab-panel'),
410
+ customElements.whenDefined('sl-tab')),
411
411
  ]).then(() => {
412
412
  this.tabGroup.show(tabId || GWUtils.tabId);
413
413
  });