@wral/studio.mods.auth 2.0.2 → 2.0.4

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/auth.cjs.js CHANGED
@@ -186,7 +186,7 @@
186
186
  font-weight: bold;
187
187
  font-style: normal;
188
188
  }
189
- `}static get states(){return{DEFAULT:"DEFAULT",FORGOT:"FORGOT",CHALLENGE_CHANGE_PW:"CHALLENGE_CHANGE_PW"}}constructor(){super(),this.formState=this.constructor.states.DEFAULT,this.errors=[],this.isBusy=!1}login({username:e,password:t}){return N({baseUrl:this.api}).mintToken({username:e,password:t})}emitLoginSuccess(e){this.dispatchEvent(new CustomEvent("login-success",{detail:{token:e},bubbles:!0,composed:!0,cancelable:!0}))}handleSubmitLogin(e){e.preventDefault();const t=e.currentTarget,s=new FormData(t).entries().reduce((o,[i,n])=>(o[i]=n,o),{});this.isBusy=!0,this.errors=[],this.requestUpdate(),this.login(s).then(({token:o,challenge:i})=>{if((i==null?void 0:i.name)==="NEW_PASSWORD_REQUIRED"){this.token=o,this.username=s.username,this.password=s.password,this.setState(this.constructor.states.CHALLENGE_CHANGE_PW);return}this.emitLoginSuccess(o)}).catch(o=>{console.error(o),this.errors=[o.message],this.requestUpdate()})}onSubmitChangePasswordChallenge(e){e.preventDefault();const t=e.currentTarget,s=new FormData(t).entries().reduce((l,[a,d])=>(l[a]=d,l),{}),o=s.password,i=s["confirm-password"];if(o!==i){this.errors=["Passwords do not match"],this.requestUpdate();return}N({baseUrl:this.api}).updatePassword({token:this.token,currentPassword:this.password,newPassword:o}).then(()=>{this.emitLoginSuccess(this.token)}).catch(l=>{console.error(l),this.errors=[l.message],this.requestUpdate()})}setState(e){this.formState=e,this.isBusy=!1,this.errors=[],this.requestUpdate()}render(){return p`<div class="login-form">
189
+ `}static get states(){return{DEFAULT:"DEFAULT",FORGOT:"FORGOT",CHALLENGE_CHANGE_PW:"CHALLENGE_CHANGE_PW"}}constructor(){super(),this.formState=this.constructor.states.DEFAULT,this.errors=[],this.isBusy=!1}login({username:e,password:t}){return N({baseUrl:this.api}).mintToken({username:e,password:t})}emitLoginSuccess(e){this.dispatchEvent(new CustomEvent("login-success",{detail:{token:e},bubbles:!0,composed:!0,cancelable:!0}))}handleSubmitLogin(e){e.preventDefault();const t=e.currentTarget,s=Array.from(new FormData(t).entries()).reduce((o,[i,n])=>(o[i]=n,o),{});this.isBusy=!0,this.errors=[],this.requestUpdate(),this.login(s).then(({token:o,challenge:i})=>{if((i==null?void 0:i.name)==="NEW_PASSWORD_REQUIRED"){this.token=o,this.username=s.username,this.password=s.password,this.setState(this.constructor.states.CHALLENGE_CHANGE_PW);return}this.emitLoginSuccess(o)}).catch(o=>{console.error(o),this.errors=[o.message],this.requestUpdate()})}onSubmitChangePasswordChallenge(e){e.preventDefault();const t=e.currentTarget,s=Array.from(new FormData(t).entries()).reduce((l,[a,d])=>(l[a]=d,l),{}),o=s.password,i=s["confirm-password"];if(o!==i){this.errors=["Passwords do not match"],this.requestUpdate();return}N({baseUrl:this.api}).updatePassword({token:this.token,currentPassword:this.password,newPassword:o}).then(()=>{this.emitLoginSuccess(this.token)}).catch(l=>{console.error(l),this.errors=[l.message],this.requestUpdate()})}setState(e){this.formState=e,this.isBusy=!1,this.errors=[],this.requestUpdate()}render(){return p`<div class="login-form">
190
190
  ${this.renderHeader()}
191
191
  ${this.formState===this.constructor.states.DEFAULT?this.renderLoginForm():""}
192
192
  ${this.formState===this.constructor.states.CHALLENGE_CHANGE_PW?this.renderChangePasswordForm():""}
@@ -249,4 +249,4 @@
249
249
  `}}customElements.get("auth-login-form")||customElements.define("auth-login-form",Be);function We(r,e){return p`<auth-login-form slot="main"
250
250
  api=${r==null?void 0:r.apiBaseUrl}
251
251
  @login-success=${e}>
