gw-customer-accounts 1.3.6 → 1.3.7

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 t={signIn:this.querySelector("[data-sign-in-form]"),registration:this.querySelector("[data-registeration-form]"),forgotPassword:this.querySelector("[data-forgot-password-form]")},r={signIn:this.querySelectorAll("[data-sign-in-link]"),createAccount:this.querySelectorAll("[data-create-account-link]"),forgotPassword:this.querySelectorAll("[data-forgot-password-link]")};this._setupFormNavigation(r,t),this._setupFormSubmissions(Object.values(t)),this._handleFormVisibility(t)}_handleFormVisibility(t){const i=new URLSearchParams(window.location.search).get("form_id");i&&Object.values(t).forEach(e=>{const o=e.querySelector(".gw-error-message");i===e.getAttribute("id")?(e&&e.removeAttribute("hidden"),o&&o.removeAttribute("hidden")):(e&&e.setAttribute("hidden",""),o&&o.setAttribute("hidden",""))})}_setupFormNavigation(t,r){const i=e=>{Object.values(r).forEach(o=>{o.setAttribute("hidden","")}),e.removeAttribute("hidden")};t.createAccount.forEach(e=>{e.addEventListener("click",()=>i(r.registration))}),t.forgotPassword.forEach(e=>{e.addEventListener("click",()=>i(r.forgotPassword))}),t.signIn.forEach(e=>{e.addEventListener("click",()=>i(r.signIn))})}_setupFormSubmissions(t){t.forEach(r=>{const i=new URL(window.location.href);i.searchParams.append("my-account-open",!0),i.searchParams.append("form_id",r.getAttribute("id"));const e=r.querySelector('input[name="return_to"]'),o=r.querySelector('input[name="return_url"]');e&&(e.value=i.pathname+i.search),o&&(o.value=i.pathname+i.search),r.addEventListener("submit",s=>{s.preventDefault(),r.querySelectorAll('button[type="submit"]').forEach(a=>{a.disabled=!0;const n=a.querySelector("[data-loader]");n&&(n.hidden=!1)}),Shopify.captcha.protect(r,()=>{r.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=""}_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]"),r=this.querySelector("[data-email-error]");t.addEventListener("sl-invalid",i=>{i.preventDefault(),r.textContent=i.target.validationMessage,r.hidden=!1,i.target.focus()},{capture:!0}),t.addEventListener("submit",i=>{i.preventDefault(),r.hidden=!0,r.textContent="",this._submitEmailForm(t).then(()=>{const e=this.verifyCodeBlock.querySelector(".code-input");e&&e.focus()})})})}async _submitEmailForm(t){const r=t.querySelector('button[type="submit"]');r.disabled=!0,r.querySelector("[data-loader]").hidden=!1;const i=new FormData(t);this.inputtedEmail=i.get("email");let e="";const o=await this._fetchCaptchaToken();o&&(e=o);try{const s=await this._request({customerEmail:this.inputtedEmail,token:e},c.ENDPOINTS.AUTH.SIGN_IN);if(s.error){console.error(s.error);return}const a=this.querySelector("[data-code-sent-to]");return a&&(a.textContent=this.inputtedEmail),this.signInBlock.setAttribute("hidden",""),this.verifyCodeBlock.removeAttribute("hidden"),s}catch(s){console.error(s),alert("An error occurred. Please try again later.")}finally{r.disabled=!1,r.querySelector("[data-loader]").hidden=!0}}async _submitVerificationCode(){var s,a;const t=this.querySelector(".gw-verify-code__actions"),r=t.querySelectorAll("button, a"),i=t.querySelector("[data-verify-error]"),e=t.querySelector("[data-loader]"),o=this.querySelectorAll("input.code-input");try{o.forEach(u=>{u.disabled=!0}),r.forEach(u=>{u.disabled=!0,u.hidden=!0}),i.hidden=!0,e.hidden=!1;const n=await this._request({customerEmail:this.inputtedEmail,code:this.inputtedCode},c.ENDPOINTS.AUTH.VERIFY_CODE);if(!n.url)throw new Error("Verification failed");localStorage.setItem(c.STORAGE_KEYS.TOKEN,n.token),localStorage.setItem(c.STORAGE_KEYS.EMAIL,this.inputtedEmail);const d=await fetch(n.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=(s=h.querySelector("#gw-customer-accounts-template").content)==null?void 0:s.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(n){console.error(n),setTimeout(()=>{o[o.length-1].focus()})}finally{o.forEach(n=>{n.disabled=!1}),r.forEach(n=>{n.disabled=!1,n.hidden=!1}),i.hidden=!1,e.hidden=!0}}async _request(t,r,i="POST"){return await(await fetch(l.baseURL+r,{method:i,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((r,i)=>{r.addEventListener("keydown",e=>{(e.ctrlKey||e.metaKey)&&e.keyCode===86||(e.keyCode!==8&&(e.keyCode<48||e.keyCode>57)&&e.preventDefault(),e.keyCode===8&&e.target.value===""&&t[Math.max(0,i-1)].focus())}),r.addEventListener("input",e=>{const[o,...s]=e.target.value;e.target.value=o??"";const a=i===t.length-1,n=o!==void 0;n&&!a&&(t[i+1].focus(),t[i+1].value=s.join(""),t[i+1].dispatchEvent(new Event("input"))),n&&a&&t.every(d=>d.value)&&(this.inputtedCode=t.map(d=>d.value).join(""),this._submitVerificationCode())})})}async _fetchCaptchaToken(){const r=await(await fetch(l.baseURL+c.ENDPOINTS.AUTH.GET_CAPTCHA_TOKEN)).json();return r==null?void 0:r.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]")},r={signIn:this.querySelectorAll("[data-sign-in-link]"),createAccount:this.querySelectorAll("[data-create-account-link]"),forgotPassword:this.querySelectorAll("[data-forgot-password-link]")};this._setupFormNavigation(r,t),this._setupFormSubmissions(Object.values(t)),this._handleFormVisibility(t)}_handleFormVisibility(t){const i=new URLSearchParams(window.location.search).get("form_id");i&&Object.values(t).forEach(e=>{const o=e.querySelector(".gw-error-message");i===e.getAttribute("id")?(e&&e.removeAttribute("hidden"),o&&o.removeAttribute("hidden")):(e&&e.setAttribute("hidden",""),o&&o.setAttribute("hidden",""))})}_setupFormNavigation(t,r){const i=e=>{Object.values(r).forEach(o=>{o.setAttribute("hidden","")}),e.removeAttribute("hidden")};t.createAccount.forEach(e=>{e.addEventListener("click",()=>i(r.registration))}),t.forgotPassword.forEach(e=>{e.addEventListener("click",()=>i(r.forgotPassword))}),t.signIn.forEach(e=>{e.addEventListener("click",()=>i(r.signIn))})}_setupFormSubmissions(t){t.forEach(r=>{const i=new URL(window.location.href);i.searchParams.append("my-account-open",!0),i.searchParams.append("form_id",r.getAttribute("id"));const e=r.querySelector('input[name="return_to"]'),o=r.querySelector('input[name="return_url"]');e&&(e.value=i.pathname+i.search),o&&(o.value=i.pathname+i.search),r.addEventListener("submit",s=>{s.preventDefault(),r.querySelectorAll('button[type="submit"]').forEach(a=>{a.disabled=!0;const n=a.querySelector("[data-loader]");n&&(n.hidden=!1)}),Shopify.captcha.protect(r,()=>{r.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]"),r=this.querySelector("[data-email-error]");t.addEventListener("sl-invalid",i=>{i.preventDefault(),r.textContent=i.target.validationMessage,r.hidden=!1,i.target.focus()},{capture:!0}),t.addEventListener("submit",i=>{i.preventDefault(),r.hidden=!0,r.textContent="",this._submitEmailForm(t).then(()=>{const e=this.verifyCodeBlock.querySelector(".code-input");e&&e.focus()})})})}async _submitEmailForm(t){const r=t.querySelector('button[type="submit"]');r.disabled=!0,r.querySelector("[data-loader]").hidden=!1;const i=new FormData(t);this.inputtedEmail=i.get("email"),i.has("accepts_marketing")&&(this.acceptsMarketing=i.get("accepts_marketing"));let e="";const o=await this._fetchCaptchaToken();o&&(e=o);try{const s=await this._request({customerEmail:this.inputtedEmail,token:e},c.ENDPOINTS.AUTH.SIGN_IN);if(s.error){console.error(s.error);return}const a=this.querySelector("[data-code-sent-to]");return a&&(a.textContent=this.inputtedEmail),this.signInBlock.setAttribute("hidden",""),this.verifyCodeBlock.removeAttribute("hidden"),s}catch(s){console.error(s),alert("An error occurred. Please try again later.")}finally{r.disabled=!1,r.querySelector("[data-loader]").hidden=!0}}async _submitVerificationCode(){var s,a;const t=this.querySelector(".gw-verify-code__actions"),r=t.querySelectorAll("button, a"),i=t.querySelector("[data-verify-error]"),e=t.querySelector("[data-loader]"),o=this.querySelectorAll("input.code-input");try{o.forEach(u=>{u.disabled=!0}),r.forEach(u=>{u.disabled=!0,u.hidden=!0}),i.hidden=!0,e.hidden=!1;const n=await this._request({customerEmail:this.inputtedEmail,code:this.inputtedCode,is_marketing:this.acceptsMarketing},c.ENDPOINTS.AUTH.VERIFY_CODE);if(!n.url)throw new Error("Verification failed");localStorage.setItem(c.STORAGE_KEYS.TOKEN,n.token),localStorage.setItem(c.STORAGE_KEYS.EMAIL,this.inputtedEmail);const d=await fetch(n.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=(s=h.querySelector("#gw-customer-accounts-template").content)==null?void 0:s.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(n){console.error(n),setTimeout(()=>{o[o.length-1].focus()})}finally{o.forEach(n=>{n.disabled=!1}),r.forEach(n=>{n.disabled=!1,n.hidden=!1}),i.hidden=!1,e.hidden=!0}}async _request(t,r,i="POST"){return await(await fetch(l.baseURL+r,{method:i,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((r,i)=>{r.addEventListener("keydown",e=>{(e.ctrlKey||e.metaKey)&&e.keyCode===86||(e.keyCode!==8&&(e.keyCode<48||e.keyCode>57)&&e.preventDefault(),e.keyCode===8&&e.target.value===""&&t[Math.max(0,i-1)].focus())}),r.addEventListener("input",e=>{const[o,...s]=e.target.value;e.target.value=o??"";const a=i===t.length-1,n=o!==void 0;n&&!a&&(t[i+1].focus(),t[i+1].value=s.join(""),t[i+1].dispatchEvent(new Event("input"))),n&&a&&t.every(d=>d.value)&&(this.inputtedCode=t.map(d=>d.value).join(""),this._submitVerificationCode())})})}async _fetchCaptchaToken(){const r=await(await fetch(l.baseURL+c.ENDPOINTS.AUTH.GET_CAPTCHA_TOKEN)).json();return r==null?void 0:r.token}});
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "gw-customer-accounts",
3
- "version": "1.3.6",
3
+ "version": "1.3.7",
4
4
  "description": "",
5
5
  "main": "gw-customer-accounts.js",
6
6
  "type": "module",
@@ -160,6 +160,7 @@ customElements.define(
160
160
 
161
161
  this.inputtedEmail = '';
162
162
  this.inputtedCode = '';
163
+ this.acceptsMarketing = false;
163
164
  }
164
165
 
165
166
  _resendVerificationCode() {
@@ -217,6 +218,9 @@ customElements.define(
217
218
 
218
219
  const formData = new FormData(form);
219
220
  this.inputtedEmail = formData.get('email');
221
+ if (formData.has('accepts_marketing')) {
222
+ this.acceptsMarketing = formData.get('accepts_marketing');
223
+ }
220
224
 
221
225
  // Fetch hidden captcha token
222
226
  let token = '';
@@ -284,7 +288,11 @@ customElements.define(
284
288
  verifyLoader.hidden = false;
285
289
 
286
290
  const verificationResponse = await this._request(
287
- { customerEmail: this.inputtedEmail, code: this.inputtedCode },
291
+ {
292
+ customerEmail: this.inputtedEmail,
293
+ code: this.inputtedCode,
294
+ is_marketing: this.acceptsMarketing,
295
+ },
288
296
  GWCA.ENDPOINTS.AUTH.VERIFY_CODE
289
297
  );
290
298