@wral/studio.mods.auth 2.0.3 → 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
@@ -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
@@ -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.3",
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
  };