@faable/auth-js 1.9.2 → 1.10.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/README.md +40 -0
- package/dist/dts/FaableAuthClient.d.ts +6 -21
- package/dist/dts/entry.d.ts +4 -3
- package/dist/dts/lib/types.d.ts +31 -0
- package/dist/dts/lib/unwrap.d.ts +28 -0
- package/dist/faableauth.js +2 -2
- package/dist/faableauth.js.map +1 -1
- package/dist/main.js +2 -2
- package/dist/main.js.map +1 -1
- package/package.json +1 -1
package/dist/main.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
/*!
|
|
2
|
-
* @faable/auth-js v1.
|
|
2
|
+
* @faable/auth-js v1.10.0
|
|
3
3
|
* (c) 2026
|
|
4
4
|
* @license SEE LICENSE.md
|
|
5
5
|
*/
|
|
6
|
-
var e=function(t,r){return e=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(e,t){e.__proto__=t}||function(e,t){for(var r in t)Object.prototype.hasOwnProperty.call(t,r)&&(e[r]=t[r])},e(t,r)};function t(t,r){if("function"!=typeof r&&null!==r)throw new TypeError("Class extends value "+String(r)+" is not a constructor or null");function n(){this.constructor=t}e(t,r),t.prototype=null===r?Object.create(r):(n.prototype=r.prototype,new n)}var r=function(){return r=Object.assign||function(e){for(var t,r=1,n=arguments.length;r<n;r++)for(var i in t=arguments[r])Object.prototype.hasOwnProperty.call(t,i)&&(e[i]=t[i]);return e},r.apply(this,arguments)};function n(e,t,r,n){return new(r||(r=Promise))(function(i,s){function o(e){try{u(n.next(e))}catch(e){s(e)}}function a(e){try{u(n.throw(e))}catch(e){s(e)}}function u(e){var t;e.done?i(e.value):(t=e.value,t instanceof r?t:new r(function(e){e(t)})).then(o,a)}u((n=n.apply(e,t||[])).next())})}function i(e,t){var r,n,i,s={label:0,sent:function(){if(1&i[0])throw i[1];return i[1]},trys:[],ops:[]},o=Object.create(("function"==typeof Iterator?Iterator:Object).prototype);return o.next=a(0),o.throw=a(1),o.return=a(2),"function"==typeof Symbol&&(o[Symbol.iterator]=function(){return this}),o;function a(a){return function(u){return function(a){if(r)throw new TypeError("Generator is already executing.");for(;o&&(o=0,a[0]&&(s=0)),s;)try{if(r=1,n&&(i=2&a[0]?n.return:a[0]?n.throw||((i=n.return)&&i.call(n),0):n.next)&&!(i=i.call(n,a[1])).done)return i;switch(n=0,i&&(a=[2&a[0],i.value]),a[0]){case 0:case 1:i=a;break;case 4:return s.label++,{value:a[1],done:!1};case 5:s.label++,n=a[1],a=[0];continue;case 7:a=s.ops.pop(),s.trys.pop();continue;default:if(!(i=s.trys,(i=i.length>0&&i[i.length-1])||6!==a[0]&&2!==a[0])){s=0;continue}if(3===a[0]&&(!i||a[1]>i[0]&&a[1]<i[3])){s.label=a[1];break}if(6===a[0]&&s.label<i[1]){s.label=i[1],i=a;break}if(i&&s.label<i[2]){s.label=i[2],s.ops.push(a);break}i[2]&&s.ops.pop(),s.trys.pop();continue}a=t.call(e,s)}catch(e){a=[6,e],n=0}finally{r=i=0}if(5&a[0])throw a[1];return{value:a[0]?a[1]:void 0,done:!0}}([a,u])}}}function s(e,t,r){if(r||2===arguments.length)for(var n,i=0,s=t.length;i<s;i++)!n&&i in t||(n||(n=Array.prototype.slice.call(t,0,i)),n[i]=t[i]);return e.concat(n||Array.prototype.slice.call(t))}"function"==typeof SuppressedError&&SuppressedError;var o="0.0.0",a=function(){function e(e){void 0===e&&(e={}),this.logger=console.log,this.logDebugMessages=!!e.debug,"function"==typeof e.debug&&(this.logger=e.debug)}return e.prototype._debug=function(){for(var e=[],t=0;t<arguments.length;t++)e[t]=arguments[t];if(this.logDebugMessages){var r=this.extraPrint?this.extraPrint():"";this.logger.apply(this,s(["FaableAuth@".concat(r," (").concat(o,") ").concat((new Date).toISOString())],e,!1))}return this},e}(),u=function(e){function r(t){void 0===t&&(t={});var n=e.call(this,t)||this;return n.instanceID=r.nextInstanceID,r.nextInstanceID+=1,n}return t(r,e),r.prototype.extraPrint=function(){return this.instanceID.toString()},r.nextInstanceID=0,r}(a),c="undefined"!=typeof window?window:void 0,l="undefined"!=typeof globalThis?globalThis:c,h=null==l?void 0:l.document,d=l.fetch,f=function(e){void 0===e&&(e={});var t={"x-faable-client":"auth-js/".concat(o)};return(null==e?void 0:e.token)&&(t=r(r({},t),{Authorization:"Bearer ".concat(null==e?void 0:e.token)})),r(r({},null==e?void 0:e.headers),t)},v=function(e){for(var t=[],r=1;r<arguments.length;r++)t[r-1]=arguments[r];return n(void 0,s([e],t,!0),void 0,function(e,t){var r,n,s;return void 0===t&&(t={}),i(this,function(i){switch(i.label){case 0:return t.raw?[4,e.text()]:[3,2];case 1:return n=i.sent(),[3,4];case 2:return[4,e.json()];case 3:n=i.sent(),i.label=4;case 4:return r=n,e.status>=300?[2,{data:r,error:t.raw?null===(s=JSON.parse(r))||void 0===s?void 0:s.message:null==r?void 0:r.message}]:[2,{data:r,error:null}]}})})},p=function(e,t){for(var o=[],a=2;a<arguments.length;a++)o[a-2]=arguments[a];return n(void 0,s([e,t],o,!0),void 0,function(e,t,n){var s;return void 0===n&&(n={}),i(this,function(i){switch(i.label){case 0:return i.trys.push([0,3,,4]),[4,d(e,{method:"POST",body:JSON.stringify(t),headers:r(r({},f(n)),{"Content-Type":"application/json"})})];case 1:return s=i.sent(),[4,v(s,n)];case 2:return[2,i.sent()];case 3:return[2,{data:null,error:i.sent()}];case 4:return[2]}})})},b=function(e){for(var t=[],o=1;o<arguments.length;o++)t[o-1]=arguments[o];return n(void 0,s([e],t,!0),void 0,function(e,t){var n;return void 0===t&&(t={}),i(this,function(i){switch(i.label){case 0:return i.trys.push([0,3,,4]),[4,d(e,r(r({},t),{method:"GET",headers:f(t)}))];case 1:return n=i.sent(),[4,v(n,t)];case 2:return[2,i.sent()];case 3:return[2,{data:null,error:i.sent()}];case 4:return[2]}})})},_=function(e){function r(t,r){var n=e.call(this,r)||this;return n.base_url=t,n}return t(r,e),r.prototype.extraPrint=function(){return"api"},r.prototype.signOut=function(e){return n(this,void 0,void 0,function(){var t,r;return i(this,function(n){switch(n.label){case 0:return t="".concat(this.base_url,"/logout?").concat(new URLSearchParams(e)),this._debug("requesting ".concat(t)),[4,b(t)];case 1:return r=n.sent(),this._debug(r),r.error?[2,{error:r.error,data:null}]:[2,{error:null,data:null}]}})})},r}(a),g=function(e,t){return e.response_type||(t?"code":"token")},y=function(e,t,r){return n(void 0,void 0,void 0,function(){return i(this,function(n){switch(n.label){case 0:return[4,e.setItem(t,JSON.stringify(r))];case 1:return n.sent(),[2]}})})},w=function(e,t){return n(void 0,void 0,void 0,function(){var r;return i(this,function(n){switch(n.label){case 0:return[4,e.getItem(t)];case 1:if(!(r=n.sent()))return[2,null];try{return[2,JSON.parse(r)]}catch(e){return[2,r]}return[2]}})})},m=function(e,t,r){return n(void 0,[e,t,r],void 0,function(e,t,r){var n,s=r.verifier,o=r.redirectType,a=r.returnTo,u=r.now,c=void 0===u?Date.now():u;return i(this,function(r){switch(r.label){case 0:return n={verifier:s,createdAt:c},o&&(n.redirectType=o),a&&(n.returnTo=a),[4,y(e,t,n)];case 1:return r.sent(),[2]}})})},k=function(e,t){for(var r=[],o=2;o<arguments.length;o++)r[o-2]=arguments[o];return n(void 0,s([e,t],r,!0),void 0,function(e,t,r){var n,s,o=(void 0===r?{}:r).now,a=void 0===o?Date.now():o;return i(this,function(r){switch(r.label){case 0:return[4,w(e,t)];case 1:return n=r.sent(),"object"!=typeof(i=n)||null===i||"string"!=typeof i.verifier||"number"!=typeof i.createdAt?[2,null]:a-n.createdAt>6e5?[4,e.removeItem(t)]:[3,3];case 2:return r.sent(),[2,null];case 3:return s={verifier:n.verifier},n.redirectType&&(s.redirectType=n.redirectType),n.returnTo&&(s.returnTo=n.returnTo),[2,s]}var i})})};function S(e){return("0"+e.toString(16)).substr(-2)}function T(e){return n(this,void 0,void 0,function(){var t,r,n,s;return i(this,function(i){switch(i.label){case 0:return t=new TextEncoder,r=t.encode(e),[4,crypto.subtle.digest("SHA-256",r)];case 1:return n=i.sent(),s=new Uint8Array(n),[2,Array.from(s).map(function(e){return String.fromCharCode(e)}).join("")]}})})}function I(e){return n(this,void 0,void 0,function(){var t;return i(this,function(r){switch(r.label){case 0:return"undefined"!=typeof crypto&&void 0!==crypto.subtle&&"undefined"!=typeof TextEncoder?[4,T(e)]:(console.warn("WebCrypto API is not supported. Code challenge method will default to use plain instead of sha256."),[2,e]);case 1:return t=r.sent(),[2,(n=t,btoa(n).replace(/\+/g,"-").replace(/\//g,"_").replace(/=+$/,""))]}var n})})}function x(e,t){return n(this,arguments,void 0,function(e,t,r,n){var s,o;return void 0===r&&(r=!1),i(this,function(i){switch(i.label){case 0:return s=function(){if("undefined"==typeof crypto||"function"!=typeof crypto.getRandomValues)throw new Error("Web Crypto API is required to generate a PKCE code verifier");var e=new Uint32Array(56);return crypto.getRandomValues(e),Array.from(e,S).join("")}(),[4,m(e,"".concat(t,"-code-verifier"),{verifier:s,redirectType:r?"PASSWORD_RECOVERY":void 0,returnTo:n})];case 1:return i.sent(),[4,I(s)];case 2:return[2,[o=i.sent(),s===o?"plain":"S256"]]}})})}var A=function(){return"undefined"!=typeof document},R={tested:!1,writable:!1},E=function(){if(!A())return!1;try{if("object"!=typeof globalThis.localStorage)return!1}catch(e){return!1}if(R.tested)return R.writable;var e="lswt-".concat(Math.random()).concat(Math.random());try{globalThis.localStorage.setItem(e,e),globalThis.localStorage.removeItem(e),R.tested=!0,R.writable=!0}catch(e){R.tested=!0,R.writable=!1}return R.writable};function C(e){var t,n,i=e.data,s=null;if(!i)throw new Error("Bad session response");return function(e){return!!e.access_token&&!!e.refresh_token&&!!e.expires_in}(i)&&(s=r({},i),!i.expires_at&&i.expires_in&&(s.expires_at=(n=i.expires_in,Math.round(Date.now()/1e3)+n))),{data:{session:s,user:null!==(t=i.user)&&void 0!==t?t:i},error:null}}var U=function(){function e(){var t=this;this.promise=new e.promiseConstructor(function(e,r){t.resolve=e,t.reject=r})}return e.promiseConstructor=Promise,e}();function P(e,t){var r=this;return new Promise(function(s,o){n(r,void 0,void 0,function(){var r,n,a;return i(this,function(i){switch(i.label){case 0:r=0,i.label=1;case 1:if(!(r<1/0))return[3,6];i.label=2;case 2:return i.trys.push([2,4,,5]),[4,e(r)];case 3:return n=i.sent(),t(r,null,n)?[3,5]:(s(n),[2]);case 4:return a=i.sent(),t(r,a)?[3,5]:(o(a),[2]);case 5:return r++,[3,1];case 6:return[2]}})})})}function L(e){return n(this,void 0,void 0,function(){return i(this,function(t){return[2,new Promise(function(t){setTimeout(function(){return t(null)},e)})]})})}var O=function(){function e(e,t){var r=this;if(this.debug=t,this.channel=null,this.subscribers=new Map,"undefined"!=typeof globalThis&&"function"==typeof globalThis.BroadcastChannel&&e)try{this.channel=new globalThis.BroadcastChannel(e),this.channel.addEventListener("message",function(e){return n(r,void 0,void 0,function(){return i(this,function(t){switch(t.label){case 0:return this.debug("broadcast_sync: received notification from another tab",e),[4,this.dispatch(e.data.event,e.data.session,!1)];case 1:return t.sent(),[2]}})})})}catch(e){console.error("Failed to create BroadcastChannel; cross-tab sync disabled",e)}}return e.prototype.subscribe=function(e){var t=this,r="xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g,function(e){var t=16*Math.random()|0;return("x"==e?t:3&t|8).toString(16)}),n={id:r,callback:e,unsubscribe:function(){t.subscribers.delete(r)}};return this.subscribers.set(r,n),{subscription:n}},e.prototype.notify=function(e,t){return n(this,arguments,void 0,function(e,t,r){return void 0===r&&(r=!0),i(this,function(n){switch(n.label){case 0:return r&&this.channel&&this.channel.postMessage({event:e,session:t}),[4,this.dispatch(e,t,r)];case 1:return n.sent(),[2]}})})},e.prototype.dispatch=function(e,t,r){return n(this,void 0,void 0,function(){var r,s,o,a=this;return i(this,function(u){switch(u.label){case 0:return r=[],s=Array.from(this.subscribers.values()).map(function(s){return n(a,void 0,void 0,function(){var n;return i(this,function(i){switch(i.label){case 0:return i.trys.push([0,2,,3]),[4,s.callback(e,t)];case 1:return i.sent(),[3,3];case 2:return n=i.sent(),r.push(n),[3,3];case 3:return[2]}})})}),[4,Promise.all(s)];case 1:if(u.sent(),r.length>0){for(o=0;o<r.length;o+=1)console.error(r[o]);throw r[0]}return[2]}})})},e.prototype.close=function(){var e;this.subscribers.clear();try{null===(e=this.channel)||void 0===e||e.close()}catch(e){}this.channel=null},e}(),D="faableauth",z=function(e){function r(t,r,n){var i=e.call(this,t)||this;return i.__isAuthError=!0,i.name="AuthError",i.status=r,i.code=n,i}return t(r,e),r}(Error),N=function(e){function r(t,r,n,i){var s=e.call(this,t,n,i)||this;return s.name=r,s.status=n,s}return t(r,e),r}(z),q=function(e){function r(){return e.call(this,"Auth session missing!","AuthSessionMissingError",400,void 0)||this}return t(r,e),r}(N);function j(e){return"object"==typeof e&&null!==e&&"__isAuthError"in e}var K=function(e){function r(t,r,n){var i=e.call(this,t,r,n)||this;return i.name="AuthApiError",i.status=r,i.code=n,i}return t(r,e),r}(z);var M=function(e){function r(t,r){void 0===r&&(r=null);var n=e.call(this,t,"AuthImplicitGrantRedirectError",500,void 0)||this;return n.details=null,n.details=r,n}return t(r,e),r.prototype.toJSON=function(){return{name:this.name,message:this.message,status:this.status,details:this.details}},r}(N),F=function(e){function r(t,r){void 0===r&&(r=null);var n=e.call(this,t,"AuthPKCEGrantCodeExchangeError",500,void 0)||this;return n.details=null,n.details=r,n}return t(r,e),r.prototype.toJSON=function(){return{name:this.name,message:this.message,status:this.status,details:this.details}},r}(N),V=function(e){function r(t,r){var n=e.call(this,t)||this;return n.name="AuthUnknownError",n.originalError=r,n}return t(r,e),r}(z),W=function(e){function r(){return e.call(this,"Auth session or user missing","AuthInvalidTokenResponseError",500,void 0)||this}return t(r,e),r}(N);function G(e){return j(e)&&"AuthRetryableFetchError"===e.name}function J(){if(!c)throw new Error("No window in environment");return c}!function(e){function r(t,r){return e.call(this,t,"AuthRetryableFetchError",r,void 0)||this}t(r,e)}(N);var B={redirect:function(e){J().location=e},getDocument:function(){return J().document},getWindow:J};function $(e){var t=e.split(".");if(3!==t.length)throw new Error("JWT is not valid: not a JWT structure");if(!/^([a-z0-9_-]{4})*($|[a-z0-9_-]{3}=?$|[a-z0-9_-]{2}(==)?$)$/i.test(t[1]))throw new Error("JWT is not valid: payload is not in base64url format");var r=t[1];return JSON.parse(function(e){var t,r,n,i,s,o,a="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=",u="",c=0;for(e=e.replace("-","+").replace("_","/");c<e.length;)t=a.indexOf(e.charAt(c++))<<2|(i=a.indexOf(e.charAt(c++)))>>4,r=(15&i)<<4|(s=a.indexOf(e.charAt(c++)))>>2,n=(3&s)<<6|(o=a.indexOf(e.charAt(c++))),u+=String.fromCharCode(t),64!=s&&0!=r&&(u+=String.fromCharCode(r)),64!=o&&0!=n&&(u+=String.fromCharCode(n));return u}(r))}var H=function(e,t){var r,n="".concat(null!==(r=t.storageKey)&&void 0!==r?r:D,"-").concat(t.clientId),i=Y(e,n);if(!i)return null;try{return JSON.parse(decodeURIComponent(i))}catch(e){return console.error("Failed to parse session from cookie",e),null}},Y=function(e,t){if(!e)return null;var r="function"==typeof e.get?function(t){var r;return null===(r=e.get(t))||void 0===r?void 0:r.value}:function(t){return e[t]},n=r(t);if(n)return n;for(var i=[],s=0;;s++){var o=r("".concat(t,".").concat(s));if(!o)break;i.push(o)}return i.length?i.join(""):null},Q=function(e){return"object"==typeof e&&null!==e&&"access_token"in e&&"refresh_token"in e&&"expires_at"in e},X=function(e){var t=new Map;if(!e)return t;for(var r=0,n=e.split(";");r<n.length;r++){var i=n[r].trim();if(i){var s=i.indexOf("=");if(!(s<0)){var o=Z(i.slice(0,s).trim()),a=Z(i.slice(s+1).trim());o&&t.set(o,a)}}}return t},Z=function(e){try{return decodeURIComponent(e)}catch(t){return e}},ee=function(e,t,r){var n="".concat(encodeURIComponent(e),"=").concat(encodeURIComponent(t));return void 0!==r.maxAge&&(n+="; Max-Age=".concat(r.maxAge)),r.domain&&(n+="; Domain=".concat(r.domain)),r.path&&(n+="; Path=".concat(r.path)),r.sameSite&&(n+="; SameSite=".concat(r.sameSite)),(!0===r.secure||"None"===r.sameSite)&&(n+="; Secure"),n},te=function(e,t){return ee(e,"",{domain:t.domain,path:t.path,sameSite:t.sameSite,secure:t.secure,maxAge:0})},re=3200,ne=function(e,t){return"".concat(e,".").concat(t)},ie=function(e,t){var r="".concat(t,"."),n=[];return e.forEach(function(e,t){if(t.startsWith(r)){var i=t.slice(r.length);/^\d+$/.test(i)&&n.push({name:t,idx:Number(i)})}}),n.sort(function(e,t){return e.idx-t.idx}),n.map(function(e){return e.name})},se=function(e,t){void 0===e&&(e={}),void 0===t&&(t=A()?document:null);var n=r({path:"/",sameSite:"Lax",secure:A()&&"https:"===window.location.protocol,maxAge:2592e3},e);return{getItem:function(e){return t?function(e,t){var r=e.get(t);if(void 0!==r)return r;var n=ie(e,t);return 0===n.length?null:n.map(function(t){return e.get(t)}).join("")}(X(t.cookie),e):null},setItem:function(e,r){if(t){var i=X(t.cookie);if(r.length<=re){for(var s=0,o=ie(i,e);s<o.length;s++){var a=o[s];t.cookie=te(a,n)}t.cookie=ee(e,r,n)}else{i.has(e)&&(t.cookie=te(e,n));for(var u=Math.ceil(r.length/re),c=0,l=ie(i,e);c<l.length;c++){var h=l[c];Number(h.slice(e.length+1))>=u&&(t.cookie=te(h,n))}for(var d=0,f=0;d<r.length;d+=re,f++){var v=r.slice(d,d+re);t.cookie=ee(ne(e,f),v,n)}}}},removeItem:function(e){if(t){var r=X(t.cookie);t.cookie=te(e,n);for(var i=0,s=ie(r,e);i<s.length;i++){var o=s[i];t.cookie=te(o,n)}}}}},oe={getItem:function(e){return E()?globalThis.localStorage.getItem(e):null},setItem:function(e,t){E()&&globalThis.localStorage.setItem(e,t)},removeItem:function(e){E()&&globalThis.localStorage.removeItem(e)}};function ae(e){void 0===e&&(e="");var t={},r=new URL(e);if(r.hash&&"#"===r.hash[0])try{new URLSearchParams(r.hash.substring(1)).forEach(function(e,r){t[r]=e})}catch(e){}return r.searchParams.forEach(function(e,r){t[r]=e}),t}var ue=function(e){void 0===e&&(e=[]);var t=new URL(window.location.href);e.forEach(function(e){t.searchParams.delete(e)}),t.hash="",window.history.replaceState(window.history.state,"",t.toString())};globalThis&&E()&&globalThis.localStorage&&globalThis.localStorage.getItem("faable.auth.locks.debug");var ce=function(e){function r(t){var r=e.call(this,t)||this;return r.isAcquireTimeout=!0,r}return t(r,e),r}(Error);function le(e,t,r){return n(this,void 0,void 0,function(){return i(this,function(e){switch(e.label){case 0:return[4,r()];case 1:return[2,e.sent()]}})})}!function(e){function r(){return null!==e&&e.apply(this,arguments)||this}t(r,e)}(ce);var he=function(e){function r(t){var r=e.call(this,{debug:t.debug})||this;return r.lockAcquired=!1,r.pendingInLock=[],r.lock=t.lock||le,r.storageKey=t.storageKey,r}return t(r,e),r.prototype._acquireLock=function(e,t){return n(this,void 0,void 0,function(){var r,o,a=this;return i(this,function(u){switch(u.label){case 0:this._debug("#_acquireLock","begin",e),u.label=1;case 1:return u.trys.push([1,,3,4]),this.lockAcquired?(r=this.pendingInLock.length?this.pendingInLock[this.pendingInLock.length-1]:Promise.resolve(),o=n(a,void 0,void 0,function(){return i(this,function(e){switch(e.label){case 0:return[4,r];case 1:return e.sent(),[4,t()];case 2:return[2,e.sent()]}})}),this.pendingInLock.push(n(a,void 0,void 0,function(){return i(this,function(e){switch(e.label){case 0:return e.trys.push([0,2,,3]),[4,o];case 1:case 2:return e.sent(),[3,3];case 3:return[2]}})})),[2,o]):[4,this.lock("lock:".concat(this.storageKey),e,function(){return n(a,void 0,void 0,function(){var e,r,o=this;return i(this,function(a){switch(a.label){case 0:this._debug("#_acquireLock","lock acquired for storage key",this.storageKey),a.label=1;case 1:return a.trys.push([1,,7,8]),this.lockAcquired=!0,e=t(),this.pendingInLock.push(n(o,void 0,void 0,function(){return i(this,function(t){switch(t.label){case 0:return t.trys.push([0,2,,3]),[4,e];case 1:case 2:return t.sent(),[3,3];case 3:return[2]}})})),[4,e];case 2:a.sent(),a.label=3;case 3:return this.pendingInLock.length?(r=s([],this.pendingInLock,!0),[4,Promise.all(r)]):[3,5];case 4:return a.sent(),this.pendingInLock.splice(0,r.length),[3,3];case 5:return[4,e];case 6:return[2,a.sent()];case 7:return this._debug("#_acquireLock","lock released for storage key",this.storageKey),this.lockAcquired=!1,[7];case 8:return[2]}})})})];case 2:return[2,u.sent()];case 3:return this._debug("#_acquireLock","end"),[7];case 4:return[2]}})})},r}(u),de=3e4,fe=function(e){function o(t){var r,n,i=this,o=(null==t?void 0:t.debug)||!1;if((i=e.call(this,{debug:o})||this).initializePromise=null,i._lastInitializeResult=null,i.detectSessionInUrl=!0,i.autoRefreshTicker=null,i.visibilityChangedCallback=null,i.refreshingDeferred=null,i._session=null,i.sessionCheckExpiryDays=1,i.redirectUri=(null==t?void 0:t.redirectUri)||"",!(null==t?void 0:t.domain))throw new Error("Missing domain");if(i.domainUrl=function(e){var t=(null!=e?e:"").trim().replace(/\/+$/,""),r=t.match(/^(https?):\/\/(.*)$/i);if(r){var n=r[2].replace(/^(https?:\/\/)+/i,"");return"".concat(r[1].toLowerCase(),"://").concat(n)}var i="undefined"!=typeof location&&(null===location||void 0===location?void 0:location.protocol)?location.protocol.replace(/:$/,""):"https";return"".concat(i,"://").concat(t)}(t.domain),i.tokenIssuer=(n=i.domainUrl,"".concat(n)),!t.clientId)throw new Error("Missing clientId");i.clientId=t.clientId,i.audience=t.audience,i.api=new _(i.domainUrl,{debug:o});var a=(null==t?void 0:t.storageKey)||D;return i.storageKey="".concat(a,"-").concat(i.clientId),i.storage=function(e){var t=e.storage,r=e.cookieOptions;return"cookie"===t||r?se(r):"localStorage"===t||void 0===t?oe:t}(t),i.lock=new he({lock:t.lock,storageKey:i.storageKey,debug:t.debug}),i.broadcastSync=new O(A()?i.storageKey:"",function(e){for(var t=[],r=1;r<arguments.length;r++)t[r-1]=arguments[r];return i._debug.apply(i,s([e],t,!1))}),i.flowType=null!==(r=t.flowType)&&void 0!==r?r:A()?"pkce":"implicit",i.autoRefreshToken=!0,i.initialize(),i}return t(o,e),Object.defineProperty(o.prototype,"session",{get:function(){return this._session},enumerable:!1,configurable:!0}),o.prototype.initialize=function(){return n(this,void 0,void 0,function(){var e,t=this;return i(this,function(r){switch(r.label){case 0:return this.initializePromise?[4,this.initializePromise]:[3,2];case 1:return[2,r.sent()];case 2:return this.initializePromise=n(t,void 0,void 0,function(){var e=this;return i(this,function(t){switch(t.label){case 0:return[4,this.lock._acquireLock(-1,function(){return n(e,void 0,void 0,function(){return i(this,function(e){switch(e.label){case 0:return[4,this._initialize()];case 1:return[2,e.sent()]}})})})];case 1:return[2,t.sent()]}})}),[4,this.initializePromise];case 3:return e=r.sent(),this._lastInitializeResult=e,[2,e]}})})},Object.defineProperty(o.prototype,"lastInitializeResult",{get:function(){return this._lastInitializeResult},enumerable:!1,configurable:!0}),o.prototype.handleRedirectCallback=function(){return n(this,void 0,void 0,function(){return i(this,function(e){switch(e.label){case 0:return[4,this.initialize()];case 1:return[2,e.sent()]}})})},o.prototype._initialize=function(){return n(this,void 0,void 0,function(){var e,t,r,s,o,a,u,c,l,h=this;return i(this,function(d){switch(d.label){case 0:return d.trys.push([0,8,9,11]),[4,this._detectFlowType()];case 1:return e=d.sent(),this._debug("#_initialize()","begin","flow_type",e),e?[4,this._getSessionFromURL(e)]:[3,6];case 2:return t=d.sent(),r=t.data,(s=t.error)?(this._debug("#_initialize()","error detecting session from URL",s),"Identity is already linked"===(null==s?void 0:s.message)||"Identity is already linked to another user"===(null==s?void 0:s.message)?[2,{error:s}]:[4,this._removeSession()]):[3,4];case 3:return d.sent(),[2,{error:s}];case 4:return o=r.session,a=r.redirectType,u=r.returnTo,c=r.is_new_user,this._debug("#_initialize()","detected session in URL",o,"redirect type",a),[4,this._saveSession(o)];case 5:return d.sent(),setTimeout(function(){return n(h,void 0,void 0,function(){return i(this,function(e){switch(e.label){case 0:return"recovery"!==a?[3,2]:[4,this._notifyAllSubscribers("PASSWORD_RECOVERY",o)];case 1:return e.sent(),[3,4];case 2:return[4,this._notifyAllSubscribers("SIGNED_IN",o)];case 3:e.sent(),e.label=4;case 4:return[2]}})})},0),[2,{error:null,redirectType:a,returnTo:u,is_new_user:c}];case 6:return[4,this._recoverAndRefresh()];case 7:return d.sent(),[2,{error:null}];case 8:return j(l=d.sent())?[2,{error:l}]:[2,{error:new V("Unexpected error during initialization",l)}];case 9:return[4,this._handleVisibilityChange()];case 10:return d.sent(),this._debug("#_initialize()","end"),[7];case 11:return[2]}})})},o.prototype._getSessionFromURL=function(e){return n(this,void 0,void 0,function(){var t,r,n,s,o,a,u,l,h,d,f,v,p,b,_,g,y,w,m,k,S;return i(this,function(i){switch(i.label){case 0:if(i.trys.push([0,4,,5]),t=ae(null==c?void 0:c.location.href),r="true"===t.signup,"pkce"!=e)return[3,2];if(!t.code)throw new F("No code detected.");return[4,this._exchangeCodeForSession(t.code)];case 1:if(n=i.sent(),s=n.data,o=n.error)throw o;return ue(["code","signup"]),[2,{data:{session:s.session,redirectType:s.redirectType,returnTo:s.returnTo,is_new_user:r},error:null}];case 2:if(t.error||t.error_description||t.error_code)throw new M(t.error_description||"Error in URL with unspecified error_description",{error:t.error||"unspecified_error",code:t.error_code||"unspecified_code"});if(a=t.provider_token,u=t.provider_refresh_token,l=t.access_token,h=t.refresh_token,d=t.expires_in,f=t.expires_at,v=t.token_type,!l||!h)throw new M("No session defined in URL");return p=void 0,b=void 0,d?(k=function(e){var t=e.expires_in,r=e.expires_at,n=e.refreshTick,i=Math.round(Date.now()/1e3),s=parseInt(t),o=i+s;r&&(o=parseInt(r));var a=o-i;1e3*a<=n&&console.warn("@faable/auth-js: Session as retrieved from URL expires in ".concat(a,"s, should have been closer to ").concat(s,"s"));var u=o-s;return i-u>=120?console.warn("@faable/auth-js: Session as retrieved from URL was issued over 120s ago, URL could be stale",u,o,i):i-u<0&&console.warn("@faable/auth-js: Session as retrieved from URL was issued in the future? Check the device clok for skew",u,o,i),{expiresIn:s,expiresAt:o}}({expires_in:d,expires_at:f,refreshTick:de}),p=k.expiresAt,b=k.expiresIn):(S=function(e,t,r){var n;void 0===r&&(r=Date.now()/1e3);var i=$(e),s=null!=t&&""!==t?Number(t):null!==(n=i.exp)&&void 0!==n?n:r;return{expiresAt:s,expiresIn:s-r}}(l,f),p=S.expiresAt,b=S.expiresIn),[4,this._getUser(l)];case 3:if(_=i.sent(),g=_.data,(y=_.error)||!g)throw y;return w={provider_token:a,provider_refresh_token:u,access_token:l,expires_in:b,expires_at:p,refresh_token:h,token_type:v||"bearer",user:g},ue(["access_token","expires_in","refresh_token","token_type","scope","signup"]),this._debug("#_getSessionFromURL()","clearing window.location.hash"),[2,{data:{session:w,redirectType:t.type,returnTo:null,is_new_user:r},error:null}];case 4:if(m=i.sent(),this._debug(m),j(m))return[2,{data:{session:null,redirectType:null,returnTo:null,is_new_user:!1},error:m}];throw m;case 5:return[2]}})})},o.prototype._exchangeCodeForSession=function(e){return n(this,void 0,void 0,function(){var t,n,s,o,a,u,c,l,h,d,f,v,b,_;return i(this,function(i){switch(i.label){case 0:return[4,k(this.storage,"".concat(this.storageKey,"-code-verifier"))];case 1:return(t=i.sent())?(n=t.verifier,s=t.redirectType,o=void 0===s?null:s,a=t.returnTo,u=void 0===a?null:a,[4,p("".concat(this.domainUrl,"/oauth/token"),r({client_id:this.clientId,grant_type:"authorization_code",code:e,code_verifier:n},this.audience?{audience:this.audience}:{}))]):[2,{data:{user:null,session:null,redirectType:null,returnTo:null},error:new F("No active PKCE code verifier — the authorization flow has expired or was not started")}];case 2:if(c=i.sent(),l=C(c),h=l.data,d=l.error,!h)throw new Error("Missing data");return[4,this.storage.removeItem("".concat(this.storageKey,"-code-verifier"))];case 3:return i.sent(),d?[2,{data:{user:null,session:null,redirectType:null,returnTo:null},error:d}]:h&&h.session&&h.user?(f=h.session)?[4,this._getUser(f.access_token)]:[3,7]:[2,{data:{user:null,session:null,redirectType:null,returnTo:null},error:new W}];case 4:if(v=i.sent(),b=v.data,(_=v.error)||!b)throw _;return f=r(r({},f),{user:b}),h.session=f,[4,this._saveSession(f)];case 5:return i.sent(),[4,this._notifyAllSubscribers("SIGNED_IN",f)];case 6:i.sent(),i.label=7;case 7:return[2,{data:r(r({},h),{redirectType:null!=o?o:null,returnTo:null!=u?u:null}),error:d}]}})})},o.prototype._handleVisibilityChange=function(){return n(this,void 0,void 0,function(){var e,t=this;return i(this,function(r){switch(r.label){case 0:if(this._debug("#_handleVisibilityChange()"),!A()||!(null==c?void 0:c.addEventListener))return this.autoRefreshToken&&this.startAutoRefresh(),[2,!1];r.label=1;case 1:return r.trys.push([1,3,,4]),this.visibilityChangedCallback=function(){return n(t,void 0,void 0,function(){return i(this,function(e){switch(e.label){case 0:return[4,this._onVisibilityChanged(!1)];case 1:return[2,e.sent()]}})})},null==c||c.addEventListener("visibilitychange",this.visibilityChangedCallback),[4,this._onVisibilityChanged(!0)];case 2:return r.sent(),[3,4];case 3:return e=r.sent(),console.error("_handleVisibilityChange",e),[3,4];case 4:return[2]}})})},o.prototype._onVisibilityChanged=function(e){return n(this,void 0,void 0,function(){var t,r=this;return i(this,function(s){switch(s.label){case 0:return t="#_onVisibilityChanged(".concat(e,")"),this._debug(t,"visibilityState",h.visibilityState),"visible"!==h.visibilityState?[3,4]:(this.autoRefreshToken&&this._startAutoRefresh(),e?[3,3]:[4,this.initializePromise]);case 1:return s.sent(),[4,this.lock._acquireLock(-1,function(){return n(r,void 0,void 0,function(){return i(this,function(e){switch(e.label){case 0:return"visible"!==h.visibilityState?(this._debug(t,"acquired the lock to recover the session, but the browser visibilityState is no longer visible, aborting"),[2]):[4,this._recoverAndRefresh()];case 1:return e.sent(),[2]}})})})];case 2:s.sent(),s.label=3;case 3:return[3,5];case 4:"hidden"===h.visibilityState&&this.autoRefreshToken&&this._stopAutoRefresh(),s.label=5;case 5:return[2]}})})},o.prototype._recoverAndRefresh=function(){return n(this,void 0,void 0,function(){var e,t,r,n,s,o,a;return i(this,function(i){switch(i.label){case 0:e="#_recoverAndRefresh()",this._debug(e,"begin"),i.label=1;case 1:return i.trys.push([1,12,13,14]),[4,w(this.storage,this.storageKey)];case 2:return t=i.sent(),this._debug(e,"session from storage",t),Q(t)?[3,5]:(this._debug(e,"session is not valid"),null===t?[3,4]:[4,this._removeSession()]);case 3:i.sent(),i.label=4;case 4:return[2];case 5:return r=Math.round(Date.now()/1e3),n=(null!==(a=t.expires_at)&&void 0!==a?a:1/0)<r+10,this._debug(e,"session has".concat(n?"":" not"," expired with margin of ").concat(10,"s")),n?this.autoRefreshToken&&t.refresh_token?[4,this._callRefreshToken(t.refresh_token)]:[3,8]:[3,9];case 6:return(s=i.sent().error)?(console.error(s),G(s)?[3,8]:(this._debug(e,"refresh failed with a non-retryable error, removing the session",s),[4,this._removeSession()])):[3,8];case 7:i.sent(),i.label=8;case 8:return[3,11];case 9:return this._session=t,[4,this._notifyAllSubscribers("SIGNED_IN",t)];case 10:i.sent(),i.label=11;case 11:return[3,14];case 12:return o=i.sent(),this._debug(e,"error",o),console.error(o),[2];case 13:return this._debug(e,"end"),[7];case 14:return[2]}})})},o.prototype._removeVisibilityChangedCallback=function(){this._debug("#_removeVisibilityChangedCallback()");var e=this.visibilityChangedCallback;this.visibilityChangedCallback=null;try{e&&A()&&(null==c?void 0:c.removeEventListener)&&c.removeEventListener("visibilitychange",e)}catch(e){console.error("removing visibilitychange callback failed",e)}},o.prototype.startAutoRefresh=function(){return n(this,void 0,void 0,function(){return i(this,function(e){switch(e.label){case 0:return this._removeVisibilityChangedCallback(),[4,this._startAutoRefresh()];case 1:return e.sent(),[2]}})})},o.prototype._startAutoRefresh=function(){return n(this,void 0,void 0,function(){var e,t=this;return i(this,function(r){switch(r.label){case 0:return[4,this._stopAutoRefresh()];case 1:return r.sent(),this._debug("#_startAutoRefresh()"),e=setInterval(function(){return t._autoRefreshTokenTick()},de),this.autoRefreshTicker=e,e&&"object"==typeof e&&"function"==typeof e.unref?e.unref():void 0!==globalThis.Deno&&"function"==typeof globalThis.Deno.unrefTimer&&globalThis.Deno.unrefTimer(e),setTimeout(function(){return n(t,void 0,void 0,function(){return i(this,function(e){switch(e.label){case 0:return[4,this.initializePromise];case 1:return e.sent(),[4,this._autoRefreshTokenTick()];case 2:return e.sent(),[2]}})})},0),[2]}})})},o.prototype._stopAutoRefresh=function(){return n(this,void 0,void 0,function(){var e;return i(this,function(t){return this._debug("#_stopAutoRefresh()"),e=this.autoRefreshTicker,this.autoRefreshTicker=null,e&&clearInterval(e),[2]})})},o.prototype._autoRefreshTokenTick=function(){return n(this,void 0,void 0,function(){var e,t=this;return i(this,function(r){switch(r.label){case 0:this._debug("#_autoRefreshTokenTick()","begin"),r.label=1;case 1:return r.trys.push([1,3,,4]),[4,this.lock._acquireLock(0,function(){return n(t,void 0,void 0,function(){var e,t,r=this;return i(this,function(s){switch(s.label){case 0:s.trys.push([0,,5,6]),e=Date.now(),s.label=1;case 1:return s.trys.push([1,3,,4]),[4,this._useSession(function(t){return n(r,void 0,void 0,function(){var r,n;return i(this,function(i){switch(i.label){case 0:return(r=t.data.session)&&r.refresh_token&&r.expires_at?(n=Math.floor((1e3*r.expires_at-e)/de),this._debug("#_autoRefreshTokenTick()","access token expires in ".concat(n," ticks, a tick lasts ").concat(de,"ms, refresh threshold is ").concat(3," ticks")),n<=3?[4,this._callRefreshToken(r.refresh_token)]:[3,2]):(this._debug("#_autoRefreshTokenTick()","no session"),[2]);case 1:i.sent(),i.label=2;case 2:return[2]}})})})];case 2:return[2,s.sent()];case 3:return t=s.sent(),console.error("Auto refresh tick failed with error. This is likely a transient error.",t),[3,4];case 4:return[3,6];case 5:return this._debug("#_autoRefreshTokenTick()","end"),[7];case 6:return[2]}})})})];case 2:return r.sent(),[3,4];case 3:if(!((e=r.sent()).isAcquireTimeout||e instanceof ce))throw e;return this._debug("auto refresh token tick lock not available"),[3,4];case 4:return[2]}})})},o.prototype._detectFlowType=function(){return n(this,void 0,void 0,function(){var e,t;return i(this,function(r){return e=ae(null==c?void 0:c.location.href),(t=A())&&e.code?[2,"pkce"]:t&&(e.access_token||e.error_description)?[2,"implicit"]:[2,null]})})},o.prototype._scope=function(){return this.scope||"openid profile email"},o.prototype._getUrlForConnection=function(e,t){return n(this,void 0,void 0,function(){var n,s,o,a,u,l,h;return i(this,function(i){switch(i.label){case 0:return n=t.queryParams||{},s={client_id:this.clientId,response_type:t.response_type,redirect_uri:t.redirectTo||this.redirectUri||(null==c?void 0:c.location.origin),scope:t.scopes||this._scope()},"pkce"!==this.flowType?[3,2]:[4,x(this.storage,this.storageKey,!1,t.returnTo)];case 1:o=i.sent(),a=o[0],u=o[1],n=r(r({},n),{code_challenge:a,code_challenge_method:u}),i.label=2;case 2:return t.connection_id?s.connection_id=t.connection_id:t.connection&&(s.connection=t.connection),(l=null!==(h=t.audience)&&void 0!==h?h:this.audience)&&(s.audience=l),[2,"".concat(e,"?").concat(new URLSearchParams(r(r({},n),s)))]}})})},o.prototype.signInWithOauthConnection=function(e){return n(this,void 0,void 0,function(){return i(this,function(t){switch(t.label){case 0:return[4,this._handleConnectionSignIn({connection:e.connection,connection_id:e.connection_id,redirectTo:null==e?void 0:e.redirectTo,returnTo:null==e?void 0:e.returnTo,scopes:null==e?void 0:e.scopes,queryParams:e.queryParams,skipBrowserRedirect:e.skipBrowserRedirect,audience:e.audience})];case 1:return[2,t.sent()]}})})},o.prototype.signInWithUsernamePassword=function(e){return n(this,void 0,void 0,function(){var t,n,s;return i(this,function(i){switch(i.label){case 0:return t=null!==(s=e.audience)&&void 0!==s?s:this.audience,[4,p("".concat(this.domainUrl,"/usernamepassword/login"),r({username:e.username,password:e.password,redirect_uri:e.redirectTo||this.redirectUri||(null==c?void 0:c.location.origin),client_id:this.clientId,state:e.state},t?{audience:t}:{}),{raw:!0})];case 1:return!(n=i.sent()).data||n.error?[2,{data:null,error:new V(n.error||"Error in username password login",n.error)}]:(function(e,t){var r=t.createElement("div");r.innerHTML=e;var n=t.body.appendChild(r).children[0];if(!n)throw new Error("Auth response did not contain a submittable form");n.submit()}(n.data,h),[2,{data:null,error:null}])}})})},o.prototype.signUp=function(e){return n(this,void 0,void 0,function(){var t,n,s,o,a;return i(this,function(i){switch(i.label){case 0:return(null==e?void 0:e.email)&&(null==e?void 0:e.password)?[4,p("".concat(this.domainUrl,"/dbconnections/signup"),r(r(r(r(r({client_id:this.clientId,email:e.email,password:e.password},e.name?{name:e.name}:{}),e.given_name?{given_name:e.given_name}:{}),e.family_name?{family_name:e.family_name}:{}),e.user_metadata?{user_metadata:e.user_metadata}:{}),e.connection?{connection:e.connection}:{}))]:[2,{data:null,error:new V("email and password are required",null)}];case 1:return t=i.sent(),n=t.data,(s=t.error)?(o=null==n?void 0:n.status,a=403===o?"signup_disabled":409===o?"email_exists":void 0,[2,{data:null,error:new K(String(s),null!=o?o:500,a)}]):[2,this.signInWithUsernamePassword({username:e.email,password:e.password,redirectTo:e.redirectTo,state:e.state,audience:e.audience})]}})})},o.prototype.signInWithOtp=function(e){return n(this,void 0,void 0,function(){var t,n,s,o,a,u,c,l,h,d;return i(this,function(i){switch(i.label){case 0:return t=null!==(d=e.audience)&&void 0!==d?d:this.audience,[4,p("".concat(this.domainUrl,"/oauth/token"),r({client_id:this.clientId,grant_type:"http://auth0.com/oauth/grant-type/passwordless/otp",username:e.username,otp:e.otp},t?{audience:t}:{}))];case 1:return n=i.sent(),s=C(n),o=s.data,(a=s.error)?[2,{data:{user:null,session:null},error:a}]:o&&o.session?(u=o.session,[4,this._getUser(u.access_token)]):[2,{data:{user:null,session:null},error:new W}];case 2:return c=i.sent(),l=c.data,(h=c.error)||!l?[2,{data:{user:null,session:null},error:h||new V("Could not fetch user info",null)}]:(u.user=l,[4,this._saveSession(u)]);case 3:return i.sent(),[4,this._notifyAllSubscribers("SIGNED_IN",u)];case 4:return i.sent(),[2,{data:{user:u.user,session:u},error:null}]}})})},o.prototype.signInWithPasswordless=function(e){return n(this,void 0,void 0,function(){var t,n,s;return i(this,function(i){switch(i.label){case 0:return t=null!==(s=e.audience)&&void 0!==s?s:this.audience,[4,p("".concat(this.domainUrl,"/passwordless/start"),r({client_id:this.clientId,email:e.email,send:e.type},t?{audience:t}:{}))];case 1:return[2,{data:(n=i.sent()).data,error:n.error}]}})})},o.prototype.changePassword=function(e){return n(this,void 0,void 0,function(){var t,r,n;return i(this,function(i){switch(i.label){case 0:return(null==e?void 0:e.email)?[4,p("".concat(this.domainUrl,"/dbconnections/change_password"),{email:e.email})]:[2,{data:null,error:new V("email is required",null)}];case 1:return t=i.sent(),r=t.data,n=t.error,[2,{data:null!=r?r:null,error:n?new V(String(n),n):null}]}})})},o.prototype._resolveUserId=function(e){var t,r,n,i=null!==(r=null===(t=e.user)||void 0===t?void 0:t.id)&&void 0!==r?r:null===(n=e.user)||void 0===n?void 0:n.sub;if(i)return i;try{var s=$(e.access_token);return null==s?void 0:s.sub}catch(e){return}},o.prototype.changeEmail=function(e){return n(this,void 0,void 0,function(){var t,n,s,o,a,u,c,l;return i(this,function(i){switch(i.label){case 0:return(null==e?void 0:e.new_email)?[4,this.getSession()]:[2,{data:null,error:new V("new_email is required",null)}];case 1:return t=i.sent(),n=t.data,s=t.error,o=null==n?void 0:n.session,s||!o?[2,{data:null,error:s||new q}]:(a=this._resolveUserId(o))?[4,p("".concat(this.domainUrl,"/user/").concat(a,"/change-email"),r(r({new_email:e.new_email},e.verification_mode?{verification_mode:e.verification_mode}:{}),e.redirect_uri?{redirect_uri:e.redirect_uri}:{}),{token:o.access_token})]:[2,{data:null,error:new q}];case 2:return u=i.sent(),c=u.data,l=u.error,[2,{data:null!=c?c:null,error:l?new V(String(l),l):null}]}})})},o.prototype.buildAuthorizeUrl=function(e){var t;void 0===e&&(e={});var r={client_id:this.clientId,redirect_uri:e.redirectTo||this.redirectUri||(null==c?void 0:c.location.origin),response_type:g(e,A()),audience:null!==(t=e.audience)&&void 0!==t?t:this.audience,scope:e.scope,connection:e.connection},n=Object.fromEntries(Object.entries(r).filter(function(e){return!!e[1]}));return"".concat(this.domainUrl,"/authorize?").concat(new URLSearchParams(n).toString())},o.prototype.authorize=function(e){var t=this.buildAuthorizeUrl(e);B.redirect(t)},o.prototype._handleConnectionSignIn=function(e){return n(this,void 0,void 0,function(){var t;return i(this,function(r){switch(r.label){case 0:return[4,this._getUrlForConnection("".concat(this.domainUrl,"/authorize"),{response_type:A()?"code":"token",connection:e.connection,connection_id:e.connection_id,redirectTo:e.redirectTo,returnTo:e.returnTo,scopes:e.scopes,queryParams:e.queryParams,audience:e.audience})];case 1:return t=r.sent(),this._debug("#_handleConnectionSignIn()","options",e,"url",t),!A()||e.skipBrowserRedirect?[3,3]:(null==c||c.location.assign(t),[4,new Promise(function(){})]);case 2:r.sent(),r.label=3;case 3:return[2,{data:{url:t},error:null}]}})})},o.prototype.setSession=function(e){return n(this,void 0,void 0,function(){var t=this;return i(this,function(r){switch(r.label){case 0:return[4,this.initializePromise];case 1:return r.sent(),[4,this.lock._acquireLock(-1,function(){return n(t,void 0,void 0,function(){return i(this,function(t){switch(t.label){case 0:return[4,this._setSession(e)];case 1:return[2,t.sent()]}})})})];case 2:return[2,r.sent()]}})})},o.prototype.getSession=function(){return n(this,void 0,void 0,function(){var e=this;return i(this,function(t){switch(t.label){case 0:return[4,this.initializePromise];case 1:return t.sent(),[4,this.lock._acquireLock(-1,function(){return n(e,void 0,void 0,function(){var e=this;return i(this,function(t){return[2,this._useSession(function(t){return n(e,void 0,void 0,function(){return i(this,function(e){return[2,t]})})})]})})})];case 2:return[2,t.sent()]}})})},o.prototype._useSession=function(e){return n(this,void 0,void 0,function(){var t;return i(this,function(r){switch(r.label){case 0:this._debug("#_useSession","begin"),r.label=1;case 1:return r.trys.push([1,,4,5]),[4,this.__loadSession()];case 2:return t=r.sent(),[4,e(t)];case 3:return[2,r.sent()];case 4:return this._debug("#_useSession","end"),[7];case 5:return[2]}})})},o.prototype.__loadSession=function(){return n(this,void 0,void 0,function(){var e,t,r,n,s,o,a;return i(this,function(i){switch(i.label){case 0:this._debug("#__loadSession()","begin"),this.lock.lockAcquired||this._debug("#__loadSession()","used outside of an acquired lock!",(new Error).stack),i.label=1;case 1:return i.trys.push([1,,7,8]),e=null,[4,w(this.storage,this.storageKey)];case 2:return t=i.sent(),this._debug("#getSession()","session from storage",t),null===t?[3,5]:Q(t)?(e=t,[3,5]):[3,3];case 3:return this._debug("#getSession()","session from storage is not valid"),[4,this._removeSession()];case 4:i.sent(),i.label=5;case 5:return e?(r=!!e.expires_at&&e.expires_at<=Date.now()/1e3,this._debug("#__loadSession()","session has".concat(r?"":" not"," expired"),"expires_at",e.expires_at),r?[4,this._callRefreshToken(e.refresh_token)]:(this.storage.isServer&&(n=new Proxy(e,{get:function(e,t,r){return"user"===t&&console.warn("Reading `session.user` from a server-side cookie store can be insecure: the value is whatever the cookie contains and has not been verified against Faable Auth. Re-fetch the user with `auth.getUser()` (or verify the access token yourself) before trusting it on the server."),Reflect.get(e,t,r)}}),e=n),[2,{data:{session:e},error:null}])):[2,{data:{session:null},error:null}];case 6:return s=i.sent(),o=s.session,(a=s.error)?[2,{data:{session:null},error:a}]:[2,{data:{session:o},error:null}];case 7:return this._debug("#__loadSession()","end"),[7];case 8:return[2]}})})},o.prototype._removeSession=function(){return n(this,void 0,void 0,function(){return i(this,function(e){switch(e.label){case 0:return this._debug("#_removeSession()"),this._session=null,[4,this.storage.removeItem(this.storageKey)];case 1:return e.sent(),[2]}})})},o.prototype._setSession=function(e){return n(this,void 0,void 0,function(){var t,r,n,s,o,a,u,c,l,h,d;return i(this,function(i){switch(i.label){case 0:if(i.trys.push([0,7,,8]),!e.access_token||!e.refresh_token)throw new q;return t=Date.now()/1e3,r=t,n=!0,s=null,(o=$(e.access_token)).exp&&(r=o.exp,n=r<=t),n?[4,this._callRefreshToken(e.refresh_token)]:[3,2];case 1:return a=i.sent(),u=a.session,(h=a.error)?[2,{data:{user:null,session:null},error:h}]:u?(s=u,[3,6]):[2,{data:{user:null,session:null},error:null}];case 2:return[4,this._getUser(e.access_token)];case 3:if(c=i.sent(),l=c.data,(h=c.error)||!l)throw h;return s={access_token:e.access_token,refresh_token:e.refresh_token,user:l,token_type:"bearer",expires_in:r-t,expires_at:r},[4,this._saveSession(s)];case 4:return i.sent(),[4,this._notifyAllSubscribers("SIGNED_IN",s)];case 5:i.sent(),i.label=6;case 6:return[2,{data:{user:s.user,session:s},error:null}];case 7:if(j(d=i.sent()))return[2,{data:{session:null,user:null},error:d}];throw d;case 8:return[2]}})})},o.prototype._saveSession=function(e){return n(this,void 0,void 0,function(){return i(this,function(t){switch(t.label){case 0:return this._debug("#_saveSession()",e),this._session=e,[4,y(this.storage,this.storageKey,e)];case 1:return t.sent(),[2]}})})},o.prototype._getUser=function(e){return n(this,void 0,void 0,function(){var t;return i(this,function(r){switch(r.label){case 0:if(!e)throw new Error("Cannot fetch user without token");return this._debug("#_getUser() begin"),[4,b("".concat(this.domainUrl,"/me"),{token:e})];case 1:return t=r.sent(),this._debug("#_getUser() end"),[2,{data:t.data,error:t.error}]}})})},o.prototype._callRefreshToken=function(e){return n(this,void 0,void 0,function(){var t,r,n,s,o,a,u,c;return i(this,function(i){switch(i.label){case 0:if(!e)throw new q;if(this.refreshingDeferred)return[2,this.refreshingDeferred.promise];t="#_callRefreshToken(".concat(e.substring(0,5),"...)"),this._debug(t,"begin"),i.label=1;case 1:return i.trys.push([1,5,10,11]),this.refreshingDeferred=new U,[4,(l=this._refreshAccessToken(e),h=3e4,d="Token refresh timed out",v=new Promise(function(e,t){f=setTimeout(function(){return t(new Error(d))},h)}),Promise.race([l.finally(function(){void 0!==f&&clearTimeout(f)}),v]))];case 2:if(r=i.sent(),n=r.data,s=r.error)throw s;if(!n.session)throw new q;return[4,this._saveSession(n.session)];case 3:return i.sent(),[4,this._notifyAllSubscribers("TOKEN_REFRESHED",n.session)];case 4:return i.sent(),a={session:n.session,error:null},this.refreshingDeferred.resolve(a),[2,a];case 5:return o=i.sent(),this._debug(t,"error",o),j(o)?(a={session:null,error:o},G(o)?[3,8]:[4,this._removeSession()]):[3,9];case 6:return i.sent(),[4,this._notifyAllSubscribers("SIGNED_OUT",null)];case 7:i.sent(),i.label=8;case 8:return null===(u=this.refreshingDeferred)||void 0===u||u.resolve(a),[2,a];case 9:throw null===(c=this.refreshingDeferred)||void 0===c||c.reject(o),o;case 10:return this.refreshingDeferred=null,this._debug(t,"end"),[7];case 11:return[2]}var l,h,d,f,v})})},o.prototype._refreshAccessToken=function(e){return n(this,void 0,void 0,function(){var t,s,o,a=this;return i(this,function(u){switch(u.label){case 0:t="#_refreshAccessToken(".concat(e.substring(0,5),"...)"),this._debug(t,"begin"),u.label=1;case 1:return u.trys.push([1,3,4,5]),s=Date.now(),[4,P(function(s){return n(a,void 0,void 0,function(){var n,o,a,u,c,l,h;return i(this,function(i){switch(i.label){case 0:return s>0?[4,L(200*Math.pow(2,s-1))]:[3,2];case 1:i.sent(),i.label=2;case 2:return this._debug(t,"refreshing attempt",s),[4,p("".concat(this.domainUrl,"/oauth/token"),r({client_id:this.clientId,grant_type:"refresh_token",refresh_token:e},this.audience?{audience:this.audience}:{}))];case 3:if((n=i.sent()).error)throw new V("Refresh token request failed: ".concat(n.error),n.error);if(o=C(n),!(null===(l=o.data.session)||void 0===l?void 0:l.access_token))throw new W;return[4,this._getUser(null===(h=o.data.session)||void 0===h?void 0:h.access_token)];case 4:if(a=i.sent(),u=a.data,c=a.error)throw new V("Could not fetch user info",c);if(!u)throw new V("Refresh response missing user",null);return[2,{data:{session:r(r({},o.data.session),{user:u}),user:u},error:null}]}})})},function(e,t){var r=200*Math.pow(2,e);return t&&G(t)&&Date.now()+r-s<de})];case 2:return[2,u.sent()];case 3:if(o=u.sent(),this._debug(t,"error",o),j(o))return[2,{data:{session:null,user:null},error:o}];throw o;case 4:return this._debug(t,"end"),[7];case 5:return[2]}})})},o.prototype._notifyAllSubscribers=function(e,t){return n(this,arguments,void 0,function(e,t,r){var n;return void 0===r&&(r=!0),i(this,function(i){switch(i.label){case 0:n="#_notifyAllSubscribers(".concat(e,")"),this._debug(n,"begin",t,"broadcast = ".concat(r)),i.label=1;case 1:return i.trys.push([1,,3,4]),[4,this.broadcastSync.notify(e,t,r)];case 2:return i.sent(),[3,4];case 3:return this._debug(n,"end"),[7];case 4:return[2]}})})},o.prototype.signOut=function(){return n(this,arguments,void 0,function(e){var t=this;return void 0===e&&(e={scope:"global"}),i(this,function(r){switch(r.label){case 0:return[4,this.initializePromise];case 1:return r.sent(),[4,this.lock._acquireLock(-1,function(){return n(t,void 0,void 0,function(){return i(this,function(t){switch(t.label){case 0:return[4,this._signOut(e)];case 1:return[2,t.sent()]}})})})];case 2:return[2,r.sent()]}})})},o.prototype._signOut=function(){return n(this,arguments,void 0,function(e){var t=this,r=(void 0===e?{scope:"global"}:e).scope;return i(this,function(e){switch(e.label){case 0:return[4,this._useSession(function(e){return n(t,void 0,void 0,function(){var t,n,s,o;return i(this,function(i){switch(i.label){case 0:return t=e.data,(n=e.error)?[2,{error:n}]:(null===(o=t.session)||void 0===o?void 0:o.access_token)?[4,this.api.signOut({client_id:this.clientId})]:[3,2];case 1:if((s=i.sent().error)&&(!function(e){return j(e)&&"AuthApiError"===e.name}(s)||404!==s.status&&401!==s.status))return[2,{error:s}];i.label=2;case 2:return"others"===r?[3,6]:[4,this._removeSession()];case 3:return i.sent(),[4,this.storage.removeItem("".concat(this.storageKey,"-code-verifier"))];case 4:return i.sent(),[4,this._notifyAllSubscribers("SIGNED_OUT",null)];case 5:i.sent(),i.label=6;case 6:return[2,{error:null}]}})})})];case 1:return[2,e.sent()]}})})},o.prototype.onAuthStateChange=function(e){var t=this,r=this.broadcastSync.subscribe(e).subscription;return this._debug("#onAuthStateChange()","registered callback with id",r.id),n(t,void 0,void 0,function(){var e=this;return i(this,function(t){switch(t.label){case 0:return[4,this.initializePromise];case 1:return t.sent(),[4,this.lock._acquireLock(-1,function(){return n(e,void 0,void 0,function(){return i(this,function(e){return this._emitInitialSession(r),[2]})})})];case 2:return t.sent(),[2]}})}),{data:{subscription:r}}},o.prototype._emitInitialSession=function(e){return n(this,void 0,void 0,function(){var t=this;return i(this,function(r){switch(r.label){case 0:return[4,this._useSession(function(r){return n(t,void 0,void 0,function(){var t,n,s;return i(this,function(i){switch(i.label){case 0:if(i.trys.push([0,2,,4]),t=r.data.session,n=r.error)throw n;return[4,e.callback("INITIAL_SESSION",t)];case 1:return i.sent(),this._debug("INITIAL_SESSION","callback id",e.id,"session",t),[3,4];case 2:return s=i.sent(),[4,e.callback("INITIAL_SESSION",null)];case 3:return i.sent(),this._debug("INITIAL_SESSION","callback id",e.id,"error",s),console.error(s),[3,4];case 4:return[2]}})})})];case 1:return[2,r.sent()]}})})},o.prototype.refreshSession=function(e){return n(this,void 0,void 0,function(){var t=this;return i(this,function(r){switch(r.label){case 0:return[4,this.initializePromise];case 1:return r.sent(),[4,this.lock._acquireLock(-1,function(){return n(t,void 0,void 0,function(){return i(this,function(t){switch(t.label){case 0:return[4,this._refreshSession(e)];case 1:return[2,t.sent()]}})})})];case 2:return[2,r.sent()]}})})},o.prototype._refreshSession=function(e){return n(this,void 0,void 0,function(){var t,r=this;return i(this,function(s){switch(s.label){case 0:return s.trys.push([0,2,,3]),[4,this._useSession(function(t){return n(r,void 0,void 0,function(){var r,n,s,o,a,u;return i(this,function(i){switch(i.label){case 0:if(!e){if(r=t.data,n=t.error)throw n;e=null!==(u=r.session)&&void 0!==u?u:void 0}if(!(null==e?void 0:e.refresh_token))throw new q;return[4,this._callRefreshToken(e.refresh_token)];case 1:return s=i.sent(),o=s.session,(a=s.error)?[2,{data:{user:null,session:null},error:a}]:o?[2,{data:{user:o.user,session:o},error:null}]:[2,{data:{user:null,session:null},error:null}]}})})})];case 1:return[2,s.sent()];case 2:if(j(t=s.sent()))return[2,{data:{user:null,session:null},error:t}];throw t;case 3:return[2]}})})},o}(u),ve=function(e){return new fe(e)},pe=function(){};export{z as AuthError,fe as FaableAuthClient,pe as User,se as cookieStorageAdapter,ve as createClient,H as getSessionFromCookies};
|
|
6
|
+
var e=function(t,r){return e=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(e,t){e.__proto__=t}||function(e,t){for(var r in t)Object.prototype.hasOwnProperty.call(t,r)&&(e[r]=t[r])},e(t,r)};function t(t,r){if("function"!=typeof r&&null!==r)throw new TypeError("Class extends value "+String(r)+" is not a constructor or null");function n(){this.constructor=t}e(t,r),t.prototype=null===r?Object.create(r):(n.prototype=r.prototype,new n)}var r=function(){return r=Object.assign||function(e){for(var t,r=1,n=arguments.length;r<n;r++)for(var i in t=arguments[r])Object.prototype.hasOwnProperty.call(t,i)&&(e[i]=t[i]);return e},r.apply(this,arguments)};function n(e,t,r,n){return new(r||(r=Promise))(function(i,s){function o(e){try{u(n.next(e))}catch(e){s(e)}}function a(e){try{u(n.throw(e))}catch(e){s(e)}}function u(e){var t;e.done?i(e.value):(t=e.value,t instanceof r?t:new r(function(e){e(t)})).then(o,a)}u((n=n.apply(e,t||[])).next())})}function i(e,t){var r,n,i,s={label:0,sent:function(){if(1&i[0])throw i[1];return i[1]},trys:[],ops:[]},o=Object.create(("function"==typeof Iterator?Iterator:Object).prototype);return o.next=a(0),o.throw=a(1),o.return=a(2),"function"==typeof Symbol&&(o[Symbol.iterator]=function(){return this}),o;function a(a){return function(u){return function(a){if(r)throw new TypeError("Generator is already executing.");for(;o&&(o=0,a[0]&&(s=0)),s;)try{if(r=1,n&&(i=2&a[0]?n.return:a[0]?n.throw||((i=n.return)&&i.call(n),0):n.next)&&!(i=i.call(n,a[1])).done)return i;switch(n=0,i&&(a=[2&a[0],i.value]),a[0]){case 0:case 1:i=a;break;case 4:return s.label++,{value:a[1],done:!1};case 5:s.label++,n=a[1],a=[0];continue;case 7:a=s.ops.pop(),s.trys.pop();continue;default:if(!(i=s.trys,(i=i.length>0&&i[i.length-1])||6!==a[0]&&2!==a[0])){s=0;continue}if(3===a[0]&&(!i||a[1]>i[0]&&a[1]<i[3])){s.label=a[1];break}if(6===a[0]&&s.label<i[1]){s.label=i[1],i=a;break}if(i&&s.label<i[2]){s.label=i[2],s.ops.push(a);break}i[2]&&s.ops.pop(),s.trys.pop();continue}a=t.call(e,s)}catch(e){a=[6,e],n=0}finally{r=i=0}if(5&a[0])throw a[1];return{value:a[0]?a[1]:void 0,done:!0}}([a,u])}}}function s(e,t,r){if(r||2===arguments.length)for(var n,i=0,s=t.length;i<s;i++)!n&&i in t||(n||(n=Array.prototype.slice.call(t,0,i)),n[i]=t[i]);return e.concat(n||Array.prototype.slice.call(t))}"function"==typeof SuppressedError&&SuppressedError;var o="0.0.0",a=function(){function e(e){void 0===e&&(e={}),this.logger=console.log,this.logDebugMessages=!!e.debug,"function"==typeof e.debug&&(this.logger=e.debug)}return e.prototype._debug=function(){for(var e=[],t=0;t<arguments.length;t++)e[t]=arguments[t];if(this.logDebugMessages){var r=this.extraPrint?this.extraPrint():"";this.logger.apply(this,s(["FaableAuth@".concat(r," (").concat(o,") ").concat((new Date).toISOString())],e,!1))}return this},e}(),u=function(e){function r(t){void 0===t&&(t={});var n=e.call(this,t)||this;return n.instanceID=r.nextInstanceID,r.nextInstanceID+=1,n}return t(r,e),r.prototype.extraPrint=function(){return this.instanceID.toString()},r.nextInstanceID=0,r}(a),c="undefined"!=typeof window?window:void 0,l="undefined"!=typeof globalThis?globalThis:c,h=null==l?void 0:l.document,d=l.fetch,f=function(e){void 0===e&&(e={});var t={"x-faable-client":"auth-js/".concat(o)};return(null==e?void 0:e.token)&&(t=r(r({},t),{Authorization:"Bearer ".concat(null==e?void 0:e.token)})),r(r({},null==e?void 0:e.headers),t)},v=function(e){for(var t=[],r=1;r<arguments.length;r++)t[r-1]=arguments[r];return n(void 0,s([e],t,!0),void 0,function(e,t){var r,n,s;return void 0===t&&(t={}),i(this,function(i){switch(i.label){case 0:return t.raw?[4,e.text()]:[3,2];case 1:return n=i.sent(),[3,4];case 2:return[4,e.json()];case 3:n=i.sent(),i.label=4;case 4:return r=n,e.status>=300?[2,{data:r,error:t.raw?null===(s=JSON.parse(r))||void 0===s?void 0:s.message:null==r?void 0:r.message}]:[2,{data:r,error:null}]}})})},p=function(e,t){for(var o=[],a=2;a<arguments.length;a++)o[a-2]=arguments[a];return n(void 0,s([e,t],o,!0),void 0,function(e,t,n){var s;return void 0===n&&(n={}),i(this,function(i){switch(i.label){case 0:return i.trys.push([0,3,,4]),[4,d(e,{method:"POST",body:JSON.stringify(t),headers:r(r({},f(n)),{"Content-Type":"application/json"})})];case 1:return s=i.sent(),[4,v(s,n)];case 2:return[2,i.sent()];case 3:return[2,{data:null,error:i.sent()}];case 4:return[2]}})})},b=function(e){for(var t=[],o=1;o<arguments.length;o++)t[o-1]=arguments[o];return n(void 0,s([e],t,!0),void 0,function(e,t){var n;return void 0===t&&(t={}),i(this,function(i){switch(i.label){case 0:return i.trys.push([0,3,,4]),[4,d(e,r(r({},t),{method:"GET",headers:f(t)}))];case 1:return n=i.sent(),[4,v(n,t)];case 2:return[2,i.sent()];case 3:return[2,{data:null,error:i.sent()}];case 4:return[2]}})})},_=function(e){function r(t,r){var n=e.call(this,r)||this;return n.base_url=t,n}return t(r,e),r.prototype.extraPrint=function(){return"api"},r.prototype.signOut=function(e){return n(this,void 0,void 0,function(){var t,r;return i(this,function(n){switch(n.label){case 0:return t="".concat(this.base_url,"/logout?").concat(new URLSearchParams(e)),this._debug("requesting ".concat(t)),[4,b(t)];case 1:return r=n.sent(),this._debug(r),r.error?[2,{error:r.error,data:null}]:[2,{error:null,data:null}]}})})},r}(a),g=function(e,t){return e.response_type||(t?"code":"token")},y=function(e,t,r){return n(void 0,void 0,void 0,function(){return i(this,function(n){switch(n.label){case 0:return[4,e.setItem(t,JSON.stringify(r))];case 1:return n.sent(),[2]}})})},w=function(e,t){return n(void 0,void 0,void 0,function(){var r;return i(this,function(n){switch(n.label){case 0:return[4,e.getItem(t)];case 1:if(!(r=n.sent()))return[2,null];try{return[2,JSON.parse(r)]}catch(e){return[2,r]}return[2]}})})},m=function(e,t,r){return n(void 0,[e,t,r],void 0,function(e,t,r){var n,s=r.verifier,o=r.redirectType,a=r.returnTo,u=r.now,c=void 0===u?Date.now():u;return i(this,function(r){switch(r.label){case 0:return n={verifier:s,createdAt:c},o&&(n.redirectType=o),a&&(n.returnTo=a),[4,y(e,t,n)];case 1:return r.sent(),[2]}})})},k=function(e,t){for(var r=[],o=2;o<arguments.length;o++)r[o-2]=arguments[o];return n(void 0,s([e,t],r,!0),void 0,function(e,t,r){var n,s,o=(void 0===r?{}:r).now,a=void 0===o?Date.now():o;return i(this,function(r){switch(r.label){case 0:return[4,w(e,t)];case 1:return n=r.sent(),"object"!=typeof(i=n)||null===i||"string"!=typeof i.verifier||"number"!=typeof i.createdAt?[2,null]:a-n.createdAt>6e5?[4,e.removeItem(t)]:[3,3];case 2:return r.sent(),[2,null];case 3:return s={verifier:n.verifier},n.redirectType&&(s.redirectType=n.redirectType),n.returnTo&&(s.returnTo=n.returnTo),[2,s]}var i})})};function S(e){return("0"+e.toString(16)).substr(-2)}function T(e){return n(this,void 0,void 0,function(){var t,r,n,s;return i(this,function(i){switch(i.label){case 0:return t=new TextEncoder,r=t.encode(e),[4,crypto.subtle.digest("SHA-256",r)];case 1:return n=i.sent(),s=new Uint8Array(n),[2,Array.from(s).map(function(e){return String.fromCharCode(e)}).join("")]}})})}function I(e){return n(this,void 0,void 0,function(){var t;return i(this,function(r){switch(r.label){case 0:return"undefined"!=typeof crypto&&void 0!==crypto.subtle&&"undefined"!=typeof TextEncoder?[4,T(e)]:(console.warn("WebCrypto API is not supported. Code challenge method will default to use plain instead of sha256."),[2,e]);case 1:return t=r.sent(),[2,(n=t,btoa(n).replace(/\+/g,"-").replace(/\//g,"_").replace(/=+$/,""))]}var n})})}function x(e,t){return n(this,arguments,void 0,function(e,t,r,n){var s,o;return void 0===r&&(r=!1),i(this,function(i){switch(i.label){case 0:return s=function(){if("undefined"==typeof crypto||"function"!=typeof crypto.getRandomValues)throw new Error("Web Crypto API is required to generate a PKCE code verifier");var e=new Uint32Array(56);return crypto.getRandomValues(e),Array.from(e,S).join("")}(),[4,m(e,"".concat(t,"-code-verifier"),{verifier:s,redirectType:r?"PASSWORD_RECOVERY":void 0,returnTo:n})];case 1:return i.sent(),[4,I(s)];case 2:return[2,[o=i.sent(),s===o?"plain":"S256"]]}})})}var A=function(){return"undefined"!=typeof document},R={tested:!1,writable:!1},E=function(){if(!A())return!1;try{if("object"!=typeof globalThis.localStorage)return!1}catch(e){return!1}if(R.tested)return R.writable;var e="lswt-".concat(Math.random()).concat(Math.random());try{globalThis.localStorage.setItem(e,e),globalThis.localStorage.removeItem(e),R.tested=!0,R.writable=!0}catch(e){R.tested=!0,R.writable=!1}return R.writable};function C(e){var t,n,i=e.data,s=null;if(!i)throw new Error("Bad session response");return function(e){return!!e.access_token&&!!e.refresh_token&&!!e.expires_in}(i)&&(s=r({},i),!i.expires_at&&i.expires_in&&(s.expires_at=(n=i.expires_in,Math.round(Date.now()/1e3)+n))),{data:{session:s,user:null!==(t=i.user)&&void 0!==t?t:i},error:null}}var U=function(){function e(){var t=this;this.promise=new e.promiseConstructor(function(e,r){t.resolve=e,t.reject=r})}return e.promiseConstructor=Promise,e}();function P(e,t){var r=this;return new Promise(function(s,o){n(r,void 0,void 0,function(){var r,n,a;return i(this,function(i){switch(i.label){case 0:r=0,i.label=1;case 1:if(!(r<1/0))return[3,6];i.label=2;case 2:return i.trys.push([2,4,,5]),[4,e(r)];case 3:return n=i.sent(),t(r,null,n)?[3,5]:(s(n),[2]);case 4:return a=i.sent(),t(r,a)?[3,5]:(o(a),[2]);case 5:return r++,[3,1];case 6:return[2]}})})})}function L(e){return n(this,void 0,void 0,function(){return i(this,function(t){return[2,new Promise(function(t){setTimeout(function(){return t(null)},e)})]})})}var O=function(){function e(e,t){var r=this;if(this.debug=t,this.channel=null,this.subscribers=new Map,"undefined"!=typeof globalThis&&"function"==typeof globalThis.BroadcastChannel&&e)try{this.channel=new globalThis.BroadcastChannel(e),this.channel.addEventListener("message",function(e){return n(r,void 0,void 0,function(){return i(this,function(t){switch(t.label){case 0:return this.debug("broadcast_sync: received notification from another tab",e),[4,this.dispatch(e.data.event,e.data.session,!1)];case 1:return t.sent(),[2]}})})})}catch(e){console.error("Failed to create BroadcastChannel; cross-tab sync disabled",e)}}return e.prototype.subscribe=function(e){var t=this,r="xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g,function(e){var t=16*Math.random()|0;return("x"==e?t:3&t|8).toString(16)}),n={id:r,callback:e,unsubscribe:function(){t.subscribers.delete(r)}};return this.subscribers.set(r,n),{subscription:n}},e.prototype.notify=function(e,t){return n(this,arguments,void 0,function(e,t,r){return void 0===r&&(r=!0),i(this,function(n){switch(n.label){case 0:return r&&this.channel&&this.channel.postMessage({event:e,session:t}),[4,this.dispatch(e,t,r)];case 1:return n.sent(),[2]}})})},e.prototype.dispatch=function(e,t,r){return n(this,void 0,void 0,function(){var r,s,o,a=this;return i(this,function(u){switch(u.label){case 0:return r=[],s=Array.from(this.subscribers.values()).map(function(s){return n(a,void 0,void 0,function(){var n;return i(this,function(i){switch(i.label){case 0:return i.trys.push([0,2,,3]),[4,s.callback(e,t)];case 1:return i.sent(),[3,3];case 2:return n=i.sent(),r.push(n),[3,3];case 3:return[2]}})})}),[4,Promise.all(s)];case 1:if(u.sent(),r.length>0){for(o=0;o<r.length;o+=1)console.error(r[o]);throw r[0]}return[2]}})})},e.prototype.close=function(){var e;this.subscribers.clear();try{null===(e=this.channel)||void 0===e||e.close()}catch(e){}this.channel=null},e}(),D="faableauth",z=function(e){function r(t,r,n){var i=e.call(this,t)||this;return i.__isAuthError=!0,i.name="AuthError",i.status=r,i.code=n,i}return t(r,e),r}(Error),N=function(e){function r(t,r,n,i){var s=e.call(this,t,n,i)||this;return s.name=r,s.status=n,s}return t(r,e),r}(z),q=function(e){function r(){return e.call(this,"Auth session missing!","AuthSessionMissingError",400,void 0)||this}return t(r,e),r}(N);function j(e){return"object"==typeof e&&null!==e&&"__isAuthError"in e}var K=function(e){function r(t,r,n){var i=e.call(this,t,r,n)||this;return i.name="AuthApiError",i.status=r,i.code=n,i}return t(r,e),r}(z);var M=function(e){function r(t,r){void 0===r&&(r=null);var n=e.call(this,t,"AuthImplicitGrantRedirectError",500,void 0)||this;return n.details=null,n.details=r,n}return t(r,e),r.prototype.toJSON=function(){return{name:this.name,message:this.message,status:this.status,details:this.details}},r}(N),F=function(e){function r(t,r){void 0===r&&(r=null);var n=e.call(this,t,"AuthPKCEGrantCodeExchangeError",500,void 0)||this;return n.details=null,n.details=r,n}return t(r,e),r.prototype.toJSON=function(){return{name:this.name,message:this.message,status:this.status,details:this.details}},r}(N),V=function(e){function r(t,r){var n=e.call(this,t)||this;return n.name="AuthUnknownError",n.originalError=r,n}return t(r,e),r}(z),W=function(e){function r(){return e.call(this,"Auth session or user missing","AuthInvalidTokenResponseError",500,void 0)||this}return t(r,e),r}(N);function G(e){return j(e)&&"AuthRetryableFetchError"===e.name}function J(){if(!c)throw new Error("No window in environment");return c}!function(e){function r(t,r){return e.call(this,t,"AuthRetryableFetchError",r,void 0)||this}t(r,e)}(N);var B={redirect:function(e){J().location=e},getDocument:function(){return J().document},getWindow:J};function $(e){var t=e.split(".");if(3!==t.length)throw new Error("JWT is not valid: not a JWT structure");if(!/^([a-z0-9_-]{4})*($|[a-z0-9_-]{3}=?$|[a-z0-9_-]{2}(==)?$)$/i.test(t[1]))throw new Error("JWT is not valid: payload is not in base64url format");var r=t[1];return JSON.parse(function(e){var t,r,n,i,s,o,a="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=",u="",c=0;for(e=e.replace("-","+").replace("_","/");c<e.length;)t=a.indexOf(e.charAt(c++))<<2|(i=a.indexOf(e.charAt(c++)))>>4,r=(15&i)<<4|(s=a.indexOf(e.charAt(c++)))>>2,n=(3&s)<<6|(o=a.indexOf(e.charAt(c++))),u+=String.fromCharCode(t),64!=s&&0!=r&&(u+=String.fromCharCode(r)),64!=o&&0!=n&&(u+=String.fromCharCode(n));return u}(r))}var H=function(e,t){var r,n="".concat(null!==(r=t.storageKey)&&void 0!==r?r:D,"-").concat(t.clientId),i=Y(e,n);if(!i)return null;try{return JSON.parse(decodeURIComponent(i))}catch(e){return console.error("Failed to parse session from cookie",e),null}},Y=function(e,t){if(!e)return null;var r="function"==typeof e.get?function(t){var r;return null===(r=e.get(t))||void 0===r?void 0:r.value}:function(t){return e[t]},n=r(t);if(n)return n;for(var i=[],s=0;;s++){var o=r("".concat(t,".").concat(s));if(!o)break;i.push(o)}return i.length?i.join(""):null},Q=function(e){return"object"==typeof e&&null!==e&&"access_token"in e&&"refresh_token"in e&&"expires_at"in e},X=function(e){var t=new Map;if(!e)return t;for(var r=0,n=e.split(";");r<n.length;r++){var i=n[r].trim();if(i){var s=i.indexOf("=");if(!(s<0)){var o=Z(i.slice(0,s).trim()),a=Z(i.slice(s+1).trim());o&&t.set(o,a)}}}return t},Z=function(e){try{return decodeURIComponent(e)}catch(t){return e}},ee=function(e,t,r){var n="".concat(encodeURIComponent(e),"=").concat(encodeURIComponent(t));return void 0!==r.maxAge&&(n+="; Max-Age=".concat(r.maxAge)),r.domain&&(n+="; Domain=".concat(r.domain)),r.path&&(n+="; Path=".concat(r.path)),r.sameSite&&(n+="; SameSite=".concat(r.sameSite)),(!0===r.secure||"None"===r.sameSite)&&(n+="; Secure"),n},te=function(e,t){return ee(e,"",{domain:t.domain,path:t.path,sameSite:t.sameSite,secure:t.secure,maxAge:0})},re=3200,ne=function(e,t){return"".concat(e,".").concat(t)},ie=function(e,t){var r="".concat(t,"."),n=[];return e.forEach(function(e,t){if(t.startsWith(r)){var i=t.slice(r.length);/^\d+$/.test(i)&&n.push({name:t,idx:Number(i)})}}),n.sort(function(e,t){return e.idx-t.idx}),n.map(function(e){return e.name})},se=function(e,t){void 0===e&&(e={}),void 0===t&&(t=A()?document:null);var n=r({path:"/",sameSite:"Lax",secure:A()&&"https:"===window.location.protocol,maxAge:2592e3},e);return{getItem:function(e){return t?function(e,t){var r=e.get(t);if(void 0!==r)return r;var n=ie(e,t);return 0===n.length?null:n.map(function(t){return e.get(t)}).join("")}(X(t.cookie),e):null},setItem:function(e,r){if(t){var i=X(t.cookie);if(r.length<=re){for(var s=0,o=ie(i,e);s<o.length;s++){var a=o[s];t.cookie=te(a,n)}t.cookie=ee(e,r,n)}else{i.has(e)&&(t.cookie=te(e,n));for(var u=Math.ceil(r.length/re),c=0,l=ie(i,e);c<l.length;c++){var h=l[c];Number(h.slice(e.length+1))>=u&&(t.cookie=te(h,n))}for(var d=0,f=0;d<r.length;d+=re,f++){var v=r.slice(d,d+re);t.cookie=ee(ne(e,f),v,n)}}}},removeItem:function(e){if(t){var r=X(t.cookie);t.cookie=te(e,n);for(var i=0,s=ie(r,e);i<s.length;i++){var o=s[i];t.cookie=te(o,n)}}}}},oe={getItem:function(e){return E()?globalThis.localStorage.getItem(e):null},setItem:function(e,t){E()&&globalThis.localStorage.setItem(e,t)},removeItem:function(e){E()&&globalThis.localStorage.removeItem(e)}};function ae(e){void 0===e&&(e="");var t={},r=new URL(e);if(r.hash&&"#"===r.hash[0])try{new URLSearchParams(r.hash.substring(1)).forEach(function(e,r){t[r]=e})}catch(e){}return r.searchParams.forEach(function(e,r){t[r]=e}),t}var ue=function(e){void 0===e&&(e=[]);var t=new URL(window.location.href);e.forEach(function(e){t.searchParams.delete(e)}),t.hash="",window.history.replaceState(window.history.state,"",t.toString())};globalThis&&E()&&globalThis.localStorage&&globalThis.localStorage.getItem("faable.auth.locks.debug");var ce=function(e){function r(t){var r=e.call(this,t)||this;return r.isAcquireTimeout=!0,r}return t(r,e),r}(Error);function le(e,t,r){return n(this,void 0,void 0,function(){return i(this,function(e){switch(e.label){case 0:return[4,r()];case 1:return[2,e.sent()]}})})}!function(e){function r(){return null!==e&&e.apply(this,arguments)||this}t(r,e)}(ce);var he=function(e){function r(t){var r=e.call(this,{debug:t.debug})||this;return r.lockAcquired=!1,r.pendingInLock=[],r.lock=t.lock||le,r.storageKey=t.storageKey,r}return t(r,e),r.prototype._acquireLock=function(e,t){return n(this,void 0,void 0,function(){var r,o,a=this;return i(this,function(u){switch(u.label){case 0:this._debug("#_acquireLock","begin",e),u.label=1;case 1:return u.trys.push([1,,3,4]),this.lockAcquired?(r=this.pendingInLock.length?this.pendingInLock[this.pendingInLock.length-1]:Promise.resolve(),o=n(a,void 0,void 0,function(){return i(this,function(e){switch(e.label){case 0:return[4,r];case 1:return e.sent(),[4,t()];case 2:return[2,e.sent()]}})}),this.pendingInLock.push(n(a,void 0,void 0,function(){return i(this,function(e){switch(e.label){case 0:return e.trys.push([0,2,,3]),[4,o];case 1:case 2:return e.sent(),[3,3];case 3:return[2]}})})),[2,o]):[4,this.lock("lock:".concat(this.storageKey),e,function(){return n(a,void 0,void 0,function(){var e,r,o=this;return i(this,function(a){switch(a.label){case 0:this._debug("#_acquireLock","lock acquired for storage key",this.storageKey),a.label=1;case 1:return a.trys.push([1,,7,8]),this.lockAcquired=!0,e=t(),this.pendingInLock.push(n(o,void 0,void 0,function(){return i(this,function(t){switch(t.label){case 0:return t.trys.push([0,2,,3]),[4,e];case 1:case 2:return t.sent(),[3,3];case 3:return[2]}})})),[4,e];case 2:a.sent(),a.label=3;case 3:return this.pendingInLock.length?(r=s([],this.pendingInLock,!0),[4,Promise.all(r)]):[3,5];case 4:return a.sent(),this.pendingInLock.splice(0,r.length),[3,3];case 5:return[4,e];case 6:return[2,a.sent()];case 7:return this._debug("#_acquireLock","lock released for storage key",this.storageKey),this.lockAcquired=!1,[7];case 8:return[2]}})})})];case 2:return[2,u.sent()];case 3:return this._debug("#_acquireLock","end"),[7];case 4:return[2]}})})},r}(u),de=3e4,fe=function(e){function o(t){var r,n,i=this,o=(null==t?void 0:t.debug)||!1;if((i=e.call(this,{debug:o})||this).initializePromise=null,i._lastInitializeResult=null,i.detectSessionInUrl=!0,i.autoRefreshTicker=null,i.visibilityChangedCallback=null,i.refreshingDeferred=null,i._session=null,i.sessionCheckExpiryDays=1,i.redirectUri=(null==t?void 0:t.redirectUri)||"",!(null==t?void 0:t.domain))throw new Error("Missing domain");if(i.domainUrl=function(e){var t=(null!=e?e:"").trim().replace(/\/+$/,""),r=t.match(/^(https?):\/\/(.*)$/i);if(r){var n=r[2].replace(/^(https?:\/\/)+/i,"");return"".concat(r[1].toLowerCase(),"://").concat(n)}var i="undefined"!=typeof location&&(null===location||void 0===location?void 0:location.protocol)?location.protocol.replace(/:$/,""):"https";return"".concat(i,"://").concat(t)}(t.domain),i.tokenIssuer=(n=i.domainUrl,"".concat(n)),!t.clientId)throw new Error("Missing clientId");i.clientId=t.clientId,i.audience=t.audience,i.api=new _(i.domainUrl,{debug:o});var a=(null==t?void 0:t.storageKey)||D;return i.storageKey="".concat(a,"-").concat(i.clientId),i.storage=function(e){var t=e.storage,r=e.cookieOptions;return"cookie"===t||r?se(r):"localStorage"===t||void 0===t?oe:t}(t),i.lock=new he({lock:t.lock,storageKey:i.storageKey,debug:t.debug}),i.broadcastSync=new O(A()?i.storageKey:"",function(e){for(var t=[],r=1;r<arguments.length;r++)t[r-1]=arguments[r];return i._debug.apply(i,s([e],t,!1))}),i.flowType=null!==(r=t.flowType)&&void 0!==r?r:A()?"pkce":"implicit",i.autoRefreshToken=!0,i.initialize(),i}return t(o,e),Object.defineProperty(o.prototype,"session",{get:function(){return this._session},enumerable:!1,configurable:!0}),o.prototype.initialize=function(){return n(this,void 0,void 0,function(){var e,t=this;return i(this,function(r){switch(r.label){case 0:return this.initializePromise?[4,this.initializePromise]:[3,2];case 1:return[2,r.sent()];case 2:return this.initializePromise=n(t,void 0,void 0,function(){var e=this;return i(this,function(t){switch(t.label){case 0:return[4,this.lock._acquireLock(-1,function(){return n(e,void 0,void 0,function(){return i(this,function(e){switch(e.label){case 0:return[4,this._initialize()];case 1:return[2,e.sent()]}})})})];case 1:return[2,t.sent()]}})}),[4,this.initializePromise];case 3:return e=r.sent(),this._lastInitializeResult=e,[2,e]}})})},Object.defineProperty(o.prototype,"lastInitializeResult",{get:function(){return this._lastInitializeResult},enumerable:!1,configurable:!0}),o.prototype.handleRedirectCallback=function(){return n(this,void 0,void 0,function(){return i(this,function(e){switch(e.label){case 0:return[4,this.initialize()];case 1:return[2,e.sent()]}})})},o.prototype._initialize=function(){return n(this,void 0,void 0,function(){var e,t,r,s,o,a,u,c,l,h=this;return i(this,function(d){switch(d.label){case 0:return d.trys.push([0,8,9,11]),[4,this._detectFlowType()];case 1:return e=d.sent(),this._debug("#_initialize()","begin","flow_type",e),e?[4,this._getSessionFromURL(e)]:[3,6];case 2:return t=d.sent(),r=t.data,(s=t.error)?(this._debug("#_initialize()","error detecting session from URL",s),"Identity is already linked"===(null==s?void 0:s.message)||"Identity is already linked to another user"===(null==s?void 0:s.message)?[2,{error:s}]:[4,this._removeSession()]):[3,4];case 3:return d.sent(),[2,{error:s}];case 4:return o=r.session,a=r.redirectType,u=r.returnTo,c=r.is_new_user,this._debug("#_initialize()","detected session in URL",o,"redirect type",a),[4,this._saveSession(o)];case 5:return d.sent(),setTimeout(function(){return n(h,void 0,void 0,function(){return i(this,function(e){switch(e.label){case 0:return"recovery"!==a?[3,2]:[4,this._notifyAllSubscribers("PASSWORD_RECOVERY",o)];case 1:return e.sent(),[3,4];case 2:return[4,this._notifyAllSubscribers("SIGNED_IN",o)];case 3:e.sent(),e.label=4;case 4:return[2]}})})},0),[2,{error:null,redirectType:a,returnTo:u,is_new_user:c}];case 6:return[4,this._recoverAndRefresh()];case 7:return d.sent(),[2,{error:null}];case 8:return j(l=d.sent())?[2,{error:l}]:[2,{error:new V("Unexpected error during initialization",l)}];case 9:return[4,this._handleVisibilityChange()];case 10:return d.sent(),this._debug("#_initialize()","end"),[7];case 11:return[2]}})})},o.prototype._getSessionFromURL=function(e){return n(this,void 0,void 0,function(){var t,r,n,s,o,a,u,l,h,d,f,v,p,b,_,g,y,w,m,k,S;return i(this,function(i){switch(i.label){case 0:if(i.trys.push([0,4,,5]),t=ae(null==c?void 0:c.location.href),r="true"===t.signup,"pkce"!=e)return[3,2];if(!t.code)throw new F("No code detected.");return[4,this._exchangeCodeForSession(t.code)];case 1:if(n=i.sent(),s=n.data,o=n.error)throw o;return ue(["code","signup"]),[2,{data:{session:s.session,redirectType:s.redirectType,returnTo:s.returnTo,is_new_user:r},error:null}];case 2:if(t.error||t.error_description||t.error_code)throw new M(t.error_description||"Error in URL with unspecified error_description",{error:t.error||"unspecified_error",code:t.error_code||"unspecified_code"});if(a=t.provider_token,u=t.provider_refresh_token,l=t.access_token,h=t.refresh_token,d=t.expires_in,f=t.expires_at,v=t.token_type,!l||!h)throw new M("No session defined in URL");return p=void 0,b=void 0,d?(k=function(e){var t=e.expires_in,r=e.expires_at,n=e.refreshTick,i=Math.round(Date.now()/1e3),s=parseInt(t),o=i+s;r&&(o=parseInt(r));var a=o-i;1e3*a<=n&&console.warn("@faable/auth-js: Session as retrieved from URL expires in ".concat(a,"s, should have been closer to ").concat(s,"s"));var u=o-s;return i-u>=120?console.warn("@faable/auth-js: Session as retrieved from URL was issued over 120s ago, URL could be stale",u,o,i):i-u<0&&console.warn("@faable/auth-js: Session as retrieved from URL was issued in the future? Check the device clok for skew",u,o,i),{expiresIn:s,expiresAt:o}}({expires_in:d,expires_at:f,refreshTick:de}),p=k.expiresAt,b=k.expiresIn):(S=function(e,t,r){var n;void 0===r&&(r=Date.now()/1e3);var i=$(e),s=null!=t&&""!==t?Number(t):null!==(n=i.exp)&&void 0!==n?n:r;return{expiresAt:s,expiresIn:s-r}}(l,f),p=S.expiresAt,b=S.expiresIn),[4,this._getUser(l)];case 3:if(_=i.sent(),g=_.data,(y=_.error)||!g)throw y;return w={provider_token:a,provider_refresh_token:u,access_token:l,expires_in:b,expires_at:p,refresh_token:h,token_type:v||"bearer",user:g},ue(["access_token","expires_in","refresh_token","token_type","scope","signup"]),this._debug("#_getSessionFromURL()","clearing window.location.hash"),[2,{data:{session:w,redirectType:t.type,returnTo:null,is_new_user:r},error:null}];case 4:if(m=i.sent(),this._debug(m),j(m))return[2,{data:{session:null,redirectType:null,returnTo:null,is_new_user:!1},error:m}];throw m;case 5:return[2]}})})},o.prototype._exchangeCodeForSession=function(e){return n(this,void 0,void 0,function(){var t,n,s,o,a,u,c,l,h,d,f,v,b,_;return i(this,function(i){switch(i.label){case 0:return[4,k(this.storage,"".concat(this.storageKey,"-code-verifier"))];case 1:return(t=i.sent())?(n=t.verifier,s=t.redirectType,o=void 0===s?null:s,a=t.returnTo,u=void 0===a?null:a,[4,p("".concat(this.domainUrl,"/oauth/token"),r({client_id:this.clientId,grant_type:"authorization_code",code:e,code_verifier:n},this.audience?{audience:this.audience}:{}))]):[2,{data:{user:null,session:null,redirectType:null,returnTo:null},error:new F("No active PKCE code verifier — the authorization flow has expired or was not started")}];case 2:if(c=i.sent(),l=C(c),h=l.data,d=l.error,!h)throw new Error("Missing data");return[4,this.storage.removeItem("".concat(this.storageKey,"-code-verifier"))];case 3:return i.sent(),d?[2,{data:{user:null,session:null,redirectType:null,returnTo:null},error:d}]:h&&h.session&&h.user?(f=h.session)?[4,this._getUser(f.access_token)]:[3,7]:[2,{data:{user:null,session:null,redirectType:null,returnTo:null},error:new W}];case 4:if(v=i.sent(),b=v.data,(_=v.error)||!b)throw _;return f=r(r({},f),{user:b}),h.session=f,[4,this._saveSession(f)];case 5:return i.sent(),[4,this._notifyAllSubscribers("SIGNED_IN",f)];case 6:i.sent(),i.label=7;case 7:return[2,{data:r(r({},h),{redirectType:null!=o?o:null,returnTo:null!=u?u:null}),error:d}]}})})},o.prototype._handleVisibilityChange=function(){return n(this,void 0,void 0,function(){var e,t=this;return i(this,function(r){switch(r.label){case 0:if(this._debug("#_handleVisibilityChange()"),!A()||!(null==c?void 0:c.addEventListener))return this.autoRefreshToken&&this.startAutoRefresh(),[2,!1];r.label=1;case 1:return r.trys.push([1,3,,4]),this.visibilityChangedCallback=function(){return n(t,void 0,void 0,function(){return i(this,function(e){switch(e.label){case 0:return[4,this._onVisibilityChanged(!1)];case 1:return[2,e.sent()]}})})},null==c||c.addEventListener("visibilitychange",this.visibilityChangedCallback),[4,this._onVisibilityChanged(!0)];case 2:return r.sent(),[3,4];case 3:return e=r.sent(),console.error("_handleVisibilityChange",e),[3,4];case 4:return[2]}})})},o.prototype._onVisibilityChanged=function(e){return n(this,void 0,void 0,function(){var t,r=this;return i(this,function(s){switch(s.label){case 0:return t="#_onVisibilityChanged(".concat(e,")"),this._debug(t,"visibilityState",h.visibilityState),"visible"!==h.visibilityState?[3,4]:(this.autoRefreshToken&&this._startAutoRefresh(),e?[3,3]:[4,this.initializePromise]);case 1:return s.sent(),[4,this.lock._acquireLock(-1,function(){return n(r,void 0,void 0,function(){return i(this,function(e){switch(e.label){case 0:return"visible"!==h.visibilityState?(this._debug(t,"acquired the lock to recover the session, but the browser visibilityState is no longer visible, aborting"),[2]):[4,this._recoverAndRefresh()];case 1:return e.sent(),[2]}})})})];case 2:s.sent(),s.label=3;case 3:return[3,5];case 4:"hidden"===h.visibilityState&&this.autoRefreshToken&&this._stopAutoRefresh(),s.label=5;case 5:return[2]}})})},o.prototype._recoverAndRefresh=function(){return n(this,void 0,void 0,function(){var e,t,r,n,s,o,a;return i(this,function(i){switch(i.label){case 0:e="#_recoverAndRefresh()",this._debug(e,"begin"),i.label=1;case 1:return i.trys.push([1,12,13,14]),[4,w(this.storage,this.storageKey)];case 2:return t=i.sent(),this._debug(e,"session from storage",t),Q(t)?[3,5]:(this._debug(e,"session is not valid"),null===t?[3,4]:[4,this._removeSession()]);case 3:i.sent(),i.label=4;case 4:return[2];case 5:return r=Math.round(Date.now()/1e3),n=(null!==(a=t.expires_at)&&void 0!==a?a:1/0)<r+10,this._debug(e,"session has".concat(n?"":" not"," expired with margin of ").concat(10,"s")),n?this.autoRefreshToken&&t.refresh_token?[4,this._callRefreshToken(t.refresh_token)]:[3,8]:[3,9];case 6:return(s=i.sent().error)?(console.error(s),G(s)?[3,8]:(this._debug(e,"refresh failed with a non-retryable error, removing the session",s),[4,this._removeSession()])):[3,8];case 7:i.sent(),i.label=8;case 8:return[3,11];case 9:return this._session=t,[4,this._notifyAllSubscribers("SIGNED_IN",t)];case 10:i.sent(),i.label=11;case 11:return[3,14];case 12:return o=i.sent(),this._debug(e,"error",o),console.error(o),[2];case 13:return this._debug(e,"end"),[7];case 14:return[2]}})})},o.prototype._removeVisibilityChangedCallback=function(){this._debug("#_removeVisibilityChangedCallback()");var e=this.visibilityChangedCallback;this.visibilityChangedCallback=null;try{e&&A()&&(null==c?void 0:c.removeEventListener)&&c.removeEventListener("visibilitychange",e)}catch(e){console.error("removing visibilitychange callback failed",e)}},o.prototype.startAutoRefresh=function(){return n(this,void 0,void 0,function(){return i(this,function(e){switch(e.label){case 0:return this._removeVisibilityChangedCallback(),[4,this._startAutoRefresh()];case 1:return e.sent(),[2]}})})},o.prototype._startAutoRefresh=function(){return n(this,void 0,void 0,function(){var e,t=this;return i(this,function(r){switch(r.label){case 0:return[4,this._stopAutoRefresh()];case 1:return r.sent(),this._debug("#_startAutoRefresh()"),e=setInterval(function(){return t._autoRefreshTokenTick()},de),this.autoRefreshTicker=e,e&&"object"==typeof e&&"function"==typeof e.unref?e.unref():void 0!==globalThis.Deno&&"function"==typeof globalThis.Deno.unrefTimer&&globalThis.Deno.unrefTimer(e),setTimeout(function(){return n(t,void 0,void 0,function(){return i(this,function(e){switch(e.label){case 0:return[4,this.initializePromise];case 1:return e.sent(),[4,this._autoRefreshTokenTick()];case 2:return e.sent(),[2]}})})},0),[2]}})})},o.prototype._stopAutoRefresh=function(){return n(this,void 0,void 0,function(){var e;return i(this,function(t){return this._debug("#_stopAutoRefresh()"),e=this.autoRefreshTicker,this.autoRefreshTicker=null,e&&clearInterval(e),[2]})})},o.prototype._autoRefreshTokenTick=function(){return n(this,void 0,void 0,function(){var e,t=this;return i(this,function(r){switch(r.label){case 0:this._debug("#_autoRefreshTokenTick()","begin"),r.label=1;case 1:return r.trys.push([1,3,,4]),[4,this.lock._acquireLock(0,function(){return n(t,void 0,void 0,function(){var e,t,r=this;return i(this,function(s){switch(s.label){case 0:s.trys.push([0,,5,6]),e=Date.now(),s.label=1;case 1:return s.trys.push([1,3,,4]),[4,this._useSession(function(t){return n(r,void 0,void 0,function(){var r,n;return i(this,function(i){switch(i.label){case 0:return(r=t.data.session)&&r.refresh_token&&r.expires_at?(n=Math.floor((1e3*r.expires_at-e)/de),this._debug("#_autoRefreshTokenTick()","access token expires in ".concat(n," ticks, a tick lasts ").concat(de,"ms, refresh threshold is ").concat(3," ticks")),n<=3?[4,this._callRefreshToken(r.refresh_token)]:[3,2]):(this._debug("#_autoRefreshTokenTick()","no session"),[2]);case 1:i.sent(),i.label=2;case 2:return[2]}})})})];case 2:return[2,s.sent()];case 3:return t=s.sent(),console.error("Auto refresh tick failed with error. This is likely a transient error.",t),[3,4];case 4:return[3,6];case 5:return this._debug("#_autoRefreshTokenTick()","end"),[7];case 6:return[2]}})})})];case 2:return r.sent(),[3,4];case 3:if(!((e=r.sent()).isAcquireTimeout||e instanceof ce))throw e;return this._debug("auto refresh token tick lock not available"),[3,4];case 4:return[2]}})})},o.prototype._detectFlowType=function(){return n(this,void 0,void 0,function(){var e,t;return i(this,function(r){return e=ae(null==c?void 0:c.location.href),(t=A())&&e.code?[2,"pkce"]:t&&(e.access_token||e.error_description)?[2,"implicit"]:[2,null]})})},o.prototype._scope=function(){return this.scope||"openid profile email"},o.prototype._getUrlForConnection=function(e,t){return n(this,void 0,void 0,function(){var n,s,o,a,u,l,h;return i(this,function(i){switch(i.label){case 0:return n=t.queryParams||{},s={client_id:this.clientId,response_type:t.response_type,redirect_uri:t.redirectTo||this.redirectUri||(null==c?void 0:c.location.origin),scope:t.scopes||this._scope()},"pkce"!==this.flowType?[3,2]:[4,x(this.storage,this.storageKey,!1,t.returnTo)];case 1:o=i.sent(),a=o[0],u=o[1],n=r(r({},n),{code_challenge:a,code_challenge_method:u}),i.label=2;case 2:return t.connection_id?s.connection_id=t.connection_id:t.connection&&(s.connection=t.connection),(l=null!==(h=t.audience)&&void 0!==h?h:this.audience)&&(s.audience=l),[2,"".concat(e,"?").concat(new URLSearchParams(r(r({},n),s)))]}})})},o.prototype.signInWithOauthConnection=function(e){return n(this,void 0,void 0,function(){return i(this,function(t){switch(t.label){case 0:return[4,this._handleConnectionSignIn({connection:e.connection,connection_id:e.connection_id,redirectTo:null==e?void 0:e.redirectTo,returnTo:null==e?void 0:e.returnTo,scopes:null==e?void 0:e.scopes,queryParams:e.queryParams,skipBrowserRedirect:e.skipBrowserRedirect,audience:e.audience})];case 1:return[2,t.sent()]}})})},o.prototype.signInWithUsernamePassword=function(e){return n(this,void 0,void 0,function(){var t,n,s;return i(this,function(i){switch(i.label){case 0:return t=null!==(s=e.audience)&&void 0!==s?s:this.audience,[4,p("".concat(this.domainUrl,"/usernamepassword/login"),r({username:e.username,password:e.password,redirect_uri:e.redirectTo||this.redirectUri||(null==c?void 0:c.location.origin),client_id:this.clientId,state:e.state},t?{audience:t}:{}),{raw:!0})];case 1:return!(n=i.sent()).data||n.error?[2,{data:null,error:new V(n.error||"Error in username password login",n.error)}]:(function(e,t){var r=t.createElement("div");r.innerHTML=e;var n=t.body.appendChild(r).children[0];if(!n)throw new Error("Auth response did not contain a submittable form");n.submit()}(n.data,h),[2,{data:null,error:null}])}})})},o.prototype.signUp=function(e){return n(this,void 0,void 0,function(){var t,n,s,o,a;return i(this,function(i){switch(i.label){case 0:return(null==e?void 0:e.email)&&(null==e?void 0:e.password)?[4,p("".concat(this.domainUrl,"/dbconnections/signup"),r(r(r(r(r({client_id:this.clientId,email:e.email,password:e.password},e.name?{name:e.name}:{}),e.given_name?{given_name:e.given_name}:{}),e.family_name?{family_name:e.family_name}:{}),e.user_metadata?{user_metadata:e.user_metadata}:{}),e.connection?{connection:e.connection}:{}))]:[2,{data:null,error:new V("email and password are required",null)}];case 1:return t=i.sent(),n=t.data,(s=t.error)?(o=null==n?void 0:n.status,a=403===o?"signup_disabled":409===o?"email_exists":void 0,[2,{data:null,error:new K(String(s),null!=o?o:500,a)}]):[2,this.signInWithUsernamePassword({username:e.email,password:e.password,redirectTo:e.redirectTo,state:e.state,audience:e.audience})]}})})},o.prototype.signInWithOtp=function(e){return n(this,void 0,void 0,function(){var t,n,s,o,a,u,c,l,h,d;return i(this,function(i){switch(i.label){case 0:return t=null!==(d=e.audience)&&void 0!==d?d:this.audience,[4,p("".concat(this.domainUrl,"/oauth/token"),r({client_id:this.clientId,grant_type:"http://auth0.com/oauth/grant-type/passwordless/otp",username:e.username,otp:e.otp},t?{audience:t}:{}))];case 1:return n=i.sent(),s=C(n),o=s.data,(a=s.error)?[2,{data:{user:null,session:null},error:a}]:o&&o.session?(u=o.session,[4,this._getUser(u.access_token)]):[2,{data:{user:null,session:null},error:new W}];case 2:return c=i.sent(),l=c.data,(h=c.error)||!l?[2,{data:{user:null,session:null},error:h||new V("Could not fetch user info",null)}]:(u.user=l,[4,this._saveSession(u)]);case 3:return i.sent(),[4,this._notifyAllSubscribers("SIGNED_IN",u)];case 4:return i.sent(),[2,{data:{user:u.user,session:u},error:null}]}})})},o.prototype.signInWithPasswordless=function(e){return n(this,void 0,void 0,function(){var t,n,s;return i(this,function(i){switch(i.label){case 0:return t=null!==(s=e.audience)&&void 0!==s?s:this.audience,[4,p("".concat(this.domainUrl,"/passwordless/start"),r({client_id:this.clientId,email:e.email,send:e.type},t?{audience:t}:{}))];case 1:return[2,{data:(n=i.sent()).data,error:n.error}]}})})},o.prototype.changePassword=function(e){return n(this,void 0,void 0,function(){var t,r,n;return i(this,function(i){switch(i.label){case 0:return(null==e?void 0:e.email)?[4,p("".concat(this.domainUrl,"/dbconnections/change_password"),{email:e.email})]:[2,{data:null,error:new V("email is required",null)}];case 1:return t=i.sent(),r=t.data,n=t.error,[2,{data:null!=r?r:null,error:n?new V(String(n),n):null}]}})})},o.prototype._resolveUserId=function(e){var t,r,n,i=null!==(r=null===(t=e.user)||void 0===t?void 0:t.id)&&void 0!==r?r:null===(n=e.user)||void 0===n?void 0:n.sub;if(i)return i;try{var s=$(e.access_token);return null==s?void 0:s.sub}catch(e){return}},o.prototype.changeEmail=function(e){return n(this,void 0,void 0,function(){var t,n,s,o,a,u,c,l;return i(this,function(i){switch(i.label){case 0:return(null==e?void 0:e.new_email)?[4,this.getSession()]:[2,{data:null,error:new V("new_email is required",null)}];case 1:return t=i.sent(),n=t.data,s=t.error,o=null==n?void 0:n.session,s||!o?[2,{data:null,error:s||new q}]:(a=this._resolveUserId(o))?[4,p("".concat(this.domainUrl,"/user/").concat(a,"/change-email"),r(r({new_email:e.new_email},e.verification_mode?{verification_mode:e.verification_mode}:{}),e.redirect_uri?{redirect_uri:e.redirect_uri}:{}),{token:o.access_token})]:[2,{data:null,error:new q}];case 2:return u=i.sent(),c=u.data,l=u.error,[2,{data:null!=c?c:null,error:l?new V(String(l),l):null}]}})})},o.prototype.buildAuthorizeUrl=function(e){var t;void 0===e&&(e={});var r={client_id:this.clientId,redirect_uri:e.redirectTo||this.redirectUri||(null==c?void 0:c.location.origin),response_type:g(e,A()),audience:null!==(t=e.audience)&&void 0!==t?t:this.audience,scope:e.scope,connection:e.connection},n=Object.fromEntries(Object.entries(r).filter(function(e){return!!e[1]}));return"".concat(this.domainUrl,"/authorize?").concat(new URLSearchParams(n).toString())},o.prototype.authorize=function(e){var t=this.buildAuthorizeUrl(e);B.redirect(t)},o.prototype._handleConnectionSignIn=function(e){return n(this,void 0,void 0,function(){var t;return i(this,function(r){switch(r.label){case 0:return[4,this._getUrlForConnection("".concat(this.domainUrl,"/authorize"),{response_type:A()?"code":"token",connection:e.connection,connection_id:e.connection_id,redirectTo:e.redirectTo,returnTo:e.returnTo,scopes:e.scopes,queryParams:e.queryParams,audience:e.audience})];case 1:return t=r.sent(),this._debug("#_handleConnectionSignIn()","options",e,"url",t),!A()||e.skipBrowserRedirect?[3,3]:(null==c||c.location.assign(t),[4,new Promise(function(){})]);case 2:r.sent(),r.label=3;case 3:return[2,{data:{url:t},error:null}]}})})},o.prototype.setSession=function(e){return n(this,void 0,void 0,function(){var t=this;return i(this,function(r){switch(r.label){case 0:return[4,this.initializePromise];case 1:return r.sent(),[4,this.lock._acquireLock(-1,function(){return n(t,void 0,void 0,function(){return i(this,function(t){switch(t.label){case 0:return[4,this._setSession(e)];case 1:return[2,t.sent()]}})})})];case 2:return[2,r.sent()]}})})},o.prototype.getSession=function(){return n(this,void 0,void 0,function(){var e=this;return i(this,function(t){switch(t.label){case 0:return[4,this.initializePromise];case 1:return t.sent(),[4,this.lock._acquireLock(-1,function(){return n(e,void 0,void 0,function(){var e=this;return i(this,function(t){return[2,this._useSession(function(t){return n(e,void 0,void 0,function(){return i(this,function(e){return[2,t]})})})]})})})];case 2:return[2,t.sent()]}})})},o.prototype._useSession=function(e){return n(this,void 0,void 0,function(){var t;return i(this,function(r){switch(r.label){case 0:this._debug("#_useSession","begin"),r.label=1;case 1:return r.trys.push([1,,4,5]),[4,this.__loadSession()];case 2:return t=r.sent(),[4,e(t)];case 3:return[2,r.sent()];case 4:return this._debug("#_useSession","end"),[7];case 5:return[2]}})})},o.prototype.__loadSession=function(){return n(this,void 0,void 0,function(){var e,t,r,n,s,o,a;return i(this,function(i){switch(i.label){case 0:this._debug("#__loadSession()","begin"),this.lock.lockAcquired||this._debug("#__loadSession()","used outside of an acquired lock!",(new Error).stack),i.label=1;case 1:return i.trys.push([1,,7,8]),e=null,[4,w(this.storage,this.storageKey)];case 2:return t=i.sent(),this._debug("#getSession()","session from storage",t),null===t?[3,5]:Q(t)?(e=t,[3,5]):[3,3];case 3:return this._debug("#getSession()","session from storage is not valid"),[4,this._removeSession()];case 4:i.sent(),i.label=5;case 5:return e?(r=!!e.expires_at&&e.expires_at<=Date.now()/1e3,this._debug("#__loadSession()","session has".concat(r?"":" not"," expired"),"expires_at",e.expires_at),r?[4,this._callRefreshToken(e.refresh_token)]:(this.storage.isServer&&(n=new Proxy(e,{get:function(e,t,r){return"user"===t&&console.warn("Reading `session.user` from a server-side cookie store can be insecure: the value is whatever the cookie contains and has not been verified against Faable Auth. Re-fetch the user with `auth.getUser()` (or verify the access token yourself) before trusting it on the server."),Reflect.get(e,t,r)}}),e=n),[2,{data:{session:e},error:null}])):[2,{data:{session:null},error:null}];case 6:return s=i.sent(),o=s.session,(a=s.error)?[2,{data:{session:null},error:a}]:[2,{data:{session:o},error:null}];case 7:return this._debug("#__loadSession()","end"),[7];case 8:return[2]}})})},o.prototype._removeSession=function(){return n(this,void 0,void 0,function(){return i(this,function(e){switch(e.label){case 0:return this._debug("#_removeSession()"),this._session=null,[4,this.storage.removeItem(this.storageKey)];case 1:return e.sent(),[2]}})})},o.prototype._setSession=function(e){return n(this,void 0,void 0,function(){var t,r,n,s,o,a,u,c,l,h,d;return i(this,function(i){switch(i.label){case 0:if(i.trys.push([0,7,,8]),!e.access_token||!e.refresh_token)throw new q;return t=Date.now()/1e3,r=t,n=!0,s=null,(o=$(e.access_token)).exp&&(r=o.exp,n=r<=t),n?[4,this._callRefreshToken(e.refresh_token)]:[3,2];case 1:return a=i.sent(),u=a.session,(h=a.error)?[2,{data:{user:null,session:null},error:h}]:u?(s=u,[3,6]):[2,{data:{user:null,session:null},error:null}];case 2:return[4,this._getUser(e.access_token)];case 3:if(c=i.sent(),l=c.data,(h=c.error)||!l)throw h;return s={access_token:e.access_token,refresh_token:e.refresh_token,user:l,token_type:"bearer",expires_in:r-t,expires_at:r},[4,this._saveSession(s)];case 4:return i.sent(),[4,this._notifyAllSubscribers("SIGNED_IN",s)];case 5:i.sent(),i.label=6;case 6:return[2,{data:{user:s.user,session:s},error:null}];case 7:if(j(d=i.sent()))return[2,{data:{session:null,user:null},error:d}];throw d;case 8:return[2]}})})},o.prototype._saveSession=function(e){return n(this,void 0,void 0,function(){return i(this,function(t){switch(t.label){case 0:return this._debug("#_saveSession()",e),this._session=e,[4,y(this.storage,this.storageKey,e)];case 1:return t.sent(),[2]}})})},o.prototype._getUser=function(e){return n(this,void 0,void 0,function(){var t;return i(this,function(r){switch(r.label){case 0:if(!e)throw new Error("Cannot fetch user without token");return this._debug("#_getUser() begin"),[4,b("".concat(this.domainUrl,"/me"),{token:e})];case 1:return t=r.sent(),this._debug("#_getUser() end"),[2,{data:t.data,error:t.error}]}})})},o.prototype._callRefreshToken=function(e){return n(this,void 0,void 0,function(){var t,r,n,s,o,a,u,c;return i(this,function(i){switch(i.label){case 0:if(!e)throw new q;if(this.refreshingDeferred)return[2,this.refreshingDeferred.promise];t="#_callRefreshToken(".concat(e.substring(0,5),"...)"),this._debug(t,"begin"),i.label=1;case 1:return i.trys.push([1,5,10,11]),this.refreshingDeferred=new U,[4,(l=this._refreshAccessToken(e),h=3e4,d="Token refresh timed out",v=new Promise(function(e,t){f=setTimeout(function(){return t(new Error(d))},h)}),Promise.race([l.finally(function(){void 0!==f&&clearTimeout(f)}),v]))];case 2:if(r=i.sent(),n=r.data,s=r.error)throw s;if(!n.session)throw new q;return[4,this._saveSession(n.session)];case 3:return i.sent(),[4,this._notifyAllSubscribers("TOKEN_REFRESHED",n.session)];case 4:return i.sent(),a={session:n.session,error:null},this.refreshingDeferred.resolve(a),[2,a];case 5:return o=i.sent(),this._debug(t,"error",o),j(o)?(a={session:null,error:o},G(o)?[3,8]:[4,this._removeSession()]):[3,9];case 6:return i.sent(),[4,this._notifyAllSubscribers("SIGNED_OUT",null)];case 7:i.sent(),i.label=8;case 8:return null===(u=this.refreshingDeferred)||void 0===u||u.resolve(a),[2,a];case 9:throw null===(c=this.refreshingDeferred)||void 0===c||c.reject(o),o;case 10:return this.refreshingDeferred=null,this._debug(t,"end"),[7];case 11:return[2]}var l,h,d,f,v})})},o.prototype._refreshAccessToken=function(e){return n(this,void 0,void 0,function(){var t,s,o,a=this;return i(this,function(u){switch(u.label){case 0:t="#_refreshAccessToken(".concat(e.substring(0,5),"...)"),this._debug(t,"begin"),u.label=1;case 1:return u.trys.push([1,3,4,5]),s=Date.now(),[4,P(function(s){return n(a,void 0,void 0,function(){var n,o,a,u,c,l,h;return i(this,function(i){switch(i.label){case 0:return s>0?[4,L(200*Math.pow(2,s-1))]:[3,2];case 1:i.sent(),i.label=2;case 2:return this._debug(t,"refreshing attempt",s),[4,p("".concat(this.domainUrl,"/oauth/token"),r({client_id:this.clientId,grant_type:"refresh_token",refresh_token:e},this.audience?{audience:this.audience}:{}))];case 3:if((n=i.sent()).error)throw new V("Refresh token request failed: ".concat(n.error),n.error);if(o=C(n),!(null===(l=o.data.session)||void 0===l?void 0:l.access_token))throw new W;return[4,this._getUser(null===(h=o.data.session)||void 0===h?void 0:h.access_token)];case 4:if(a=i.sent(),u=a.data,c=a.error)throw new V("Could not fetch user info",c);if(!u)throw new V("Refresh response missing user",null);return[2,{data:{session:r(r({},o.data.session),{user:u}),user:u},error:null}]}})})},function(e,t){var r=200*Math.pow(2,e);return t&&G(t)&&Date.now()+r-s<de})];case 2:return[2,u.sent()];case 3:if(o=u.sent(),this._debug(t,"error",o),j(o))return[2,{data:{session:null,user:null},error:o}];throw o;case 4:return this._debug(t,"end"),[7];case 5:return[2]}})})},o.prototype._notifyAllSubscribers=function(e,t){return n(this,arguments,void 0,function(e,t,r){var n;return void 0===r&&(r=!0),i(this,function(i){switch(i.label){case 0:n="#_notifyAllSubscribers(".concat(e,")"),this._debug(n,"begin",t,"broadcast = ".concat(r)),i.label=1;case 1:return i.trys.push([1,,3,4]),[4,this.broadcastSync.notify(e,t,r)];case 2:return i.sent(),[3,4];case 3:return this._debug(n,"end"),[7];case 4:return[2]}})})},o.prototype.signOut=function(){return n(this,arguments,void 0,function(e){var t=this;return void 0===e&&(e={scope:"global"}),i(this,function(r){switch(r.label){case 0:return[4,this.initializePromise];case 1:return r.sent(),[4,this.lock._acquireLock(-1,function(){return n(t,void 0,void 0,function(){return i(this,function(t){switch(t.label){case 0:return[4,this._signOut(e)];case 1:return[2,t.sent()]}})})})];case 2:return[2,r.sent()]}})})},o.prototype._signOut=function(){return n(this,arguments,void 0,function(e){var t=this,r=(void 0===e?{scope:"global"}:e).scope;return i(this,function(e){switch(e.label){case 0:return[4,this._useSession(function(e){return n(t,void 0,void 0,function(){var t,n,s,o;return i(this,function(i){switch(i.label){case 0:return t=e.data,(n=e.error)?[2,{error:n}]:(null===(o=t.session)||void 0===o?void 0:o.access_token)?[4,this.api.signOut({client_id:this.clientId})]:[3,2];case 1:if((s=i.sent().error)&&(!function(e){return j(e)&&"AuthApiError"===e.name}(s)||404!==s.status&&401!==s.status))return[2,{error:s}];i.label=2;case 2:return"others"===r?[3,6]:[4,this._removeSession()];case 3:return i.sent(),[4,this.storage.removeItem("".concat(this.storageKey,"-code-verifier"))];case 4:return i.sent(),[4,this._notifyAllSubscribers("SIGNED_OUT",null)];case 5:i.sent(),i.label=6;case 6:return[2,{error:null}]}})})})];case 1:return[2,e.sent()]}})})},o.prototype.onAuthStateChange=function(e){var t=this,r=this.broadcastSync.subscribe(e).subscription;return this._debug("#onAuthStateChange()","registered callback with id",r.id),n(t,void 0,void 0,function(){var e=this;return i(this,function(t){switch(t.label){case 0:return[4,this.initializePromise];case 1:return t.sent(),[4,this.lock._acquireLock(-1,function(){return n(e,void 0,void 0,function(){return i(this,function(e){return this._emitInitialSession(r),[2]})})})];case 2:return t.sent(),[2]}})}),{data:{subscription:r}}},o.prototype._emitInitialSession=function(e){return n(this,void 0,void 0,function(){var t=this;return i(this,function(r){switch(r.label){case 0:return[4,this._useSession(function(r){return n(t,void 0,void 0,function(){var t,n,s;return i(this,function(i){switch(i.label){case 0:if(i.trys.push([0,2,,4]),t=r.data.session,n=r.error)throw n;return[4,e.callback("INITIAL_SESSION",t)];case 1:return i.sent(),this._debug("INITIAL_SESSION","callback id",e.id,"session",t),[3,4];case 2:return s=i.sent(),[4,e.callback("INITIAL_SESSION",null)];case 3:return i.sent(),this._debug("INITIAL_SESSION","callback id",e.id,"error",s),console.error(s),[3,4];case 4:return[2]}})})})];case 1:return[2,r.sent()]}})})},o.prototype.refreshSession=function(e){return n(this,void 0,void 0,function(){var t=this;return i(this,function(r){switch(r.label){case 0:return[4,this.initializePromise];case 1:return r.sent(),[4,this.lock._acquireLock(-1,function(){return n(t,void 0,void 0,function(){return i(this,function(t){switch(t.label){case 0:return[4,this._refreshSession(e)];case 1:return[2,t.sent()]}})})})];case 2:return[2,r.sent()]}})})},o.prototype._refreshSession=function(e){return n(this,void 0,void 0,function(){var t,r=this;return i(this,function(s){switch(s.label){case 0:return s.trys.push([0,2,,3]),[4,this._useSession(function(t){return n(r,void 0,void 0,function(){var r,n,s,o,a,u;return i(this,function(i){switch(i.label){case 0:if(!e){if(r=t.data,n=t.error)throw n;e=null!==(u=r.session)&&void 0!==u?u:void 0}if(!(null==e?void 0:e.refresh_token))throw new q;return[4,this._callRefreshToken(e.refresh_token)];case 1:return s=i.sent(),o=s.session,(a=s.error)?[2,{data:{user:null,session:null},error:a}]:o?[2,{data:{user:o.user,session:o},error:null}]:[2,{data:{user:null,session:null},error:null}]}})})})];case 1:return[2,s.sent()];case 2:if(j(t=s.sent()))return[2,{data:{user:null,session:null},error:t}];throw t;case 3:return[2]}})})},o}(u),ve=function(e){return new fe(e)},pe=function(){};function be(e){if(e.error)throw e.error;return e.data}export{z as AuthError,fe as FaableAuthClient,pe as User,se as cookieStorageAdapter,ve as createClient,H as getSessionFromCookies,be as unwrap};
|
|
7
7
|
//# sourceMappingURL=main.js.map
|