@faable/auth-js 1.5.6 → 1.6.0-next.1

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.
@@ -2,6 +2,9 @@ import { SupportedStorage, SignInWithOAuthConnection, OAuthResponse, Subscriptio
2
2
  import { FaableAuthClientConfig, AuthResponse, AuthChangeEvent } from "./lib/types";
3
3
  import { Session, SignOut } from "./lib/types";
4
4
  import { Deferred } from "./lib/helpers";
5
+ import { cookieStorageAdapter } from "./lib/storage/cookie-storage";
6
+ import { getSessionFromCookies } from "./lib/nextjs";
7
+ export { cookieStorageAdapter, getSessionFromCookies };
5
8
  import { AuthError } from "./lib/errors";
6
9
  import FaableAuthApi from "./FaableAuthApi";
7
10
  import { Base } from "./Base";
@@ -27,8 +30,13 @@ export declare class FaableAuthClient extends Base {
27
30
  */
28
31
  protected broadcastChannel: BroadcastChannel | null;
29
32
  protected stateChangeEmitters: Map<string, Subscription>;
33
+ protected _session: Session | null;
30
34
  protected lock: Lock;
31
35
  constructor(config: FaableAuthClientConfig);
36
+ /**
37
+ * Returns the current session, if any.
38
+ */
39
+ get session(): Session | null;
32
40
  /**
33
41
  * Initializes the client session either from the url or from storage.
34
42
  * This method is automatically called when instantiating the client, but should also be called
@@ -116,10 +124,30 @@ export declare class FaableAuthClient extends Base {
116
124
  redirect_uri?: string;
117
125
  state?: string;
118
126
  }): Promise<void>;
127
+ /**
128
+ * Completes a passwordless login using an OTP code.
129
+ * @param data The username and OTP code.
130
+ */
131
+ signInWithOtp(data: {
132
+ username: string;
133
+ otp: string;
134
+ }): Promise<AuthResponse>;
135
+ /**
136
+ * Starts a passwordless login flow by requesting an OTP code or a magic link.
137
+ * @param data The email and the type of delivery (code or link).
138
+ */
139
+ signInWithPasswordless(data: {
140
+ email: string;
141
+ type: "code" | "link";
142
+ }): Promise<{
143
+ data: any;
144
+ error: AuthError | null;
145
+ }>;
119
146
  changePassword(params: {
120
147
  email: string;
121
148
  }): Promise<unknown>;
122
149
  buildAuthorizeUrl(options?: {
150
+ connection?: string;
123
151
  redirectTo?: string;
124
152
  scope?: string;
125
153
  response_type?: string;
@@ -0,0 +1,6 @@
1
+ import { Session } from "./types";
2
+ /**
3
+ * Helper for Next.js to get the session from the server-side cookies.
4
+ * This can be used in API routes or Server Components.
5
+ */
6
+ export declare const getSessionFromCookies: (cookiesStore: any, storageKey: string) => Session | null;
@@ -0,0 +1,14 @@
1
+ import { SupportedStorage } from "../types";
2
+ interface CookieOptions {
3
+ domain?: string;
4
+ path?: string;
5
+ sameSite?: "Lax" | "Strict" | "None";
6
+ secure?: boolean;
7
+ maxAge?: number;
8
+ }
9
+ /**
10
+ * A storage adapter that uses document.cookie to persist data.
11
+ * This is useful for SSR as cookies are sent to the server on every request.
12
+ */
13
+ export declare const cookieStorageAdapter: (options?: CookieOptions) => SupportedStorage;
14
+ export {};
@@ -1,4 +1,4 @@
1
- import { SupportedStorage } from "./types";
1
+ import { SupportedStorage } from "../types";
2
2
  /**
3
3
  * Provides safe access to the globalThis.localStorage property.
4
4
  */
@@ -1,4 +1,3 @@
1
1
  import { SupportedStorage } from "./types";
2
2
  export declare const setItemAsync: (storage: SupportedStorage, key: string, data: any) => Promise<void>;
3
3
  export declare const getItemAsync: (storage: SupportedStorage, key: string) => Promise<unknown>;
4
- export declare const removeItemAsync: (storage: SupportedStorage, key: string) => Promise<void>;
@@ -227,6 +227,28 @@ export type TokenEndpointResponse = {
227
227
  expires_in: number;
228
228
  scope?: string;
229
229
  };
230
+ export interface CookieOptions {
231
+ /**
232
+ * (Optional) The domain of the cookie.
233
+ */
234
+ domain?: string;
235
+ /**
236
+ * (Optional) The path of the cookie.
237
+ */
238
+ path?: string;
239
+ /**
240
+ * (Optional) The same-site attribute of the cookie.
241
+ */
242
+ sameSite?: "Lax" | "Strict" | "None";
243
+ /**
244
+ * (Optional) Whether the cookie should only be sent over HTTPS.
245
+ */
246
+ secure?: boolean;
247
+ /**
248
+ * (Optional) The maximum age of the cookie in seconds.
249
+ */
250
+ maxAge?: number;
251
+ }
230
252
  export type FaableAuthClientConfig = {
231
253
  domain: string;
232
254
  clientId: string;
@@ -239,6 +261,10 @@ export type FaableAuthClientConfig = {
239
261
  flowType?: AuthFlowType;
240
262
  storage?: SupportedStorage;
241
263
  storageKey?: string;
264
+ /**
265
+ * (Optional) Options for the cookie storage.
266
+ */
267
+ cookieOptions?: CookieOptions;
242
268
  /**
243
269
  * Provide your own locking mechanism based on the environment. By default no locking is done at this time.
244
270
  *
@@ -1,7 +1,7 @@
1
1
  /*!
2
- * @faable/auth-js v1.5.6
3
- * (c) 2025
2
+ * @faable/auth-js v1.6.0-next.1
3
+ * (c) 2026
4
4
  * @license SEE LICENSE.md
5
5
  */
6
- !function(e){"use strict";var t=function(e,r){return t=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])},t(e,r)};function r(e,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=e}t(e,r),e.prototype=null===r?Object.create(r):(n.prototype=r.prototype,new n)}var n=function(){return n=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},n.apply(this,arguments)};function i(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 s(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 o(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 a=function(e,t,r){return i(void 0,void 0,void 0,(function(){return s(this,(function(n){switch(n.label){case 0:return[4,e.setItem(t,JSON.stringify(r))];case 1:return n.sent(),[2]}}))}))},u=function(e,t){return i(void 0,void 0,void 0,(function(){var r;return s(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]}}))}))},c=function(e,t){return i(void 0,void 0,void 0,(function(){return s(this,(function(r){switch(r.label){case 0:return[4,e.removeItem(t)];case 1:return r.sent(),[2]}}))}))};function l(e){return("0"+e.toString(16)).substr(-2)}function h(e){return i(this,void 0,void 0,(function(){var t,r,n,i;return s(this,(function(s){switch(s.label){case 0:return t=new TextEncoder,r=t.encode(e),[4,crypto.subtle.digest("SHA-256",r)];case 1:return n=s.sent(),i=new Uint8Array(n),[2,Array.from(i).map((function(e){return String.fromCharCode(e)})).join("")]}}))}))}function d(e){return i(this,void 0,void 0,(function(){var t;return s(this,(function(r){switch(r.label){case 0:return"undefined"!=typeof crypto&&void 0!==crypto.subtle&&"undefined"!=typeof TextEncoder?[4,h(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 f(e,t){return i(this,arguments,void 0,(function(e,t,r){var n,i,o;return void 0===r&&(r=!1),s(this,(function(s){switch(s.label){case 0:return n=function(){var e=new Uint32Array(56);if("undefined"==typeof crypto){for(var t="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-._~",r="",n=0;n<56;n++)r+=t.charAt(Math.floor(66*Math.random()));return r}return crypto.getRandomValues(e),Array.from(e,l).join("")}(),i=n,r&&(i+="/PASSWORD_RECOVERY"),[4,a(e,"".concat(t,"-code-verifier"),i)];case 1:return s.sent(),[4,d(n)];case 2:return[2,[o=s.sent(),n===o?"plain":"S256"]]}}))}))}var v=function(){return"undefined"!=typeof document},p={tested:!1,writable:!1},b=function(){if(!v())return!1;try{if("object"!=typeof globalThis.localStorage)return!1}catch(e){return!1}if(p.tested)return p.writable;var e="lswt-".concat(Math.random()).concat(Math.random());try{globalThis.localStorage.setItem(e,e),globalThis.localStorage.removeItem(e),p.tested=!0,p.writable=!0}catch(e){p.tested=!0,p.writable=!1}return p.writable};function g(e){var t,r,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=n({},i),!i.expires_at&&i.expires_in&&(s.expires_at=(r=i.expires_in,Math.round(Date.now()/1e3)+r))),{data:{session:s,user:null!==(t=i.user)&&void 0!==t?t:i},error:null}}var _=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 y(e,t){var r=this;return new Promise((function(n,o){i(r,void 0,void 0,(function(){var r,i,a;return s(this,(function(s){switch(s.label){case 0:r=0,s.label=1;case 1:if(!(r<1/0))return[3,6];s.label=2;case 2:return s.trys.push([2,4,,5]),[4,e(r)];case 3:return i=s.sent(),t(r,null,i)?[3,5]:(n(i),[2]);case 4:return a=s.sent(),t(r,a)?[3,5]:(o(a),[2]);case 5:return r++,[3,1];case 6:return[2]}}))}))}))}function w(e){return i(this,void 0,void 0,(function(){return s(this,(function(t){return[2,new Promise((function(t){setTimeout((function(){return t(null)}),e)}))]}))}))}function k(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 m={getItem:function(e){return b()?globalThis.localStorage.getItem(e):null},setItem:function(e,t){b()&&globalThis.localStorage.setItem(e,t)},removeItem:function(e){b()&&globalThis.localStorage.removeItem(e)}},S=function(e){function t(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 r(t,e),t}(Error),T=function(e){function t(t,r,n,i){var s=e.call(this,t,n,i)||this;return s.name=r,s.status=n,s}return r(t,e),t}(S),x=function(e){function t(){return e.call(this,"Auth session missing!","AuthSessionMissingError",400,void 0)||this}return r(t,e),t}(T);function I(e){return"object"==typeof e&&null!==e&&"__isAuthError"in e}!function(e){function t(t,r,n){var i=e.call(this,t,r,n)||this;return i.name="AuthApiError",i.status=r,i.code=n,i}r(t,e)}(S);var A=function(e){function t(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 r(t,e),t.prototype.toJSON=function(){return{name:this.name,message:this.message,status:this.status,details:this.details}},t}(T),E=function(e){function t(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 r(t,e),t.prototype.toJSON=function(){return{name:this.name,message:this.message,status:this.status,details:this.details}},t}(T),C=function(e){function t(t,r){var n=e.call(this,t)||this;return n.name="AuthUnknownError",n.originalError=r,n}return r(t,e),t}(S),R=function(e){function t(){return e.call(this,"Auth session or user missing","AuthInvalidTokenResponseError",500,void 0)||this}return r(t,e),t}(T);function U(e){return I(e)&&"AuthRetryableFetchError"===e.name}!function(e){function t(t,r){return e.call(this,t,"AuthRetryableFetchError",r,void 0)||this}r(t,e)}(T);var L=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,o(["FaableAuth@".concat(r," (").concat("0.0.0",") ").concat((new Date).toISOString())],e,!1))}return this},e}(),P="undefined"!=typeof window?window:void 0,O="undefined"!=typeof globalThis?globalThis:P,D=null==O?void 0:O.document,q=O.fetch,z=function(e){void 0===e&&(e={});var t={};return(null==e?void 0:e.token)&&(t=n(n({},t),{Authorization:"Bearer ".concat(null==e?void 0:e.token)})),n(n({},null==e?void 0:e.headers),t)},N=function(e){for(var t=[],r=1;r<arguments.length;r++)t[r-1]=arguments[r];return i(void 0,o([e],t,!0),void 0,(function(e,t){var r,n,i;return void 0===t&&(t={}),s(this,(function(s){switch(s.label){case 0:return t.raw?[4,e.text()]:[3,2];case 1:return n=s.sent(),[3,4];case 2:return[4,e.json()];case 3:n=s.sent(),s.label=4;case 4:return r=n,e.status>=300?[2,{data:r,error:t.raw?null===(i=JSON.parse(r))||void 0===i?void 0:i.message:null==r?void 0:r.message}]:[2,{data:r,error:null}]}}))}))},j=function(e,t){for(var r=[],a=2;a<arguments.length;a++)r[a-2]=arguments[a];return i(void 0,o([e,t],r,!0),void 0,(function(e,t,r){var i;return void 0===r&&(r={}),s(this,(function(s){switch(s.label){case 0:return s.trys.push([0,2,,3]),[4,q(e,{method:"POST",body:JSON.stringify(t),headers:n(n({},z(r)),{"Content-Type":"application/json"})})];case 1:return i=s.sent(),[2,N(i,r)];case 2:return[2,{data:null,error:s.sent()}];case 3:return[2]}}))}))},K=function(e){for(var t=[],r=1;r<arguments.length;r++)t[r-1]=arguments[r];return i(void 0,o([e],t,!0),void 0,(function(e,t){var r;return void 0===t&&(t={}),s(this,(function(i){switch(i.label){case 0:return i.trys.push([0,2,,3]),[4,q(e,n(n({},t),{method:"GET",headers:z(t)}))];case 1:return r=i.sent(),[2,N(r,t)];case 2:return[2,{data:null,error:i.sent()}];case 3:return[2]}}))}))},M=function(e){function t(t,r){var n=e.call(this,r)||this;return n.base_url=t,n}return r(t,e),t.prototype.extraPrint=function(){return"api"},t.prototype.signOut=function(e){return i(this,void 0,void 0,(function(){var t,r;return s(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,K(t)];case 1:return r=n.sent(),this._debug(r),r.error?[2,{error:r.error,data:null}]:[2,{error:null,data:null}]}}))}))},t}(L);globalThis&&b()&&globalThis.localStorage&&globalThis.localStorage.getItem("supabase.gotrue-js.locks.debug");var V=function(e){function t(t){var r=e.call(this,t)||this;return r.isAcquireTimeout=!0,r}return r(t,e),t}(Error);function F(e,t,r){return i(this,void 0,void 0,(function(){return s(this,(function(e){switch(e.label){case 0:return[4,r()];case 1:return[2,e.sent()]}}))}))}!function(e){function t(){return null!==e&&e.apply(this,arguments)||this}r(t,e)}(V);var G=function(e){function t(r){void 0===r&&(r={});var n=e.call(this,r)||this;return n.instanceID=t.nextInstanceID,t.nextInstanceID+=1,n}return r(t,e),t.prototype.extraPrint=function(){return this.instanceID.toString()},t.nextInstanceID=0,t}(L),J=function(e){function t(t){var r=e.call(this,{debug:t.debug})||this;return r.lockAcquired=!1,r.pendingInLock=[],r.lock=t.lock||F,r.storageKey=t.storageKey,r}return r(t,e),t.prototype._acquireLock=function(e,t){return i(this,void 0,void 0,(function(){var r,n,a=this;return s(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(),n=i(a,void 0,void 0,(function(){return s(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(i(a,void 0,void 0,(function(){return s(this,(function(e){switch(e.label){case 0:return e.trys.push([0,2,,3]),[4,n];case 1:case 2:return e.sent(),[3,3];case 3:return[2]}}))}))),[2,n]):[4,this.lock("lock:".concat(this.storageKey),e,(function(){return i(a,void 0,void 0,(function(){var e,r,n=this;return s(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(i(n,void 0,void 0,(function(){return s(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=o([],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]}}))}))},t}(G);function B(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 W=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())};function H(){if(!P)throw new Error("No window in environment");return P}var $={redirect:function(e){H().location=e},getDocument:function(){return H().document},getWindow:H},Y=3e4,Q=function(e){function t(t){var r,n,o=this,a=(null==t?void 0:t.debug)||!1;if((o=e.call(this,{debug:a})||this).initializePromise=null,o.detectSessionInUrl=!0,o.autoRefreshTicker=null,o.visibilityChangedCallback=null,o.refreshingDeferred=null,o.broadcastChannel=null,o.stateChangeEmitters=new Map,o.sessionCheckExpiryDays=1,o.redirect_uri=(null==t?void 0:t.redirect_uri)||"",!(null==t?void 0:t.domain))throw new Error("Missing domain");if(o.domainUrl=function(e){if(!/^(https|http)?:\/\//.test(e)){var t=(null===location||void 0===location?void 0:location.protocol)||"https";return"".concat(t,"//").concat(e)}return e}(t.domain),o.tokenIssuer=(n=o.domainUrl,"".concat(n)),!t.clientId)throw new Error("Missing clientId");o.clientId=t.clientId,o.api=new M(o.domainUrl,{debug:a});var u=(null==t?void 0:t.storageKey)||"faableauth";if(o.storageKey="".concat(u,"-").concat(o.clientId),o.storage=(null==t?void 0:t.storage)||m,o.lock=new J({lock:t.lock,storageKey:o.storageKey,debug:t.debug}),v()&&globalThis.BroadcastChannel&&o.storageKey){try{o.broadcastChannel=new globalThis.BroadcastChannel(o.storageKey)}catch(e){console.error("Failed to create a new BroadcastChannel, multi-tab state changes will not be available",e)}null===(r=o.broadcastChannel)||void 0===r||r.addEventListener("message",(function(e){return i(o,void 0,void 0,(function(){return s(this,(function(t){switch(t.label){case 0:return this._debug("received broadcast notification from other tab or client",e),[4,this._notifyAllSubscribers(e.data.event,e.data.session,!1)];case 1:return t.sent(),[2]}}))}))}))}return o.autoRefreshToken=!0,o.initialize(),o}return r(t,e),t.prototype.initialize=function(){return i(this,void 0,void 0,(function(){var e=this;return s(this,(function(t){switch(t.label){case 0:return this.initializePromise?[4,this.initializePromise]:[3,2];case 1:case 3:return[2,t.sent()];case 2:return this.initializePromise=i(e,void 0,void 0,(function(){var e=this;return s(this,(function(t){switch(t.label){case 0:return[4,this.lock._acquireLock(-1,(function(){return i(e,void 0,void 0,(function(){return s(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]}}))}))},t.prototype._initialize=function(){return i(this,void 0,void 0,(function(){var e,t,r,n,o,a,u,c=this;return s(this,(function(l){switch(l.label){case 0:return l.trys.push([0,8,9,11]),[4,this._detectFlowType()];case 1:return e=l.sent(),this._debug("#_initialize()","begin","flow_type",e),e?[4,this._getSessionFromURL(e)]:[3,6];case 2:return t=l.sent(),r=t.data,(n=t.error)?(this._debug("#_initialize()","error detecting session from URL",n),"Identity is already linked"===(null==n?void 0:n.message)||"Identity is already linked to another user"===(null==n?void 0:n.message)?[2,{error:n}]:[4,this._removeSession()]):[3,4];case 3:return l.sent(),[2,{error:n}];case 4:return o=r.session,a=r.redirectType,this._debug("#_initialize()","detected session in URL",o,"redirect type",a),[4,this._saveSession(o)];case 5:return l.sent(),setTimeout((function(){return i(c,void 0,void 0,(function(){return s(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}];case 6:return[4,this._recoverAndRefresh()];case 7:return l.sent(),[2,{error:null}];case 8:return I(u=l.sent())?[2,{error:u}]:[2,{error:new C("Unexpected error during initialization",u)}];case 9:return[4,this._handleVisibilityChange()];case 10:return l.sent(),this._debug("#_initialize()","end"),[7];case 11:return[2]}}))}))},t.prototype._getSessionFromURL=function(e){return i(this,void 0,void 0,(function(){var t,r,n,i,o,a,u,c,l,h,d,f,v,p,b,g,_,y,w;return s(this,(function(s){switch(s.label){case 0:if(s.trys.push([0,4,,5]),t=B(null==P?void 0:P.location.href),"pkce"!=e)return[3,2];if(!t.code)throw new E("No code detected.");return[4,this._exchangeCodeForSession(t.code)];case 1:if(r=s.sent(),n=r.data,i=r.error)throw i;return W(["code"]),[2,{data:{session:n.session,redirectType:null},error:null}];case 2:if(t.error||t.error_description||t.error_code)throw new A(t.error_description||"Error in URL with unspecified error_description",{error:t.error||"unspecified_error",code:t.error_code||"unspecified_code"});if(o=t.provider_token,a=t.provider_refresh_token,u=t.access_token,c=t.refresh_token,l=t.expires_in,h=t.expires_at,d=t.token_type,!(u&&l&&c&&d))throw new A("No session defined in URL");return f=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("@supabase/gotrue-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("@supabase/gotrue-js: Session as retrieved from URL was issued over 120s ago, URL could be stale",u,o,i):i-u<0&&console.warn("@supabase/gotrue-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:l,expires_at:h,refreshTick:Y}),v=f.expiresAt,p=f.expiresIn,[4,this._getUser(u)];case 3:if(b=s.sent(),g=b.data,(_=b.error)||!g)throw _;return y={provider_token:o,provider_refresh_token:a,access_token:u,expires_in:p,expires_at:v,refresh_token:c,token_type:d,user:g},W(["access_token","expires_in","refresh_token","token_type","scope"]),this._debug("#_getSessionFromURL()","clearing window.location.hash"),[2,{data:{session:y,redirectType:t.type},error:null}];case 4:if(w=s.sent(),this._debug(w),I(w))return[2,{data:{session:null,redirectType:null},error:w}];throw w;case 5:return[2]}}))}))},t.prototype._exchangeCodeForSession=function(e){return i(this,void 0,void 0,(function(){var t,r,i,o,a,l,h,d,f,v,p,b;return s(this,(function(s){switch(s.label){case 0:return[4,u(this.storage,"".concat(this.storageKey,"-code-verifier"))];case 1:return t=s.sent(),r=(null!=t?t:"").split("/"),i=r[0],o=r[1],[4,j("".concat(this.domainUrl,"/oauth/token"),{client_id:this.clientId,grant_type:"authorization_code",code:e,code_verifier:i})];case 2:if(a=s.sent(),l=g(a),h=l.data,d=l.error,!h)throw new Error("Missing data");return[4,c(this.storage,"".concat(this.storageKey,"-code-verifier"))];case 3:return s.sent(),d?[2,{data:{user:null,session:null,redirectType: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},error:new R}];case 4:if(v=s.sent(),p=v.data,(b=v.error)||!p)throw b;return f=n(n({},f),{user:p}),h.session=f,[4,this._saveSession(f)];case 5:return s.sent(),[4,this._notifyAllSubscribers("SIGNED_IN",f)];case 6:s.sent(),s.label=7;case 7:return[2,{data:n(n({},h),{redirectType:null!=o?o:null}),error:d}]}}))}))},t.prototype._handleVisibilityChange=function(){return i(this,void 0,void 0,(function(){var e,t=this;return s(this,(function(r){switch(r.label){case 0:if(this._debug("#_handleVisibilityChange()"),!v()||!(null==P?void 0:P.addEventListener))return this.autoRefreshToken&&this.startAutoRefresh(),[2,!1];r.label=1;case 1:return r.trys.push([1,3,,4]),this.visibilityChangedCallback=function(){return i(t,void 0,void 0,(function(){return s(this,(function(e){switch(e.label){case 0:return[4,this._onVisibilityChanged(!1)];case 1:return[2,e.sent()]}}))}))},null==P||P.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]}}))}))},t.prototype._onVisibilityChanged=function(e){return i(this,void 0,void 0,(function(){var t,r=this;return s(this,(function(n){switch(n.label){case 0:return t="#_onVisibilityChanged(".concat(e,")"),this._debug(t,"visibilityState",D.visibilityState),"visible"!==D.visibilityState?[3,4]:(this.autoRefreshToken&&this._startAutoRefresh(),e?[3,3]:[4,this.initializePromise]);case 1:return n.sent(),[4,this.lock._acquireLock(-1,(function(){return i(r,void 0,void 0,(function(){return s(this,(function(e){switch(e.label){case 0:return"visible"!==D.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:n.sent(),n.label=3;case 3:return[3,5];case 4:"hidden"===D.visibilityState&&this.autoRefreshToken&&this._stopAutoRefresh(),n.label=5;case 5:return[2]}}))}))},t.prototype._recoverAndRefresh=function(){return i(this,void 0,void 0,(function(){var e,t,r,n,i,o,a;return s(this,(function(s){switch(s.label){case 0:e="#_recoverAndRefresh()",this._debug(e,"begin"),s.label=1;case 1:return s.trys.push([1,12,13,14]),[4,u(this.storage,this.storageKey)];case 2:return t=s.sent(),this._debug(e,"session from storage",t),this._isValidSession(t)?[3,5]:(this._debug(e,"session is not valid"),null===t?[3,4]:[4,this._removeSession()]);case 3:s.sent(),s.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(i=s.sent().error)?(console.error(i),U(i)?[3,8]:(this._debug(e,"refresh failed with a non-retryable error, removing the session",i),[4,this._removeSession()])):[3,8];case 7:s.sent(),s.label=8;case 8:return[3,11];case 9:return[4,this._notifyAllSubscribers("SIGNED_IN",t)];case 10:s.sent(),s.label=11;case 11:return[3,14];case 12:return o=s.sent(),this._debug(e,"error",o),console.error(o),[2];case 13:return this._debug(e,"end"),[7];case 14:return[2]}}))}))},t.prototype._removeVisibilityChangedCallback=function(){this._debug("#_removeVisibilityChangedCallback()");var e=this.visibilityChangedCallback;this.visibilityChangedCallback=null;try{e&&v()&&(null==P?void 0:P.removeEventListener)&&P.removeEventListener("visibilitychange",e)}catch(e){console.error("removing visibilitychange callback failed",e)}},t.prototype.startAutoRefresh=function(){return i(this,void 0,void 0,(function(){return s(this,(function(e){switch(e.label){case 0:return this._removeVisibilityChangedCallback(),[4,this._startAutoRefresh()];case 1:return e.sent(),[2]}}))}))},t.prototype._startAutoRefresh=function(){return i(this,void 0,void 0,(function(){var e,t=this;return s(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()}),Y),this.autoRefreshTicker=e,e&&"object"==typeof e&&"function"==typeof e.unref?e.unref():void 0!==globalThis.Deno&&"function"==typeof globalThis.Deno.unrefTimer&&Deno.unrefTimer(e),setTimeout((function(){return i(t,void 0,void 0,(function(){return s(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]}}))}))},t.prototype._stopAutoRefresh=function(){return i(this,void 0,void 0,(function(){var e;return s(this,(function(t){return this._debug("#_stopAutoRefresh()"),e=this.autoRefreshTicker,this.autoRefreshTicker=null,e&&clearInterval(e),[2]}))}))},t.prototype._autoRefreshTokenTick=function(){return i(this,void 0,void 0,(function(){var e,t=this;return s(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 i(t,void 0,void 0,(function(){var e,t,r=this;return s(this,(function(n){switch(n.label){case 0:n.trys.push([0,,5,6]),e=Date.now(),n.label=1;case 1:return n.trys.push([1,3,,4]),[4,this._useSession((function(t){return i(r,void 0,void 0,(function(){var r,n;return s(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)/Y),this._debug("#_autoRefreshTokenTick()","access token expires in ".concat(n," ticks, a tick lasts ").concat(Y,"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,n.sent()];case 3:return t=n.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 V))throw e;return this._debug("auto refresh token tick lock not available"),[3,4];case 4:return[2]}}))}))},t.prototype._detectFlowType=function(){return i(this,void 0,void 0,(function(){var e,t;return s(this,(function(r){return e=B(null==P?void 0:P.location.href),(t=v())&&e.code?[2,"pkce"]:t&&(e.access_token||e.error_description)?[2,"implicit"]:[2,null]}))}))},t.prototype._scope=function(){return this.scope||"openid profile email"},t.prototype._getUrlForConnection=function(e,t){return i(this,void 0,void 0,(function(){var r,i,o,a,u;return s(this,(function(s){switch(s.label){case 0:return r=t.queryParams||{},i={client_id:this.clientId,response_type:t.response_type,redirect_uri:t.redirectTo||this.redirect_uri||(null==P?void 0:P.location.origin),scope:t.scopes||this._scope()},[4,this._detectFlowType()];case 1:return"pkce"!==s.sent()?[3,3]:[4,f(this.storage,this.storageKey)];case 2:o=s.sent(),a=o[0],u=o[1],r=n(n({},r),{code_challenge:a,code_challenge_method:u}),s.label=3;case 3:return t.connection&&(i.connection=t.connection),[2,"".concat(e,"?").concat(new URLSearchParams(n(n({},r),i)))]}}))}))},t.prototype.signInWithOauthConnection=function(e){return i(this,void 0,void 0,(function(){return s(this,(function(t){switch(t.label){case 0:return[4,this._handleConnectionSignIn({connection:e.connection,redirectTo:null==e?void 0:e.redirectTo,scopes:null==e?void 0:e.scopes,queryParams:e.queryParams,skipBrowserRedirect:e.skipBrowserRedirect})];case 1:return[2,t.sent()]}}))}))},t.prototype.signInWithUsernamePassword=function(e){return i(this,void 0,void 0,(function(){var t,r,n=this;return s(this,(function(o){switch(o.label){case 0:return t=function(e){return i(n,void 0,void 0,(function(){var t;return s(this,(function(n){return console.log("rawres"),console.log(r),(t=D.createElement("div")).innerHTML=e,D.body.appendChild(t).children[0].submit(),[2]}))}))},[4,j("".concat(this.domainUrl,"/usernamepassword/login"),{username:e.username,password:e.password,redirect_uri:e.redirect_uri||this.redirect_uri||(null==P?void 0:P.location.origin),client_id:this.clientId,state:e.state},{raw:!0})];case 1:if(!(r=o.sent()).data||r.error)throw new Error(r.error||"Error in username password login");return t(r.data),[2]}}))}))},t.prototype.changePassword=function(e){return i(this,void 0,void 0,(function(){var t,r;return s(this,(function(n){switch(n.label){case 0:if(!(null==e?void 0:e.email))throw new Error("email is required");return[4,j("".concat(this.domainUrl,"/dbconnections/change_password"),{email:e.email})];case 1:return t=n.sent(),r=t.data,t.error,[2,r]}}))}))},t.prototype.buildAuthorizeUrl=function(e){void 0===e&&(e={});var t={client_id:this.clientId,redirect_uri:e.redirectTo||this.redirect_uri||(null==P?void 0:P.location.origin),response_type:e.response_type||v()?"code":"token",audience:e.audience,scope:e.scope},r=Object.fromEntries(Object.entries(t).filter((function(e){return e[0],!!e[1]})));return"".concat(this.domainUrl,"/authorize?").concat(new URLSearchParams(r).toString())},t.prototype.authorize=function(e){var t=this.buildAuthorizeUrl(e);$.redirect(t)},t.prototype._handleConnectionSignIn=function(e){return i(this,void 0,void 0,(function(){var t;return s(this,(function(r){switch(r.label){case 0:return[4,this._getUrlForConnection("".concat(this.domainUrl,"/authorize"),{response_type:v()?"code":"token",connection:e.connection,redirectTo:e.redirectTo,scopes:e.scopes,queryParams:e.queryParams})];case 1:return t=r.sent(),this._debug("#_handleProviderSignIn()","options",e,"url",t),v()&&!e.skipBrowserRedirect&&(null==P||P.location.assign(t)),[2,{data:{url:t},error:null}]}}))}))},t.prototype.setSession=function(e){return i(this,void 0,void 0,(function(){var t=this;return s(this,(function(r){switch(r.label){case 0:return[4,this.initializePromise];case 1:return r.sent(),[4,this.lock._acquireLock(-1,(function(){return i(t,void 0,void 0,(function(){return s(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()]}}))}))},t.prototype.getSession=function(){return i(this,void 0,void 0,(function(){var e=this;return s(this,(function(t){switch(t.label){case 0:return[4,this.initializePromise];case 1:return t.sent(),[4,this.lock._acquireLock(-1,(function(){return i(e,void 0,void 0,(function(){var e=this;return s(this,(function(t){return[2,this._useSession((function(t){return i(e,void 0,void 0,(function(){return s(this,(function(e){return[2,t]}))}))}))]}))}))}))];case 2:return[2,t.sent()]}}))}))},t.prototype._useSession=function(e){return i(this,void 0,void 0,(function(){var t;return s(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]}}))}))},t.prototype.__loadSession=function(){return i(this,void 0,void 0,(function(){var e,t,r,n,i,o,a;return s(this,(function(s){switch(s.label){case 0:this._debug("#__loadSession()","begin"),this.lock.lockAcquired||this._debug("#__loadSession()","used outside of an acquired lock!",(new Error).stack),s.label=1;case 1:return s.trys.push([1,,7,8]),e=null,[4,u(this.storage,this.storageKey)];case 2:return t=s.sent(),this._debug("#getSession()","session from storage",t),null===t?[3,5]:this._isValidSession(t)?(e=t,[3,5]):[3,3];case 3:return this._debug("#getSession()","session from storage is not valid"),[4,this._removeSession()];case 4:s.sent(),s.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("Using the user object as returned from supabase.auth.getSession() or from some supabase.auth.onAuthStateChange() events could be insecure! This value comes directly from the storage medium (usually cookies on the server) and many not be authentic. Use supabase.auth.getUser() instead which authenticates the data by contacting the Supabase Auth server."),Reflect.get(e,t,r)}}),e=n),[2,{data:{session:e},error:null}])):[2,{data:{session:null},error:null}];case 6:return i=s.sent(),o=i.session,(a=i.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]}}))}))},t.prototype._removeSession=function(){return i(this,void 0,void 0,(function(){return s(this,(function(e){switch(e.label){case 0:return this._debug("#_removeSession()"),[4,c(this.storage,this.storageKey)];case 1:return e.sent(),[2]}}))}))},t.prototype._isValidSession=function(e){return"object"==typeof e&&null!==e&&"access_token"in e&&"refresh_token"in e&&"expires_at"in e},t.prototype._setSession=function(e){return i(this,void 0,void 0,(function(){var t,r,n,i,o,a,u,c,l,h,d;return s(this,(function(s){switch(s.label){case 0:if(s.trys.push([0,7,,8]),!e.access_token||!e.refresh_token)throw new x;return t=Date.now()/1e3,r=t,n=!0,i=null,(o=k(e.access_token)).exp&&(r=o.exp,n=r<=t),n?[4,this._callRefreshToken(e.refresh_token)]:[3,2];case 1:return a=s.sent(),u=a.session,(h=a.error)?[2,{data:{user:null,session:null},error:h}]:u?(i=u,[3,6]):[2,{data:{user:null,session:null},error:null}];case 2:return[4,this._getUser(e.access_token)];case 3:if(c=s.sent(),l=c.data,(h=c.error)||!l)throw h;return i={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(i)];case 4:return s.sent(),[4,this._notifyAllSubscribers("SIGNED_IN",i)];case 5:s.sent(),s.label=6;case 6:return[2,{data:{user:i.user,session:i},error:null}];case 7:if(I(d=s.sent()))return[2,{data:{session:null,user:null},error:d}];throw d;case 8:return[2]}}))}))},t.prototype._saveSession=function(e){return i(this,void 0,void 0,(function(){return s(this,(function(t){switch(t.label){case 0:return this._debug("#_saveSession()",e),[4,a(this.storage,this.storageKey,e)];case 1:return t.sent(),[2]}}))}))},t.prototype._getUser=function(e){return i(this,void 0,void 0,(function(){var t;return s(this,(function(r){switch(r.label){case 0:if(!e)throw new Error("Cannot fetch user without token");return this._debug("#_getUser() begin"),[4,K("".concat(this.domainUrl,"/me"),{token:e})];case 1:return t=r.sent(),this._debug("#_getUser() end"),[2,{data:t.data,error:t.error}]}}))}))},t.prototype._callRefreshToken=function(e){return i(this,void 0,void 0,(function(){var t,r,n,i,o,a,u,c;return s(this,(function(s){switch(s.label){case 0:if(!e)throw new x;if(this.refreshingDeferred)return[2,this.refreshingDeferred.promise];t="#_callRefreshToken(".concat(e.substring(0,5),"...)"),this._debug(t,"begin"),s.label=1;case 1:return s.trys.push([1,5,10,11]),this.refreshingDeferred=new _,[4,this._refreshAccessToken(e)];case 2:if(r=s.sent(),n=r.data,i=r.error)throw i;if(!n.session)throw new x;return[4,this._saveSession(n.session)];case 3:return s.sent(),[4,this._notifyAllSubscribers("TOKEN_REFRESHED",n.session)];case 4:return s.sent(),a={session:n.session,error:null},this.refreshingDeferred.resolve(a),[2,a];case 5:return o=s.sent(),this._debug(t,"error",o),I(o)?(a={session:null,error:o},U(o)?[3,8]:[4,this._removeSession()]):[3,9];case 6:return s.sent(),[4,this._notifyAllSubscribers("SIGNED_OUT",null)];case 7:s.sent(),s.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]}}))}))},t.prototype._refreshAccessToken=function(e){return i(this,void 0,void 0,(function(){var t,r,o,a=this;return s(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]),r=Date.now(),[4,y((function(r){return i(a,void 0,void 0,(function(){var i,o,a,u,c,l;return s(this,(function(s){switch(s.label){case 0:return r>0?[4,w(200*Math.pow(2,r-1))]:[3,2];case 1:s.sent(),s.label=2;case 2:return this._debug(t,"refreshing attempt",r),[4,j("".concat(this.domainUrl,"/oauth/token"),{client_id:this.clientId,grant_type:"refresh_token",refresh_token:e})];case 3:if(i=s.sent(),o=g(i),!(null===(c=o.data.session)||void 0===c?void 0:c.access_token))throw new Error("Bad user");return[4,this._getUser(null===(l=o.data.session)||void 0===l?void 0:l.access_token)];case 4:if(a=s.sent(),u=a.data,a.error)throw new Error("Error requesting user");if(!u)throw new Error("No user found");return[2,{data:{session:n(n({},o.data.session),{user:u}),user:u},error:null}]}}))}))}),(function(e,t){var n=200*Math.pow(2,e);return t&&U(t)&&Date.now()+n-r<Y}))];case 2:return[2,u.sent()];case 3:if(o=u.sent(),this._debug(t,"error",o),I(o))return[2,{data:{session:null,user:null},error:o}];throw o;case 4:return this._debug(t,"end"),[7];case 5:return[2]}}))}))},t.prototype._notifyAllSubscribers=function(e,t){return i(this,arguments,void 0,(function(e,t,r){var n,o,a,u,c=this;return void 0===r&&(r=!0),s(this,(function(l){switch(l.label){case 0:n="#_notifyAllSubscribers(".concat(e,")"),this._debug(n,"begin",t,"broadcast = ".concat(r)),l.label=1;case 1:return l.trys.push([1,,3,4]),this.broadcastChannel&&r&&this.broadcastChannel.postMessage({event:e,session:t}),o=[],a=Array.from(this.stateChangeEmitters.values()).map((function(r){return i(c,void 0,void 0,(function(){var n;return s(this,(function(i){switch(i.label){case 0:return i.trys.push([0,2,,3]),[4,r.callback(e,t)];case 1:return i.sent(),[3,3];case 2:return n=i.sent(),o.push(n),[3,3];case 3:return[2]}}))}))})),[4,Promise.all(a)];case 2:if(l.sent(),o.length>0){for(u=0;u<o.length;u+=1)console.error(o[u]);throw o[0]}return[3,4];case 3:return this._debug(n,"end"),[7];case 4:return[2]}}))}))},t.prototype.signOut=function(){return i(this,arguments,void 0,(function(e){var t=this;return void 0===e&&(e={scope:"global"}),s(this,(function(r){switch(r.label){case 0:return[4,this.initializePromise];case 1:return r.sent(),[4,this.lock._acquireLock(-1,(function(){return i(t,void 0,void 0,(function(){return s(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()]}}))}))},t.prototype._signOut=function(){return i(this,arguments,void 0,(function(e){var t=this,r=void 0===e?{scope:"global"}:e,n=r.scope;return r.returnTo,s(this,(function(e){switch(e.label){case 0:return[4,this._useSession((function(e){return i(t,void 0,void 0,(function(){var t,r,i,o;return s(this,(function(s){switch(s.label){case 0:return t=e.data,(r=e.error)?[2,{error:r}]:(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((i=s.sent().error)&&(!function(e){return I(e)&&"AuthApiError"===e.name}(i)||404!==i.status&&401!==i.status))return[2,{error:i}];s.label=2;case 2:return"others"===n?[3,6]:[4,this._removeSession()];case 3:return s.sent(),[4,c(this.storage,"".concat(this.storageKey,"-code-verifier"))];case 4:return s.sent(),[4,this._notifyAllSubscribers("SIGNED_OUT",null)];case 5:s.sent(),s.label=6;case 6:return[2,{error:null}]}}))}))}))];case 1:return[2,e.sent()]}}))}))},t.prototype.onAuthStateChange=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._debug("#unsubscribe()","state change callback with id removed",r),t.stateChangeEmitters.delete(r)}};return this._debug("#onAuthStateChange()","registered callback with id",r),this.stateChangeEmitters.set(r,n),i(t,void 0,void 0,(function(){var e=this;return s(this,(function(t){switch(t.label){case 0:return[4,this.initializePromise];case 1:return t.sent(),[4,this.lock._acquireLock(-1,(function(){return i(e,void 0,void 0,(function(){return s(this,(function(e){return this._emitInitialSession(r),[2]}))}))}))];case 2:return t.sent(),[2]}}))})),{data:{subscription:n}}},t.prototype._emitInitialSession=function(e){return i(this,void 0,void 0,(function(){var t=this;return s(this,(function(r){switch(r.label){case 0:return[4,this._useSession((function(r){return i(t,void 0,void 0,(function(){var t,n,i,o,a;return s(this,(function(s){switch(s.label){case 0:if(s.trys.push([0,2,,4]),t=r.data.session,n=r.error)throw n;return[4,null===(o=this.stateChangeEmitters.get(e))||void 0===o?void 0:o.callback("INITIAL_SESSION",t)];case 1:return s.sent(),this._debug("INITIAL_SESSION","callback id",e,"session",t),[3,4];case 2:return i=s.sent(),[4,null===(a=this.stateChangeEmitters.get(e))||void 0===a?void 0:a.callback("INITIAL_SESSION",null)];case 3:return s.sent(),this._debug("INITIAL_SESSION","callback id",e,"error",i),console.error(i),[3,4];case 4:return[2]}}))}))}))];case 1:return[2,r.sent()]}}))}))},t.prototype.refreshSession=function(e){return i(this,void 0,void 0,(function(){var t=this;return s(this,(function(r){switch(r.label){case 0:return[4,this.initializePromise];case 1:return r.sent(),[4,this.lock._acquireLock(-1,(function(){return i(t,void 0,void 0,(function(){return s(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()]}}))}))},t.prototype._refreshSession=function(e){return i(this,void 0,void 0,(function(){var t,r=this;return s(this,(function(n){switch(n.label){case 0:return n.trys.push([0,2,,3]),[4,this._useSession((function(t){return i(r,void 0,void 0,(function(){var r,n,i,o,a,u;return s(this,(function(s){switch(s.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 x;return[4,this._callRefreshToken(e.refresh_token)];case 1:return i=s.sent(),o=i.session,(a=i.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,n.sent()];case 2:if(I(t=n.sent()))return[2,{data:{user:null,session:null},error:t}];throw t;case 3:return[2]}}))}))},t}(G),X=function(){};e.AuthError=S,e.FaableAuthClient=Q,e.User=X,e.createClient=function(e){return new Q(e)}}({});
6
+ !function(e){"use strict";var t=function(e,r){return t=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])},t(e,r)};function r(e,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=e}t(e,r),e.prototype=null===r?Object.create(r):(n.prototype=r.prototype,new n)}var n=function(){return n=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},n.apply(this,arguments)};function i(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 s(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 o(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 a=function(e,t,r){return i(void 0,void 0,void 0,(function(){return s(this,(function(n){switch(n.label){case 0:return[4,e.setItem(t,JSON.stringify(r))];case 1:return n.sent(),[2]}}))}))},u=function(e,t){return i(void 0,void 0,void 0,(function(){var r;return s(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]}}))}))};function c(e){return("0"+e.toString(16)).substr(-2)}function l(e){return i(this,void 0,void 0,(function(){var t,r,n,i;return s(this,(function(s){switch(s.label){case 0:return t=new TextEncoder,r=t.encode(e),[4,crypto.subtle.digest("SHA-256",r)];case 1:return n=s.sent(),i=new Uint8Array(n),[2,Array.from(i).map((function(e){return String.fromCharCode(e)})).join("")]}}))}))}function h(e){return i(this,void 0,void 0,(function(){var t;return s(this,(function(r){switch(r.label){case 0:return"undefined"!=typeof crypto&&void 0!==crypto.subtle&&"undefined"!=typeof TextEncoder?[4,l(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 d(e,t){return i(this,arguments,void 0,(function(e,t,r){var n,i,o;return void 0===r&&(r=!1),s(this,(function(s){switch(s.label){case 0:return n=function(){var e=new Uint32Array(56);if("undefined"==typeof crypto){for(var t="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-._~",r="",n=0;n<56;n++)r+=t.charAt(Math.floor(66*Math.random()));return r}return crypto.getRandomValues(e),Array.from(e,c).join("")}(),i=n,r&&(i+="/PASSWORD_RECOVERY"),[4,a(e,"".concat(t,"-code-verifier"),i)];case 1:return s.sent(),[4,h(n)];case 2:return[2,[o=s.sent(),n===o?"plain":"S256"]]}}))}))}var f=function(){return"undefined"!=typeof document},v={tested:!1,writable:!1},p=function(){if(!f())return!1;try{if("object"!=typeof globalThis.localStorage)return!1}catch(e){return!1}if(v.tested)return v.writable;var e="lswt-".concat(Math.random()).concat(Math.random());try{globalThis.localStorage.setItem(e,e),globalThis.localStorage.removeItem(e),v.tested=!0,v.writable=!0}catch(e){v.tested=!0,v.writable=!1}return v.writable};function b(e){var t,r,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=n({},i),!i.expires_at&&i.expires_in&&(s.expires_at=(r=i.expires_in,Math.round(Date.now()/1e3)+r))),{data:{session:s,user:null!==(t=i.user)&&void 0!==t?t:i},error:null}}var g=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 _(e,t){var r=this;return new Promise((function(n,o){i(r,void 0,void 0,(function(){var r,i,a;return s(this,(function(s){switch(s.label){case 0:r=0,s.label=1;case 1:if(!(r<1/0))return[3,6];s.label=2;case 2:return s.trys.push([2,4,,5]),[4,e(r)];case 3:return i=s.sent(),t(r,null,i)?[3,5]:(n(i),[2]);case 4:return a=s.sent(),t(r,a)?[3,5]:(o(a),[2]);case 5:return r++,[3,1];case 6:return[2]}}))}))}))}function y(e){return i(this,void 0,void 0,(function(){return s(this,(function(t){return[2,new Promise((function(t){setTimeout((function(){return t(null)}),e)}))]}))}))}function w(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 m={getItem:function(e){return p()?globalThis.localStorage.getItem(e):null},setItem:function(e,t){p()&&globalThis.localStorage.setItem(e,t)},removeItem:function(e){p()&&globalThis.localStorage.removeItem(e)}},k=function(e){function t(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 r(t,e),t}(Error),S=function(e){function t(t,r,n,i){var s=e.call(this,t,n,i)||this;return s.name=r,s.status=n,s}return r(t,e),t}(k),I=function(e){function t(){return e.call(this,"Auth session missing!","AuthSessionMissingError",400,void 0)||this}return r(t,e),t}(S);function x(e){return"object"==typeof e&&null!==e&&"__isAuthError"in e}!function(e){function t(t,r,n){var i=e.call(this,t,r,n)||this;return i.name="AuthApiError",i.status=r,i.code=n,i}r(t,e)}(k);var T=function(e){function t(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 r(t,e),t.prototype.toJSON=function(){return{name:this.name,message:this.message,status:this.status,details:this.details}},t}(S),A=function(e){function t(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 r(t,e),t.prototype.toJSON=function(){return{name:this.name,message:this.message,status:this.status,details:this.details}},t}(S),E=function(e){function t(t,r){var n=e.call(this,t)||this;return n.name="AuthUnknownError",n.originalError=r,n}return r(t,e),t}(k),C=function(e){function t(){return e.call(this,"Auth session or user missing","AuthInvalidTokenResponseError",500,void 0)||this}return r(t,e),t}(S);function R(e){return x(e)&&"AuthRetryableFetchError"===e.name}!function(e){function t(t,r){return e.call(this,t,"AuthRetryableFetchError",r,void 0)||this}r(t,e)}(S);var U=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,o(["FaableAuth@".concat(r," (").concat("0.0.0",") ").concat((new Date).toISOString())],e,!1))}return this},e}(),L="undefined"!=typeof window?window:void 0,P="undefined"!=typeof globalThis?globalThis:L,O=null==P?void 0:P.document,D=P.fetch,N=function(e){void 0===e&&(e={});var t={};return(null==e?void 0:e.token)&&(t=n(n({},t),{Authorization:"Bearer ".concat(null==e?void 0:e.token)})),n(n({},null==e?void 0:e.headers),t)},q=function(e){for(var t=[],r=1;r<arguments.length;r++)t[r-1]=arguments[r];return i(void 0,o([e],t,!0),void 0,(function(e,t){var r,n,i;return void 0===t&&(t={}),s(this,(function(s){switch(s.label){case 0:return t.raw?[4,e.text()]:[3,2];case 1:return n=s.sent(),[3,4];case 2:return[4,e.json()];case 3:n=s.sent(),s.label=4;case 4:return r=n,e.status>=300?[2,{data:r,error:t.raw?null===(i=JSON.parse(r))||void 0===i?void 0:i.message:null==r?void 0:r.message}]:[2,{data:r,error:null}]}}))}))},z=function(e,t){for(var r=[],a=2;a<arguments.length;a++)r[a-2]=arguments[a];return i(void 0,o([e,t],r,!0),void 0,(function(e,t,r){var i;return void 0===r&&(r={}),s(this,(function(s){switch(s.label){case 0:return s.trys.push([0,2,,3]),[4,D(e,{method:"POST",body:JSON.stringify(t),headers:n(n({},N(r)),{"Content-Type":"application/json"})})];case 1:return i=s.sent(),[2,q(i,r)];case 2:return[2,{data:null,error:s.sent()}];case 3:return[2]}}))}))},j=function(e){for(var t=[],r=1;r<arguments.length;r++)t[r-1]=arguments[r];return i(void 0,o([e],t,!0),void 0,(function(e,t){var r;return void 0===t&&(t={}),s(this,(function(i){switch(i.label){case 0:return i.trys.push([0,2,,3]),[4,D(e,n(n({},t),{method:"GET",headers:N(t)}))];case 1:return r=i.sent(),[2,q(r,t)];case 2:return[2,{data:null,error:i.sent()}];case 3:return[2]}}))}))},M=function(e){function t(t,r){var n=e.call(this,r)||this;return n.base_url=t,n}return r(t,e),t.prototype.extraPrint=function(){return"api"},t.prototype.signOut=function(e){return i(this,void 0,void 0,(function(){var t,r;return s(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,j(t)];case 1:return r=n.sent(),this._debug(r),r.error?[2,{error:r.error,data:null}]:[2,{error:null,data:null}]}}))}))},t}(U);globalThis&&p()&&globalThis.localStorage&&globalThis.localStorage.getItem("supabase.gotrue-js.locks.debug");var K=function(e){function t(t){var r=e.call(this,t)||this;return r.isAcquireTimeout=!0,r}return r(t,e),t}(Error);function V(e,t,r){return i(this,void 0,void 0,(function(){return s(this,(function(e){switch(e.label){case 0:return[4,r()];case 1:return[2,e.sent()]}}))}))}!function(e){function t(){return null!==e&&e.apply(this,arguments)||this}r(t,e)}(K);var F=function(e){function t(r){void 0===r&&(r={});var n=e.call(this,r)||this;return n.instanceID=t.nextInstanceID,t.nextInstanceID+=1,n}return r(t,e),t.prototype.extraPrint=function(){return this.instanceID.toString()},t.nextInstanceID=0,t}(U),G=function(e){function t(t){var r=e.call(this,{debug:t.debug})||this;return r.lockAcquired=!1,r.pendingInLock=[],r.lock=t.lock||V,r.storageKey=t.storageKey,r}return r(t,e),t.prototype._acquireLock=function(e,t){return i(this,void 0,void 0,(function(){var r,n,a=this;return s(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(),n=i(a,void 0,void 0,(function(){return s(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(i(a,void 0,void 0,(function(){return s(this,(function(e){switch(e.label){case 0:return e.trys.push([0,2,,3]),[4,n];case 1:case 2:return e.sent(),[3,3];case 3:return[2]}}))}))),[2,n]):[4,this.lock("lock:".concat(this.storageKey),e,(function(){return i(a,void 0,void 0,(function(){var e,r,n=this;return s(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(i(n,void 0,void 0,(function(){return s(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=o([],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]}}))}))},t}(F);function W(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 J=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())};function B(){if(!L)throw new Error("No window in environment");return L}var H={redirect:function(e){B().location=e},getDocument:function(){return B().document},getWindow:B},$=3e4,Y=function(e){function t(t){var r,o,a=this,u=(null==t?void 0:t.debug)||!1;if((a=e.call(this,{debug:u})||this).initializePromise=null,a.detectSessionInUrl=!0,a.autoRefreshTicker=null,a.visibilityChangedCallback=null,a.refreshingDeferred=null,a.broadcastChannel=null,a.stateChangeEmitters=new Map,a._session=null,a.sessionCheckExpiryDays=1,a.redirect_uri=(null==t?void 0:t.redirect_uri)||"",!(null==t?void 0:t.domain))throw new Error("Missing domain");if(a.domainUrl=function(e){if(!/^(https|http)?:\/\//.test(e)){var t=(null===location||void 0===location?void 0:location.protocol)||"https";return"".concat(t,"//").concat(e)}return e}(t.domain),a.tokenIssuer=(o=a.domainUrl,"".concat(o)),!t.clientId)throw new Error("Missing clientId");a.clientId=t.clientId,a.api=new M(a.domainUrl,{debug:u});var c=(null==t?void 0:t.storageKey)||"faableauth";if(a.storageKey="".concat(c,"-").concat(a.clientId),(null==t?void 0:t.cookieOptions)?a.storage=function(e){void 0===e&&(e={});var t=n({path:"/",sameSite:"Lax",secure:f()&&"https:"===window.location.protocol},e);return{getItem:function(e){if(!f())return null;for(var t=encodeURIComponent(e)+"=",r=decodeURIComponent(document.cookie).split(";"),n=0;n<r.length;n++){for(var i=r[n];" "===i.charAt(0);)i=i.substring(1);if(0===i.indexOf(t))return i.substring(t.length,i.length)}return null},setItem:function(e,r){if(f()){var n="".concat(encodeURIComponent(e),"=").concat(encodeURIComponent(r));t.maxAge&&(n+="; Max-Age=".concat(t.maxAge)),t.domain&&(n+="; Domain=".concat(t.domain)),t.path&&(n+="; Path=".concat(t.path)),t.sameSite&&(n+="; SameSite=".concat(t.sameSite)),t.secure&&(n+="; Secure"),document.cookie=n}},removeItem:function(e){if(f()){var r="".concat(encodeURIComponent(e),"=; Max-Age=-99999999;");t.domain&&(r+=" Domain=".concat(t.domain,";")),t.path&&(r+=" Path=".concat(t.path,";")),document.cookie=r}}}}(t.cookieOptions):a.storage=(null==t?void 0:t.storage)||m,a.lock=new G({lock:t.lock,storageKey:a.storageKey,debug:t.debug}),f()&&globalThis.BroadcastChannel&&a.storageKey){try{a.broadcastChannel=new globalThis.BroadcastChannel(a.storageKey)}catch(e){console.error("Failed to create a new BroadcastChannel, multi-tab state changes will not be available",e)}null===(r=a.broadcastChannel)||void 0===r||r.addEventListener("message",(function(e){return i(a,void 0,void 0,(function(){return s(this,(function(t){switch(t.label){case 0:return this._debug("received broadcast notification from other tab or client",e),[4,this._notifyAllSubscribers(e.data.event,e.data.session,!1)];case 1:return t.sent(),[2]}}))}))}))}return a.autoRefreshToken=!0,a.initialize(),a}return r(t,e),Object.defineProperty(t.prototype,"session",{get:function(){return this._session},enumerable:!1,configurable:!0}),t.prototype.initialize=function(){return i(this,void 0,void 0,(function(){var e=this;return s(this,(function(t){switch(t.label){case 0:return this.initializePromise?[4,this.initializePromise]:[3,2];case 1:case 3:return[2,t.sent()];case 2:return this.initializePromise=i(e,void 0,void 0,(function(){var e=this;return s(this,(function(t){switch(t.label){case 0:return[4,this.lock._acquireLock(-1,(function(){return i(e,void 0,void 0,(function(){return s(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]}}))}))},t.prototype._initialize=function(){return i(this,void 0,void 0,(function(){var e,t,r,n,o,a,u,c=this;return s(this,(function(l){switch(l.label){case 0:return l.trys.push([0,8,9,11]),[4,this._detectFlowType()];case 1:return e=l.sent(),this._debug("#_initialize()","begin","flow_type",e),e?[4,this._getSessionFromURL(e)]:[3,6];case 2:return t=l.sent(),r=t.data,(n=t.error)?(this._debug("#_initialize()","error detecting session from URL",n),"Identity is already linked"===(null==n?void 0:n.message)||"Identity is already linked to another user"===(null==n?void 0:n.message)?[2,{error:n}]:[4,this._removeSession()]):[3,4];case 3:return l.sent(),[2,{error:n}];case 4:return o=r.session,a=r.redirectType,this._debug("#_initialize()","detected session in URL",o,"redirect type",a),[4,this._saveSession(o)];case 5:return l.sent(),setTimeout((function(){return i(c,void 0,void 0,(function(){return s(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}];case 6:return[4,this._recoverAndRefresh()];case 7:return l.sent(),[2,{error:null}];case 8:return x(u=l.sent())?[2,{error:u}]:[2,{error:new E("Unexpected error during initialization",u)}];case 9:return[4,this._handleVisibilityChange()];case 10:return l.sent(),this._debug("#_initialize()","end"),[7];case 11:return[2]}}))}))},t.prototype._getSessionFromURL=function(e){return i(this,void 0,void 0,(function(){var t,r,n,i,o,a,u,c,l,h,d,f,v,p,b,g,_,y,w;return s(this,(function(s){switch(s.label){case 0:if(s.trys.push([0,4,,5]),t=W(null==L?void 0:L.location.href),"pkce"!=e)return[3,2];if(!t.code)throw new A("No code detected.");return[4,this._exchangeCodeForSession(t.code)];case 1:if(r=s.sent(),n=r.data,i=r.error)throw i;return J(["code"]),[2,{data:{session:n.session,redirectType:null},error:null}];case 2:if(t.error||t.error_description||t.error_code)throw new T(t.error_description||"Error in URL with unspecified error_description",{error:t.error||"unspecified_error",code:t.error_code||"unspecified_code"});if(o=t.provider_token,a=t.provider_refresh_token,u=t.access_token,c=t.refresh_token,l=t.expires_in,h=t.expires_at,d=t.token_type,!(u&&l&&c&&d))throw new T("No session defined in URL");return f=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("@supabase/gotrue-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("@supabase/gotrue-js: Session as retrieved from URL was issued over 120s ago, URL could be stale",u,o,i):i-u<0&&console.warn("@supabase/gotrue-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:l,expires_at:h,refreshTick:$}),v=f.expiresAt,p=f.expiresIn,[4,this._getUser(u)];case 3:if(b=s.sent(),g=b.data,(_=b.error)||!g)throw _;return y={provider_token:o,provider_refresh_token:a,access_token:u,expires_in:p,expires_at:v,refresh_token:c,token_type:d,user:g},J(["access_token","expires_in","refresh_token","token_type","scope"]),this._debug("#_getSessionFromURL()","clearing window.location.hash"),[2,{data:{session:y,redirectType:t.type},error:null}];case 4:if(w=s.sent(),this._debug(w),x(w))return[2,{data:{session:null,redirectType:null},error:w}];throw w;case 5:return[2]}}))}))},t.prototype._exchangeCodeForSession=function(e){return i(this,void 0,void 0,(function(){var t,r,i,o,a,c,l,h,d,f,v,p;return s(this,(function(s){switch(s.label){case 0:return[4,u(this.storage,"".concat(this.storageKey,"-code-verifier"))];case 1:return t=s.sent(),r=(null!=t?t:"").split("/"),i=r[0],o=r[1],[4,z("".concat(this.domainUrl,"/oauth/token"),{client_id:this.clientId,grant_type:"authorization_code",code:e,code_verifier:i})];case 2:if(a=s.sent(),c=b(a),l=c.data,h=c.error,!l)throw new Error("Missing data");return[4,this.storage.removeItem("".concat(this.storageKey,"-code-verifier"))];case 3:return s.sent(),h?[2,{data:{user:null,session:null,redirectType:null},error:h}]:l&&l.session&&l.user?(d=l.session)?[4,this._getUser(d.access_token)]:[3,7]:[2,{data:{user:null,session:null,redirectType:null},error:new C}];case 4:if(f=s.sent(),v=f.data,(p=f.error)||!v)throw p;return d=n(n({},d),{user:v}),l.session=d,[4,this._saveSession(d)];case 5:return s.sent(),[4,this._notifyAllSubscribers("SIGNED_IN",d)];case 6:s.sent(),s.label=7;case 7:return[2,{data:n(n({},l),{redirectType:null!=o?o:null}),error:h}]}}))}))},t.prototype._handleVisibilityChange=function(){return i(this,void 0,void 0,(function(){var e,t=this;return s(this,(function(r){switch(r.label){case 0:if(this._debug("#_handleVisibilityChange()"),!f()||!(null==L?void 0:L.addEventListener))return this.autoRefreshToken&&this.startAutoRefresh(),[2,!1];r.label=1;case 1:return r.trys.push([1,3,,4]),this.visibilityChangedCallback=function(){return i(t,void 0,void 0,(function(){return s(this,(function(e){switch(e.label){case 0:return[4,this._onVisibilityChanged(!1)];case 1:return[2,e.sent()]}}))}))},null==L||L.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]}}))}))},t.prototype._onVisibilityChanged=function(e){return i(this,void 0,void 0,(function(){var t,r=this;return s(this,(function(n){switch(n.label){case 0:return t="#_onVisibilityChanged(".concat(e,")"),this._debug(t,"visibilityState",O.visibilityState),"visible"!==O.visibilityState?[3,4]:(this.autoRefreshToken&&this._startAutoRefresh(),e?[3,3]:[4,this.initializePromise]);case 1:return n.sent(),[4,this.lock._acquireLock(-1,(function(){return i(r,void 0,void 0,(function(){return s(this,(function(e){switch(e.label){case 0:return"visible"!==O.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:n.sent(),n.label=3;case 3:return[3,5];case 4:"hidden"===O.visibilityState&&this.autoRefreshToken&&this._stopAutoRefresh(),n.label=5;case 5:return[2]}}))}))},t.prototype._recoverAndRefresh=function(){return i(this,void 0,void 0,(function(){var e,t,r,n,i,o,a;return s(this,(function(s){switch(s.label){case 0:e="#_recoverAndRefresh()",this._debug(e,"begin"),s.label=1;case 1:return s.trys.push([1,12,13,14]),[4,u(this.storage,this.storageKey)];case 2:return t=s.sent(),this._debug(e,"session from storage",t),this._isValidSession(t)?[3,5]:(this._debug(e,"session is not valid"),null===t?[3,4]:[4,this._removeSession()]);case 3:s.sent(),s.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(i=s.sent().error)?(console.error(i),R(i)?[3,8]:(this._debug(e,"refresh failed with a non-retryable error, removing the session",i),[4,this._removeSession()])):[3,8];case 7:s.sent(),s.label=8;case 8:return[3,11];case 9:return this._session=t,[4,this._notifyAllSubscribers("SIGNED_IN",t)];case 10:s.sent(),s.label=11;case 11:return[3,14];case 12:return o=s.sent(),this._debug(e,"error",o),console.error(o),[2];case 13:return this._debug(e,"end"),[7];case 14:return[2]}}))}))},t.prototype._removeVisibilityChangedCallback=function(){this._debug("#_removeVisibilityChangedCallback()");var e=this.visibilityChangedCallback;this.visibilityChangedCallback=null;try{e&&f()&&(null==L?void 0:L.removeEventListener)&&L.removeEventListener("visibilitychange",e)}catch(e){console.error("removing visibilitychange callback failed",e)}},t.prototype.startAutoRefresh=function(){return i(this,void 0,void 0,(function(){return s(this,(function(e){switch(e.label){case 0:return this._removeVisibilityChangedCallback(),[4,this._startAutoRefresh()];case 1:return e.sent(),[2]}}))}))},t.prototype._startAutoRefresh=function(){return i(this,void 0,void 0,(function(){var e,t=this;return s(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()}),$),this.autoRefreshTicker=e,e&&"object"==typeof e&&"function"==typeof e.unref?e.unref():void 0!==globalThis.Deno&&"function"==typeof globalThis.Deno.unrefTimer&&Deno.unrefTimer(e),setTimeout((function(){return i(t,void 0,void 0,(function(){return s(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]}}))}))},t.prototype._stopAutoRefresh=function(){return i(this,void 0,void 0,(function(){var e;return s(this,(function(t){return this._debug("#_stopAutoRefresh()"),e=this.autoRefreshTicker,this.autoRefreshTicker=null,e&&clearInterval(e),[2]}))}))},t.prototype._autoRefreshTokenTick=function(){return i(this,void 0,void 0,(function(){var e,t=this;return s(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 i(t,void 0,void 0,(function(){var e,t,r=this;return s(this,(function(n){switch(n.label){case 0:n.trys.push([0,,5,6]),e=Date.now(),n.label=1;case 1:return n.trys.push([1,3,,4]),[4,this._useSession((function(t){return i(r,void 0,void 0,(function(){var r,n;return s(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)/$),this._debug("#_autoRefreshTokenTick()","access token expires in ".concat(n," ticks, a tick lasts ").concat($,"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,n.sent()];case 3:return t=n.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 K))throw e;return this._debug("auto refresh token tick lock not available"),[3,4];case 4:return[2]}}))}))},t.prototype._detectFlowType=function(){return i(this,void 0,void 0,(function(){var e,t;return s(this,(function(r){return e=W(null==L?void 0:L.location.href),(t=f())&&e.code?[2,"pkce"]:t&&(e.access_token||e.error_description)?[2,"implicit"]:[2,null]}))}))},t.prototype._scope=function(){return this.scope||"openid profile email"},t.prototype._getUrlForConnection=function(e,t){return i(this,void 0,void 0,(function(){var r,i,o,a,u;return s(this,(function(s){switch(s.label){case 0:return r=t.queryParams||{},i={client_id:this.clientId,response_type:t.response_type,redirect_uri:t.redirectTo||this.redirect_uri||(null==L?void 0:L.location.origin),scope:t.scopes||this._scope()},[4,this._detectFlowType()];case 1:return"pkce"!==s.sent()?[3,3]:[4,d(this.storage,this.storageKey)];case 2:o=s.sent(),a=o[0],u=o[1],r=n(n({},r),{code_challenge:a,code_challenge_method:u}),s.label=3;case 3:return t.connection&&(i.connection=t.connection),[2,"".concat(e,"?").concat(new URLSearchParams(n(n({},r),i)))]}}))}))},t.prototype.signInWithOauthConnection=function(e){return i(this,void 0,void 0,(function(){return s(this,(function(t){switch(t.label){case 0:return[4,this._handleConnectionSignIn({connection:e.connection,redirectTo:null==e?void 0:e.redirectTo,scopes:null==e?void 0:e.scopes,queryParams:e.queryParams,skipBrowserRedirect:e.skipBrowserRedirect})];case 1:return[2,t.sent()]}}))}))},t.prototype.signInWithUsernamePassword=function(e){return i(this,void 0,void 0,(function(){var t,r,n=this;return s(this,(function(o){switch(o.label){case 0:return t=function(e){return i(n,void 0,void 0,(function(){var t;return s(this,(function(n){return console.log("rawres"),console.log(r),(t=O.createElement("div")).innerHTML=e,O.body.appendChild(t).children[0].submit(),[2]}))}))},[4,z("".concat(this.domainUrl,"/usernamepassword/login"),{username:e.username,password:e.password,redirect_uri:e.redirect_uri||this.redirect_uri||(null==L?void 0:L.location.origin),client_id:this.clientId,state:e.state},{raw:!0})];case 1:if(!(r=o.sent()).data||r.error)throw new Error(r.error||"Error in username password login");return t(r.data),[2]}}))}))},t.prototype.signInWithOtp=function(e){return i(this,void 0,void 0,(function(){var t,r,n,i,o;return s(this,(function(s){switch(s.label){case 0:return[4,z("".concat(this.domainUrl,"/oauth/token"),{client_id:this.clientId,grant_type:"http://auth0.com/oauth/grant-type/passwordless/otp",username:e.username,otp:e.otp})];case 1:return t=s.sent(),r=b(t),n=r.data,(i=r.error)?[2,{data:{user:null,session:null},error:i}]:n&&n.session&&n.user?(o=n.session,[4,this._saveSession(o)]):[2,{data:{user:null,session:null},error:new C}];case 2:return s.sent(),[4,this._notifyAllSubscribers("SIGNED_IN",o)];case 3:return s.sent(),[2,{data:{user:o.user,session:o},error:null}]}}))}))},t.prototype.signInWithPasswordless=function(e){return i(this,void 0,void 0,(function(){var t;return s(this,(function(r){switch(r.label){case 0:return[4,z("".concat(this.domainUrl,"/passwordless/start"),{client_id:this.clientId,email:e.email,send:e.type})];case 1:return[2,{data:(t=r.sent()).data,error:t.error}]}}))}))},t.prototype.changePassword=function(e){return i(this,void 0,void 0,(function(){var t,r;return s(this,(function(n){switch(n.label){case 0:if(!(null==e?void 0:e.email))throw new Error("email is required");return[4,z("".concat(this.domainUrl,"/dbconnections/change_password"),{email:e.email})];case 1:return t=n.sent(),r=t.data,t.error,[2,r]}}))}))},t.prototype.buildAuthorizeUrl=function(e){void 0===e&&(e={});var t={client_id:this.clientId,redirect_uri:e.redirectTo||this.redirect_uri||(null==L?void 0:L.location.origin),response_type:e.response_type||f()?"code":"token",audience:e.audience,scope:e.scope,connection:e.connection},r=Object.fromEntries(Object.entries(t).filter((function(e){return e[0],!!e[1]})));return"".concat(this.domainUrl,"/authorize?").concat(new URLSearchParams(r).toString())},t.prototype.authorize=function(e){var t=this.buildAuthorizeUrl(e);H.redirect(t)},t.prototype._handleConnectionSignIn=function(e){return i(this,void 0,void 0,(function(){var t;return s(this,(function(r){switch(r.label){case 0:return[4,this._getUrlForConnection("".concat(this.domainUrl,"/authorize"),{response_type:f()?"code":"token",connection:e.connection,redirectTo:e.redirectTo,scopes:e.scopes,queryParams:e.queryParams})];case 1:return t=r.sent(),this._debug("#_handleProviderSignIn()","options",e,"url",t),f()&&!e.skipBrowserRedirect&&(null==L||L.location.assign(t)),[2,{data:{url:t},error:null}]}}))}))},t.prototype.setSession=function(e){return i(this,void 0,void 0,(function(){var t=this;return s(this,(function(r){switch(r.label){case 0:return[4,this.initializePromise];case 1:return r.sent(),[4,this.lock._acquireLock(-1,(function(){return i(t,void 0,void 0,(function(){return s(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()]}}))}))},t.prototype.getSession=function(){return i(this,void 0,void 0,(function(){var e=this;return s(this,(function(t){switch(t.label){case 0:return[4,this.initializePromise];case 1:return t.sent(),[4,this.lock._acquireLock(-1,(function(){return i(e,void 0,void 0,(function(){var e=this;return s(this,(function(t){return[2,this._useSession((function(t){return i(e,void 0,void 0,(function(){return s(this,(function(e){return[2,t]}))}))}))]}))}))}))];case 2:return[2,t.sent()]}}))}))},t.prototype._useSession=function(e){return i(this,void 0,void 0,(function(){var t;return s(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]}}))}))},t.prototype.__loadSession=function(){return i(this,void 0,void 0,(function(){var e,t,r,n,i,o,a;return s(this,(function(s){switch(s.label){case 0:this._debug("#__loadSession()","begin"),this.lock.lockAcquired||this._debug("#__loadSession()","used outside of an acquired lock!",(new Error).stack),s.label=1;case 1:return s.trys.push([1,,7,8]),e=null,[4,u(this.storage,this.storageKey)];case 2:return t=s.sent(),this._debug("#getSession()","session from storage",t),null===t?[3,5]:this._isValidSession(t)?(e=t,[3,5]):[3,3];case 3:return this._debug("#getSession()","session from storage is not valid"),[4,this._removeSession()];case 4:s.sent(),s.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("Using the user object as returned from supabase.auth.getSession() or from some supabase.auth.onAuthStateChange() events could be insecure! This value comes directly from the storage medium (usually cookies on the server) and many not be authentic. Use supabase.auth.getUser() instead which authenticates the data by contacting the Supabase Auth server."),Reflect.get(e,t,r)}}),e=n),[2,{data:{session:e},error:null}])):[2,{data:{session:null},error:null}];case 6:return i=s.sent(),o=i.session,(a=i.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]}}))}))},t.prototype._removeSession=function(){return i(this,void 0,void 0,(function(){return s(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]}}))}))},t.prototype._isValidSession=function(e){return"object"==typeof e&&null!==e&&"access_token"in e&&"refresh_token"in e&&"expires_at"in e},t.prototype._setSession=function(e){return i(this,void 0,void 0,(function(){var t,r,n,i,o,a,u,c,l,h,d;return s(this,(function(s){switch(s.label){case 0:if(s.trys.push([0,7,,8]),!e.access_token||!e.refresh_token)throw new I;return t=Date.now()/1e3,r=t,n=!0,i=null,(o=w(e.access_token)).exp&&(r=o.exp,n=r<=t),n?[4,this._callRefreshToken(e.refresh_token)]:[3,2];case 1:return a=s.sent(),u=a.session,(h=a.error)?[2,{data:{user:null,session:null},error:h}]:u?(i=u,[3,6]):[2,{data:{user:null,session:null},error:null}];case 2:return[4,this._getUser(e.access_token)];case 3:if(c=s.sent(),l=c.data,(h=c.error)||!l)throw h;return i={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(i)];case 4:return s.sent(),[4,this._notifyAllSubscribers("SIGNED_IN",i)];case 5:s.sent(),s.label=6;case 6:return[2,{data:{user:i.user,session:i},error:null}];case 7:if(x(d=s.sent()))return[2,{data:{session:null,user:null},error:d}];throw d;case 8:return[2]}}))}))},t.prototype._saveSession=function(e){return i(this,void 0,void 0,(function(){return s(this,(function(t){switch(t.label){case 0:return this._debug("#_saveSession()",e),this._session=e,[4,a(this.storage,this.storageKey,e)];case 1:return t.sent(),[2]}}))}))},t.prototype._getUser=function(e){return i(this,void 0,void 0,(function(){var t;return s(this,(function(r){switch(r.label){case 0:if(!e)throw new Error("Cannot fetch user without token");return this._debug("#_getUser() begin"),[4,j("".concat(this.domainUrl,"/me"),{token:e})];case 1:return t=r.sent(),this._debug("#_getUser() end"),[2,{data:t.data,error:t.error}]}}))}))},t.prototype._callRefreshToken=function(e){return i(this,void 0,void 0,(function(){var t,r,n,i,o,a,u,c;return s(this,(function(s){switch(s.label){case 0:if(!e)throw new I;if(this.refreshingDeferred)return[2,this.refreshingDeferred.promise];t="#_callRefreshToken(".concat(e.substring(0,5),"...)"),this._debug(t,"begin"),s.label=1;case 1:return s.trys.push([1,5,10,11]),this.refreshingDeferred=new g,[4,this._refreshAccessToken(e)];case 2:if(r=s.sent(),n=r.data,i=r.error)throw i;if(!n.session)throw new I;return[4,this._saveSession(n.session)];case 3:return s.sent(),[4,this._notifyAllSubscribers("TOKEN_REFRESHED",n.session)];case 4:return s.sent(),a={session:n.session,error:null},this.refreshingDeferred.resolve(a),[2,a];case 5:return o=s.sent(),this._debug(t,"error",o),x(o)?(a={session:null,error:o},R(o)?[3,8]:[4,this._removeSession()]):[3,9];case 6:return s.sent(),[4,this._notifyAllSubscribers("SIGNED_OUT",null)];case 7:s.sent(),s.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]}}))}))},t.prototype._refreshAccessToken=function(e){return i(this,void 0,void 0,(function(){var t,r,o,a=this;return s(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]),r=Date.now(),[4,_((function(r){return i(a,void 0,void 0,(function(){var i,o,a,u,c,l;return s(this,(function(s){switch(s.label){case 0:return r>0?[4,y(200*Math.pow(2,r-1))]:[3,2];case 1:s.sent(),s.label=2;case 2:return this._debug(t,"refreshing attempt",r),[4,z("".concat(this.domainUrl,"/oauth/token"),{client_id:this.clientId,grant_type:"refresh_token",refresh_token:e})];case 3:if(i=s.sent(),o=b(i),!(null===(c=o.data.session)||void 0===c?void 0:c.access_token))throw new Error("Bad user");return[4,this._getUser(null===(l=o.data.session)||void 0===l?void 0:l.access_token)];case 4:if(a=s.sent(),u=a.data,a.error)throw new Error("Error requesting user");if(!u)throw new Error("No user found");return[2,{data:{session:n(n({},o.data.session),{user:u}),user:u},error:null}]}}))}))}),(function(e,t){var n=200*Math.pow(2,e);return t&&R(t)&&Date.now()+n-r<$}))];case 2:return[2,u.sent()];case 3:if(o=u.sent(),this._debug(t,"error",o),x(o))return[2,{data:{session:null,user:null},error:o}];throw o;case 4:return this._debug(t,"end"),[7];case 5:return[2]}}))}))},t.prototype._notifyAllSubscribers=function(e,t){return i(this,arguments,void 0,(function(e,t,r){var n,o,a,u,c=this;return void 0===r&&(r=!0),s(this,(function(l){switch(l.label){case 0:n="#_notifyAllSubscribers(".concat(e,")"),this._debug(n,"begin",t,"broadcast = ".concat(r)),l.label=1;case 1:return l.trys.push([1,,3,4]),this.broadcastChannel&&r&&this.broadcastChannel.postMessage({event:e,session:t}),o=[],a=Array.from(this.stateChangeEmitters.values()).map((function(r){return i(c,void 0,void 0,(function(){var n;return s(this,(function(i){switch(i.label){case 0:return i.trys.push([0,2,,3]),[4,r.callback(e,t)];case 1:return i.sent(),[3,3];case 2:return n=i.sent(),o.push(n),[3,3];case 3:return[2]}}))}))})),[4,Promise.all(a)];case 2:if(l.sent(),o.length>0){for(u=0;u<o.length;u+=1)console.error(o[u]);throw o[0]}return[3,4];case 3:return this._debug(n,"end"),[7];case 4:return[2]}}))}))},t.prototype.signOut=function(){return i(this,arguments,void 0,(function(e){var t=this;return void 0===e&&(e={scope:"global"}),s(this,(function(r){switch(r.label){case 0:return[4,this.initializePromise];case 1:return r.sent(),[4,this.lock._acquireLock(-1,(function(){return i(t,void 0,void 0,(function(){return s(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()]}}))}))},t.prototype._signOut=function(){return i(this,arguments,void 0,(function(e){var t=this,r=void 0===e?{scope:"global"}:e,n=r.scope;return r.returnTo,s(this,(function(e){switch(e.label){case 0:return[4,this._useSession((function(e){return i(t,void 0,void 0,(function(){var t,r,i,o;return s(this,(function(s){switch(s.label){case 0:return t=e.data,(r=e.error)?[2,{error:r}]:(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((i=s.sent().error)&&(!function(e){return x(e)&&"AuthApiError"===e.name}(i)||404!==i.status&&401!==i.status))return[2,{error:i}];s.label=2;case 2:return"others"===n?[3,6]:[4,this._removeSession()];case 3:return s.sent(),[4,this.storage.removeItem("".concat(this.storageKey,"-code-verifier"))];case 4:return s.sent(),[4,this._notifyAllSubscribers("SIGNED_OUT",null)];case 5:s.sent(),s.label=6;case 6:return[2,{error:null}]}}))}))}))];case 1:return[2,e.sent()]}}))}))},t.prototype.onAuthStateChange=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._debug("#unsubscribe()","state change callback with id removed",r),t.stateChangeEmitters.delete(r)}};return this._debug("#onAuthStateChange()","registered callback with id",r),this.stateChangeEmitters.set(r,n),i(t,void 0,void 0,(function(){var e=this;return s(this,(function(t){switch(t.label){case 0:return[4,this.initializePromise];case 1:return t.sent(),[4,this.lock._acquireLock(-1,(function(){return i(e,void 0,void 0,(function(){return s(this,(function(e){return this._emitInitialSession(r),[2]}))}))}))];case 2:return t.sent(),[2]}}))})),{data:{subscription:n}}},t.prototype._emitInitialSession=function(e){return i(this,void 0,void 0,(function(){var t=this;return s(this,(function(r){switch(r.label){case 0:return[4,this._useSession((function(r){return i(t,void 0,void 0,(function(){var t,n,i,o,a;return s(this,(function(s){switch(s.label){case 0:if(s.trys.push([0,2,,4]),t=r.data.session,n=r.error)throw n;return[4,null===(o=this.stateChangeEmitters.get(e))||void 0===o?void 0:o.callback("INITIAL_SESSION",t)];case 1:return s.sent(),this._debug("INITIAL_SESSION","callback id",e,"session",t),[3,4];case 2:return i=s.sent(),[4,null===(a=this.stateChangeEmitters.get(e))||void 0===a?void 0:a.callback("INITIAL_SESSION",null)];case 3:return s.sent(),this._debug("INITIAL_SESSION","callback id",e,"error",i),console.error(i),[3,4];case 4:return[2]}}))}))}))];case 1:return[2,r.sent()]}}))}))},t.prototype.refreshSession=function(e){return i(this,void 0,void 0,(function(){var t=this;return s(this,(function(r){switch(r.label){case 0:return[4,this.initializePromise];case 1:return r.sent(),[4,this.lock._acquireLock(-1,(function(){return i(t,void 0,void 0,(function(){return s(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()]}}))}))},t.prototype._refreshSession=function(e){return i(this,void 0,void 0,(function(){var t,r=this;return s(this,(function(n){switch(n.label){case 0:return n.trys.push([0,2,,3]),[4,this._useSession((function(t){return i(r,void 0,void 0,(function(){var r,n,i,o,a,u;return s(this,(function(s){switch(s.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 I;return[4,this._callRefreshToken(e.refresh_token)];case 1:return i=s.sent(),o=i.session,(a=i.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,n.sent()];case 2:if(x(t=n.sent()))return[2,{data:{user:null,session:null},error:t}];throw t;case 3:return[2]}}))}))},t}(F),Q=function(){};e.AuthError=k,e.FaableAuthClient=Y,e.User=Q,e.createClient=function(e){return new Y(e)}}({});
7
7
  //# sourceMappingURL=faableauth.js.map