252
- </auth-login-form>`}function Ge({tokenKey:r}){return window.localStorage.getItem(r)}function Ve({tokenKey:r},e){window.localStorage.setItem(r,e)}function Je({tokenKey:r}){window.localStorage.removeItem(r)}async function Qe(r){const e=Ge(r);if(!e||e.length<1)throw new Error("No token found");return Ke(e)?await N({baseUrl:r.apiBaseUrl}).refreshToken({token:e}).then(({token:s})=>s):e}function Ke(r,e=300,t=Date.now()){return Ye(r).exp*1e3-e<t}function Ye(r){return JSON.parse(window.atob(r.split(".")[1].replace(/-/g,"+").replace(/_/g,"/")))}function Ze(r,e){var n,l,a,d,h,c,m,f,y;const t="mod-auth",s=((a=(l=(n=e==null?void 0:e.element)==null?void 0:n.attributes)==null?void 0:l.debug)==null?void 0:a.value)||!1,o=((c=(h=(d=e==null?void 0:e.element)==null?void 0:d.attributes)==null?void 0:h["force-login"])==null?void 0:c.value)||!1,i={modName:t,toolkit:r,apiBaseUrl:((y=(f=(m=e==null?void 0:e.element)==null?void 0:m.attributes)==null?void 0:f.api)==null?void 0:y.value)||"https://api.wral.com/auth",debug:s?(...L)=>console.log("[auth]",...L):()=>{},tokenKey:`${t}::token`,callbacks:[],isLoginPresent:!1};Object.entries({"auth:requestToken":Xe,"auth:destroy":et}).forEach(([L,pe])=>{const me=(...F)=>i.debug("[action]",L,...F);r.dispatchAction({type:"action:register",detail:{actionType:L,modName:t,handler:(...F)=>{me("handler",...F),pe(i,...F)}}})}),r.dispatchAction({type:"layout:register",detail:{name:`${t}::auth-layout`,templateFn:Le}}),o&&fe.getToken(e.element)}async function Xe(r,e){const{callback:t}=e;let s;try{s=await Qe(r),t(s)}catch(o){console.log("Presenting login form to get a fresh token",o),r.callbacks.push(t),r.isLoginPresent||tt(r)}}function et(r){Je(r)}function tt(r){r.isLoginPresent=!0,r.toolkit.dispatchAction({type:"layout:push",detail:{name:`${r.modName}::auth-layout`}}),r.toolkit.dispatchAction({type:"layout:content:append",detail:{content:We(r,st(r))}})}function st(r){return e=>{const{token:t}=e.detail;Ve(r,t),r.callbacks.forEach(s=>s(t)),r.callbacks=[],r.isLoginPresent=!1,r.toolkit.dispatchAction({type:"layout:pop"})}}exports.init=Ze;
252
+ </auth-login-form>`}function Ge({tokenKey:r}){return window.localStorage.getItem(r)}function Ve({tokenKey:r},e){window.localStorage.setItem(r,e)}function Je({tokenKey:r}){window.localStorage.removeItem(r)}async function Qe(r){const e=Ge(r);if(!e||e.length<1)throw new Error("No token found");return Ke(e)?await N({baseUrl:r.apiBaseUrl}).refreshToken({token:e}).then(({token:s})=>s):e}function Ke(r,e=300,t=Date.now()){return Ye(r).exp*1e3-e<t}function Ye(r){return JSON.parse(window.atob(r.split(".")[1].replace(/-/g,"+").replace(/_/g,"/")))}function Ze(r,e){var n,l,a,d,h,c,m,f,y;const t="mod-auth",s=((a=(l=(n=e==null?void 0:e.element)==null?void 0:n.attributes)==null?void 0:l.debug)==null?void 0:a.value)||!1,o=((c=(h=(d=e==null?void 0:e.element)==null?void 0:d.attributes)==null?void 0:h["force-login"])==null?void 0:c.value)||!1,i={modName:t,toolkit:r,apiBaseUrl:((y=(f=(m=e==null?void 0:e.element)==null?void 0:m.attributes)==null?void 0:f.api)==null?void 0:y.value)||"https://api.wral.com/auth",debug:s?(...L)=>console.log("[auth]",...L):()=>{},tokenKey:`${t}::token`,callbacks:[],isLoginPresent:!1};Object.entries({"auth:requestToken":Xe,"auth:destroy":et}).forEach(([L,pe])=>{const me=(...F)=>i.debug("[action]",L,...F);r.dispatchAction({type:"action:register",detail:{actionType:L,modName:t,handler:(...F)=>{me("handler",...F),pe(i,...F)}}})}),r.dispatchAction({type:"layout:register",detail:{name:`${t}::auth-layout`,templateFn:Le}}),o&&fe.getToken(e.element)}async function Xe(r,e){const{callback:t}=e;let s;try{s=await Qe(r)}catch(o){console.log("Presenting login form to get a fresh token",o),r.callbacks.push(t),r.isLoginPresent||tt(r);return}try{t(s)}catch(o){console.log("Callback failed",o)}}function et(r){Je(r)}function tt(r){r.isLoginPresent=!0,r.toolkit.dispatchAction({type:"layout:push",detail:{name:`${r.modName}::auth-layout`}}),r.toolkit.dispatchAction({type:"layout:content:append",detail:{content:We(r,st(r))}})}function st(r){return e=>{const{token:t}=e.detail;Ve(r,t),r.callbacks.forEach(s=>s(t)),r.callbacks=[],r.isLoginPresent=!1,r.toolkit.dispatchAction({type:"layout:pop"})}}exports.init=Ze;
package/dist/auth.es.js CHANGED
@@ -934,7 +934,7 @@ class Be extends v {
934
934
  }
935
935
  handleSubmitLogin(e) {
936
936
  e.preventDefault();
937
- const t = e.currentTarget, s = new FormData(t).entries().reduce((o, [i, n]) => (o[i] = n, o), {});
937
+ const t = e.currentTarget, s = Array.from(new FormData(t).entries()).reduce((o, [i, n]) => (o[i] = n, o), {});
938
938
  this.isBusy = !0, this.errors = [], this.requestUpdate(), this.login(s).then(({ token: o, challenge: i }) => {
939
939
  if ((i == null ? void 0 : i.name) === "NEW_PASSWORD_REQUIRED") {
940
940
  this.token = o, this.username = s.username, this.password = s.password, this.setState(this.constructor.states.CHALLENGE_CHANGE_PW);
@@ -947,7 +947,7 @@ class Be extends v {
947
947
  }
948
948
  onSubmitChangePasswordChallenge(e) {
949
949
  e.preventDefault();
950
- const t = e.currentTarget, s = new FormData(t).entries().reduce((l, [a, d]) => (l[a] = d, l), {}), o = s.password, i = s["confirm-password"];
950
+ const t = e.currentTarget, s = Array.from(new FormData(t).entries()).reduce((l, [a, d]) => (l[a] = d, l), {}), o = s.password, i = s["confirm-password"];
951
951
  if (o !== i) {
952
952
  this.errors = ["Passwords do not match"], this.requestUpdate();
953
953
  return;
@@ -1114,9 +1114,15 @@ async function Ze(r, e) {
1114
1114
  const { callback: t } = e;
1115
1115
  let s;
1116
1116
  try {
1117
- s = await Qe(r), t(s);
1117
+ s = await Qe(r);
1118
1118
  } catch (o) {
1119
1119
  console.log("Presenting login form to get a fresh token", o), r.callbacks.push(t), r.isLoginPresent || et(r);
1120
+ return;
1121
+ }
1122
+ try {
1123
+ t(s);
1124
+ } catch (o) {
1125
+ console.log("Callback failed", o);
1120
1126
  }
1121
1127
  }
1122
1128
  function Xe(r) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@wral/studio.mods.auth",
3
- "version": "2.0.2",
3
+ "version": "2.0.4",
4
4
  "description": "Auth mod for Studio/Vellum",
5
5
  "main": "dist/auth.cjs.js",
6
6
  "module": "dist/auth.es.js",
package/src/index.mjs CHANGED
@@ -65,13 +65,19 @@ async function requestToken(state, eventDetail) {
65
65
  let token;
66
66
  try {
67
67
  token = await getFreshToken(state);
68
- callback(token);
69
68
  } catch (err) {
70
69
  console.log('Presenting login form to get a fresh token', err);
71
70
  state.callbacks.push(callback);
72
71
  if (!state.isLoginPresent) {
73
72
  presentLoginForm(state);
74
73
  }
74
+ return;
75
+ }
76
+
77
+ try {
78
+ callback(token);
79
+ } catch (err) {
80
+ console.log('Callback failed', err);
75
81
  }
76
82
 
77
83
  };
@@ -118,10 +118,8 @@ class AuthLoginForm extends LitElement {
118
118
  handleSubmitLogin(event) {
119
119
  event.preventDefault();
120
120
  const formElem = event.currentTarget;
121
- const formData = new FormData(formElem).entries().reduce((acc, [key, value]) => {
122
- acc[key] = value;
123
- return acc;
124
- }, {});
121
+ const formData = Array.from(new FormData(formElem).entries())
122
+ .reduce((acc, [k, v]) => (acc[k] = v, acc), {});
125
123
 
126
124
  this.isBusy = true;
127
125
  this.errors = [];
@@ -151,10 +149,8 @@ class AuthLoginForm extends LitElement {
151
149
  onSubmitChangePasswordChallenge(e) {
152
150
  e.preventDefault();
153
151
  const formElem = e.currentTarget;
154
- const formData = new FormData(formElem).entries().reduce((acc, [key, value]) => {
155
- acc[key] = value;
156
- return acc;
157
- }, {});
152
+ const formData = Array.from(new FormData(formElem).entries())
153
+ .reduce((acc, [k, v]) => (acc[k] = v, acc), {});
158
154
 
159
155
  // validate password, client-side
160
156
  const password = formData['password'];