rehive 4.2.1 → 4.2.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/admin.d.mts +2 -2
- package/dist/admin.d.ts +2 -2
- package/dist/auth.d.mts +2 -2
- package/dist/auth.d.ts +2 -2
- package/dist/auth.js +1 -1
- package/dist/auth.mjs +1 -1
- package/dist/chunk-647XG2KN.js +1 -0
- package/dist/chunk-BAUHEOL5.mjs +1 -0
- package/dist/{create-api-client-CgvKBlQ_.d.ts → create-api-client-DzUaFuXN.d.ts} +1 -1
- package/dist/{create-api-client-Bkkri-AM.d.mts → create-api-client-JpNvOfh9.d.mts} +1 -1
- package/dist/{create-auth-ChOASbvo.d.mts → create-auth-D-3te_Jw.d.mts} +46 -1
- package/dist/{create-auth-ChOASbvo.d.ts → create-auth-D-3te_Jw.d.ts} +46 -1
- package/dist/extensions/alchemy.d.mts +2 -2
- package/dist/extensions/alchemy.d.ts +2 -2
- package/dist/extensions/app.d.mts +2 -2
- package/dist/extensions/app.d.ts +2 -2
- package/dist/extensions/billing.d.mts +2 -2
- package/dist/extensions/billing.d.ts +2 -2
- package/dist/extensions/bridge.d.mts +2 -2
- package/dist/extensions/bridge.d.ts +2 -2
- package/dist/extensions/builder.d.mts +2 -2
- package/dist/extensions/builder.d.ts +2 -2
- package/dist/extensions/business.d.mts +2 -2
- package/dist/extensions/business.d.ts +2 -2
- package/dist/extensions/conversion.d.mts +2 -2
- package/dist/extensions/conversion.d.ts +2 -2
- package/dist/extensions/mass-send.d.mts +2 -2
- package/dist/extensions/mass-send.d.ts +2 -2
- package/dist/extensions/notifications.d.mts +2 -2
- package/dist/extensions/notifications.d.ts +2 -2
- package/dist/extensions/payment-requests.d.mts +2 -2
- package/dist/extensions/payment-requests.d.ts +2 -2
- package/dist/extensions/products.d.mts +2 -2
- package/dist/extensions/products.d.ts +2 -2
- package/dist/extensions/rain.d.mts +2 -2
- package/dist/extensions/rain.d.ts +2 -2
- package/dist/extensions/rewards.d.mts +2 -2
- package/dist/extensions/rewards.d.ts +2 -2
- package/dist/extensions/stellar-testnet.d.mts +2 -2
- package/dist/extensions/stellar-testnet.d.ts +2 -2
- package/dist/extensions/stellar.d.mts +2 -2
- package/dist/extensions/stellar.d.ts +2 -2
- package/dist/index.d.mts +2 -2
- package/dist/index.d.ts +2 -2
- package/dist/index.js +1 -1
- package/dist/index.mjs +1 -1
- package/dist/react.d.mts +11 -3
- package/dist/react.d.ts +11 -3
- package/dist/react.js +1 -1
- package/dist/react.mjs +1 -1
- package/dist/user.d.mts +3 -3
- package/dist/user.d.ts +3 -3
- package/package.json +3 -3
- package/src/auth/create-auth.ts +774 -170
- package/src/auth/index.ts +25 -2
- package/src/auth/types/index.ts +48 -0
- package/dist/chunk-OV77OD2G.js +0 -1
- package/dist/chunk-RO2QGTSG.mjs +0 -1
package/src/auth/index.ts
CHANGED
|
@@ -1,4 +1,27 @@
|
|
|
1
1
|
export { createAuth } from './create-auth.js';
|
|
2
|
-
export type {
|
|
3
|
-
|
|
2
|
+
export type {
|
|
3
|
+
Auth,
|
|
4
|
+
AuthConfig,
|
|
5
|
+
LoginParams,
|
|
6
|
+
RegisterParams,
|
|
7
|
+
RegisterCompanyParams,
|
|
8
|
+
ImportTokenOptions,
|
|
9
|
+
ValidateSessionOptions,
|
|
10
|
+
SessionPatch,
|
|
11
|
+
} from './create-auth.js';
|
|
12
|
+
export type {
|
|
13
|
+
AuthSession,
|
|
14
|
+
UserSession,
|
|
15
|
+
AuthState,
|
|
16
|
+
AuthStatus,
|
|
17
|
+
AuthRecoveryState,
|
|
18
|
+
AuthSnapshot,
|
|
19
|
+
AuthEvent,
|
|
20
|
+
AuthEventType,
|
|
21
|
+
SessionListener,
|
|
22
|
+
ErrorListener,
|
|
23
|
+
AuthStateListener,
|
|
24
|
+
AuthEventListener,
|
|
25
|
+
StorageAdapter,
|
|
26
|
+
} from './types/index.js';
|
|
4
27
|
export { WebStorageAdapter, MemoryStorageAdapter, AsyncStorageAdapter } from './core/storage-adapters.js';
|
package/src/auth/types/index.ts
CHANGED
|
@@ -24,5 +24,53 @@ export interface AuthState {
|
|
|
24
24
|
activeSessionIndex: number;
|
|
25
25
|
}
|
|
26
26
|
|
|
27
|
+
export type AuthStatus =
|
|
28
|
+
| 'loading'
|
|
29
|
+
| 'authenticated'
|
|
30
|
+
| 'refreshing'
|
|
31
|
+
| 'recoverable'
|
|
32
|
+
| 'unauthenticated';
|
|
33
|
+
|
|
34
|
+
export interface AuthRecoveryState {
|
|
35
|
+
pending: boolean;
|
|
36
|
+
expiredSession: AuthSession | null;
|
|
37
|
+
remainingSessions: AuthSession[];
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export interface AuthSnapshot {
|
|
41
|
+
status: AuthStatus;
|
|
42
|
+
session: AuthSession | null;
|
|
43
|
+
sessions: AuthSession[];
|
|
44
|
+
activeSessionIndex: number;
|
|
45
|
+
isRefreshing: boolean;
|
|
46
|
+
initialized: boolean;
|
|
47
|
+
error: Error | null;
|
|
48
|
+
recovery: AuthRecoveryState;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
export type AuthEventType =
|
|
52
|
+
| 'initialized'
|
|
53
|
+
| 'login'
|
|
54
|
+
| 'register'
|
|
55
|
+
| 'register-company'
|
|
56
|
+
| 'logout'
|
|
57
|
+
| 'logout-all'
|
|
58
|
+
| 'refresh'
|
|
59
|
+
| 'session-imported'
|
|
60
|
+
| 'session-updated'
|
|
61
|
+
| 'session-switched'
|
|
62
|
+
| 'session-expired'
|
|
63
|
+
| 'session-cleared'
|
|
64
|
+
| 'error';
|
|
65
|
+
|
|
66
|
+
export interface AuthEvent {
|
|
67
|
+
type: AuthEventType;
|
|
68
|
+
snapshot: AuthSnapshot;
|
|
69
|
+
session?: AuthSession | null;
|
|
70
|
+
error?: Error | null;
|
|
71
|
+
}
|
|
72
|
+
|
|
27
73
|
export type SessionListener = (session: AuthSession | null) => void;
|
|
28
74
|
export type ErrorListener = (error: Error | null) => void;
|
|
75
|
+
export type AuthStateListener = (snapshot: AuthSnapshot) => void;
|
|
76
|
+
export type AuthEventListener = (event: AuthEvent) => void;
|
package/dist/chunk-OV77OD2G.js
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _nullishCoalesce(lhs, rhsFn) { if (lhs != null) { return lhs; } else { return rhsFn(); } } function _optionalChain(ops) { let lastAccessLHS = undefined; let value = ops[0]; let i = 1; while (i < ops.length) { const op = ops[i]; const fn = ops[i + 1]; i += 2; if ((op === 'optionalAccess' || op === 'optionalCall') && value == null) { return undefined; } if (op === 'access' || op === 'optionalAccess') { lastAccessLHS = value; value = fn(value); } else if (op === 'call' || op === 'optionalCall') { value = fn((...args) => value.call(lastAccessLHS, ...args)); lastAccessLHS = undefined; } } return value; }var _chunkCRNPJD3Ejs = require('./chunk-CRNPJD3E.js');var _chunkOEIQFP6Tjs = require('./chunk-OEIQFP6T.js');var R=class{async getItem(o){return typeof window<"u"&&window.localStorage?window.localStorage.getItem(o):null}async setItem(o,c){typeof window<"u"&&window.localStorage&&window.localStorage.setItem(o,c)}async removeItem(o){typeof window<"u"&&window.localStorage&&window.localStorage.removeItem(o)}},_= exports.b =class{constructor(){this.storage=new Map}async getItem(o){return this.storage.get(o)||null}async setItem(o,c){this.storage.set(o,c)}async removeItem(o){this.storage.delete(o)}},$= exports.c =class{constructor(o){this.asyncStorage=o}async getItem(o){return this.asyncStorage.getItem(o)}async setItem(o,c){return this.asyncStorage.setItem(o,c)}async removeItem(o){return this.asyncStorage.removeItem(o)}};function ee(a){return!a||a==="local"?typeof window<"u"&&window.localStorage?new R:new _:a==="memory"?new _:a}function se(a){return async(...o)=>{let c=await a(...o);if(!c.ok){let g=await c.text(),h=null;try{h=JSON.parse(g)}catch (e2){}throw new (0, _chunkOEIQFP6Tjs.b)({status:c.status,error:h||g,message:_optionalChain([h, 'optionalAccess', _2 => _2.error])||_optionalChain([h, 'optionalAccess', _3 => _3.message])||"A server error occurred. HTTPStatus: "+c.status})}return c}}function ae(a={}){let o=a.baseUrl||"https://api.rehive.com",c=ee(a.storage),g=a.token,h=_nullishCoalesce(a.enableCrossTabSync, () => (!0)),E="rehive_auth_state",f=_chunkCRNPJD3Ejs.a.call(void 0, {baseUrl:o,responseStyle:"data",fetch:se(_chunkOEIQFP6Tjs.c.call(void 0, globalThis.fetch))}),y=[],S=-1,I=[],k=[],w=null,q=!1,v=null,W=!1;function z(e){return Date.now()>=e-30*1e3}function A(){return S>=0&&S<y.length?y[S]:null}function C(){let e=A();I.forEach(s=>s(e))}function l(e){k.forEach(s=>s(e))}async function d(){if(v)return v;v=(async()=>{let e={sessions:[],activeSessionIndex:-1};try{let s=await c.getItem(E);if(s){let t=JSON.parse(s);e.sessions=Array.isArray(t.sessions)?t.sessions:[],e.activeSessionIndex=typeof t.activeSessionIndex=="number"?t.activeSessionIndex:-1}}catch(s){console.error("Failed to load auth state:",s)}return y=e.sessions,S=e.activeSessionIndex,e})();try{return await v}finally{v=null}}async function m(e){try{await c.setItem(E,JSON.stringify(e)),y=e.sessions,S=e.activeSessionIndex,C()}catch(s){console.error("Failed to save auth state:",s)}}function B(){typeof window<"u"&&window.addEventListener("storage",e=>{if(e.key===E&&e.newValue)try{let s=JSON.parse(e.newValue);y=Array.isArray(s.sessions)?s.sessions:[],S=typeof s.activeSessionIndex=="number"?s.activeSessionIndex:-1,C()}catch(s){console.error("Failed to sync auth state from storage event:",s)}})}async function x(){W||(W=!0,g||(h&&B(),await d(),C()))}g||x().catch(console.error);async function H(e){await x();try{let s=e.session_duration===null?void 0:_nullishCoalesce(e.session_duration, () => (900)),t={user:e.user,password:e.password,company:e.company,...s!=null&&{session_duration:s},auth_method:"token"},r=await _chunkCRNPJD3Ejs.c.call(void 0, {client:f,body:t,throwOnError:!0}),n=_nullishCoalesce(_optionalChain([r, 'optionalAccess', _4 => _4.data]), () => (r)),i={user:n.user,token:n.token,refresh_token:n.refresh_token,challenges:n.challenges,expires:n.expires,session_duration:_nullishCoalesce(s, () => (900)),company:e.company},u=await d(),P=u.sessions.findIndex(b=>b.user.id===i.user.id&&b.company===i.company),T;if(P!==-1){let b=[...u.sessions];b[P]=i,T={...u,sessions:b,activeSessionIndex:P}}else T={sessions:[...u.sessions,i],activeSessionIndex:u.sessions.length};return await m(T),l(null),i}catch(s){let t=s instanceof _chunkOEIQFP6Tjs.b?s:new Error("Login failed");throw l(t),s}}async function V(e){await x();try{let s=e.session_duration===null?void 0:_nullishCoalesce(e.session_duration, () => (900)),t={email:e.email,mobile:e.mobile,username:e.username,company:e.company,password1:e.password,password2:e.password,first_name:e.first_name,last_name:e.last_name,terms_and_conditions:e.terms_and_conditions,privacy_policy:e.privacy_policy,...s!=null&&{session_duration:s}},r=await _chunkCRNPJD3Ejs.f.call(void 0, {client:f,body:t,throwOnError:!0}),n=_nullishCoalesce(_optionalChain([r, 'optionalAccess', _5 => _5.data]), () => (r)),i={user:n.user,token:n.token,refresh_token:n.refresh_token,challenges:n.challenges,expires:n.expires,session_duration:_nullishCoalesce(s, () => (900)),company:e.company},u=await d(),P={sessions:[...u.sessions,i],activeSessionIndex:u.sessions.length};return await m(P),l(null),i}catch(s){let t=s instanceof _chunkOEIQFP6Tjs.b?s:new Error("Registration failed");throw l(t),s}}async function K(e){await x();try{let t=await _chunkCRNPJD3Ejs.b.call(void 0, {client:f,body:e,throwOnError:!0}),r=_nullishCoalesce(_optionalChain([t, 'optionalAccess', _6 => _6.data]), () => (t)),n={user:r.user,token:r.token,refresh_token:r.refresh_token,challenges:r.challenges,expires:r.expires,session_duration:900,company:typeof e.company=="string"?e.company:_optionalChain([e, 'access', _7 => _7.company, 'optionalAccess', _8 => _8.id])},i=await d(),u={sessions:[...i.sessions,n],activeSessionIndex:i.sessions.length};return await m(u),l(null),n}catch(s){let t=s instanceof _chunkOEIQFP6Tjs.b?s:new Error("Company registration failed");throw l(t),s}}async function M(){let e=await d(),s=e.sessions[e.activeSessionIndex],t={...e,sessions:e.sessions.filter((r,n)=>n!==e.activeSessionIndex),activeSessionIndex:-1};if(_optionalChain([s, 'optionalAccess', _9 => _9.token]))try{await _chunkCRNPJD3Ejs.d.call(void 0, {client:f,body:{clear_session_option:"none"},headers:{Authorization:`Token ${s.token}`},throwOnError:!0})}catch (e3){}await m(t),l(null)}async function G(){let e=await d();await Promise.all(e.sessions.map(async s=>{try{await _chunkCRNPJD3Ejs.d.call(void 0, {client:f,body:{clear_session_option:"none"},headers:{Authorization:`Token ${s.token}`},throwOnError:!0})}catch (e4){}})),await m({sessions:[],activeSessionIndex:-1}),l(null)}async function L(){return w||(w=(async()=>{q=!0;try{let e=await d(),s=e.activeSessionIndex,t=e.sessions[s];if(!_optionalChain([t, 'optionalAccess', _10 => _10.token])||!_optionalChain([t, 'optionalAccess', _11 => _11.refresh_token]))throw new Error("No active session, token, or refresh token found");let r=_nullishCoalesce(t.session_duration, () => (900)),n=await _chunkCRNPJD3Ejs.e.call(void 0, {client:f,body:{...r!=null&&{session_duration:r}},headers:{Authorization:`Refresh-Token ${t.refresh_token}`},throwOnError:!0}),i=_nullishCoalesce(_optionalChain([n, 'optionalAccess', _12 => _12.data]), () => (n)),u=[...e.sessions];u[s]={...t,refresh_token:i.refresh_token,expires:i.expires,session_duration:r,company:t.company},await m({...e,sessions:u}),l(null)}catch(e){let s=e instanceof _chunkOEIQFP6Tjs.b?e:new Error("Refresh failed");l(s);let t=await d();throw await m({...t,sessions:t.sessions.filter((r,n)=>n!==t.activeSessionIndex),activeSessionIndex:-1}),e}finally{q=!1,w=null}})(),w)}async function Q(){if(g)return g;await x();let e=A();if(e){if(e.expires&&z(e.expires)){try{await L()}catch (e5){return}return _optionalChain([A, 'call', _13 => _13(), 'optionalAccess', _14 => _14.token])}return e.token}}async function X(e,s){let t=await d(),r=t.sessions.findIndex(i=>i.user.id===e&&i.company===s);if(r===-1)return null;let n=t.sessions[r];return await m({...t,activeSessionIndex:r}),n.expires&&z(n.expires)?(await L(),A()):n}async function Y(){await m({sessions:[],activeSessionIndex:-1}),l(null)}async function Z(e){let s=await d(),t=s.activeSessionIndex;if(t<0||t>=s.sessions.length)return;let r=s.sessions[t],n=[...s.sessions];n[t]={...r,challenges:_optionalChain([r, 'access', _15 => _15.challenges, 'optionalAccess', _16 => _16.filter, 'call', _17 => _17(i=>i.id!==e)])||[]},await m({...s,sessions:n})}return{login:H,register:V,registerCompany:K,logout:M,logoutAll:G,refresh:L,getToken:Q,getActiveSession:A,getSessions:()=>[...y],getSessionsByCompany:e=>y.filter(s=>s.company===e),switchToSession:X,clearAllSessions:Y,deleteChallenge:Z,subscribe(e){return I.push(e),()=>{I=I.filter(s=>s!==e)}},subscribeToErrors(e){return k.push(e),()=>{k=k.filter(s=>s!==e)}},get baseUrl(){return o}}}exports.a = R; exports.b = _; exports.c = $; exports.d = ae;
|
package/dist/chunk-RO2QGTSG.mjs
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
import{a as F,b as J,c as N,d as O,e as U,f as D}from"./chunk-TAABJLJG.mjs";import{b as p,c as j}from"./chunk-S4QKN5WW.mjs";var R=class{async getItem(o){return typeof window<"u"&&window.localStorage?window.localStorage.getItem(o):null}async setItem(o,c){typeof window<"u"&&window.localStorage&&window.localStorage.setItem(o,c)}async removeItem(o){typeof window<"u"&&window.localStorage&&window.localStorage.removeItem(o)}},_=class{constructor(){this.storage=new Map}async getItem(o){return this.storage.get(o)||null}async setItem(o,c){this.storage.set(o,c)}async removeItem(o){this.storage.delete(o)}},$=class{constructor(o){this.asyncStorage=o}async getItem(o){return this.asyncStorage.getItem(o)}async setItem(o,c){return this.asyncStorage.setItem(o,c)}async removeItem(o){return this.asyncStorage.removeItem(o)}};function ee(a){return!a||a==="local"?typeof window<"u"&&window.localStorage?new R:new _:a==="memory"?new _:a}function se(a){return async(...o)=>{let c=await a(...o);if(!c.ok){let g=await c.text(),h=null;try{h=JSON.parse(g)}catch{}throw new p({status:c.status,error:h||g,message:h?.error||h?.message||"A server error occurred. HTTPStatus: "+c.status})}return c}}function ae(a={}){let o=a.baseUrl||"https://api.rehive.com",c=ee(a.storage),g=a.token,h=a.enableCrossTabSync??!0,E="rehive_auth_state",f=F({baseUrl:o,responseStyle:"data",fetch:se(j(globalThis.fetch))}),y=[],S=-1,I=[],k=[],w=null,q=!1,v=null,W=!1;function z(e){return Date.now()>=e-30*1e3}function A(){return S>=0&&S<y.length?y[S]:null}function C(){let e=A();I.forEach(s=>s(e))}function l(e){k.forEach(s=>s(e))}async function d(){if(v)return v;v=(async()=>{let e={sessions:[],activeSessionIndex:-1};try{let s=await c.getItem(E);if(s){let t=JSON.parse(s);e.sessions=Array.isArray(t.sessions)?t.sessions:[],e.activeSessionIndex=typeof t.activeSessionIndex=="number"?t.activeSessionIndex:-1}}catch(s){console.error("Failed to load auth state:",s)}return y=e.sessions,S=e.activeSessionIndex,e})();try{return await v}finally{v=null}}async function m(e){try{await c.setItem(E,JSON.stringify(e)),y=e.sessions,S=e.activeSessionIndex,C()}catch(s){console.error("Failed to save auth state:",s)}}function B(){typeof window<"u"&&window.addEventListener("storage",e=>{if(e.key===E&&e.newValue)try{let s=JSON.parse(e.newValue);y=Array.isArray(s.sessions)?s.sessions:[],S=typeof s.activeSessionIndex=="number"?s.activeSessionIndex:-1,C()}catch(s){console.error("Failed to sync auth state from storage event:",s)}})}async function x(){W||(W=!0,g||(h&&B(),await d(),C()))}g||x().catch(console.error);async function H(e){await x();try{let s=e.session_duration===null?void 0:e.session_duration??900,t={user:e.user,password:e.password,company:e.company,...s!=null&&{session_duration:s},auth_method:"token"},r=await N({client:f,body:t,throwOnError:!0}),n=r?.data??r,i={user:n.user,token:n.token,refresh_token:n.refresh_token,challenges:n.challenges,expires:n.expires,session_duration:s??900,company:e.company},u=await d(),P=u.sessions.findIndex(b=>b.user.id===i.user.id&&b.company===i.company),T;if(P!==-1){let b=[...u.sessions];b[P]=i,T={...u,sessions:b,activeSessionIndex:P}}else T={sessions:[...u.sessions,i],activeSessionIndex:u.sessions.length};return await m(T),l(null),i}catch(s){let t=s instanceof p?s:new Error("Login failed");throw l(t),s}}async function V(e){await x();try{let s=e.session_duration===null?void 0:e.session_duration??900,t={email:e.email,mobile:e.mobile,username:e.username,company:e.company,password1:e.password,password2:e.password,first_name:e.first_name,last_name:e.last_name,terms_and_conditions:e.terms_and_conditions,privacy_policy:e.privacy_policy,...s!=null&&{session_duration:s}},r=await D({client:f,body:t,throwOnError:!0}),n=r?.data??r,i={user:n.user,token:n.token,refresh_token:n.refresh_token,challenges:n.challenges,expires:n.expires,session_duration:s??900,company:e.company},u=await d(),P={sessions:[...u.sessions,i],activeSessionIndex:u.sessions.length};return await m(P),l(null),i}catch(s){let t=s instanceof p?s:new Error("Registration failed");throw l(t),s}}async function K(e){await x();try{let t=await J({client:f,body:e,throwOnError:!0}),r=t?.data??t,n={user:r.user,token:r.token,refresh_token:r.refresh_token,challenges:r.challenges,expires:r.expires,session_duration:900,company:typeof e.company=="string"?e.company:e.company?.id},i=await d(),u={sessions:[...i.sessions,n],activeSessionIndex:i.sessions.length};return await m(u),l(null),n}catch(s){let t=s instanceof p?s:new Error("Company registration failed");throw l(t),s}}async function M(){let e=await d(),s=e.sessions[e.activeSessionIndex],t={...e,sessions:e.sessions.filter((r,n)=>n!==e.activeSessionIndex),activeSessionIndex:-1};if(s?.token)try{await O({client:f,body:{clear_session_option:"none"},headers:{Authorization:`Token ${s.token}`},throwOnError:!0})}catch{}await m(t),l(null)}async function G(){let e=await d();await Promise.all(e.sessions.map(async s=>{try{await O({client:f,body:{clear_session_option:"none"},headers:{Authorization:`Token ${s.token}`},throwOnError:!0})}catch{}})),await m({sessions:[],activeSessionIndex:-1}),l(null)}async function L(){return w||(w=(async()=>{q=!0;try{let e=await d(),s=e.activeSessionIndex,t=e.sessions[s];if(!t?.token||!t?.refresh_token)throw new Error("No active session, token, or refresh token found");let r=t.session_duration??900,n=await U({client:f,body:{...r!=null&&{session_duration:r}},headers:{Authorization:`Refresh-Token ${t.refresh_token}`},throwOnError:!0}),i=n?.data??n,u=[...e.sessions];u[s]={...t,refresh_token:i.refresh_token,expires:i.expires,session_duration:r,company:t.company},await m({...e,sessions:u}),l(null)}catch(e){let s=e instanceof p?e:new Error("Refresh failed");l(s);let t=await d();throw await m({...t,sessions:t.sessions.filter((r,n)=>n!==t.activeSessionIndex),activeSessionIndex:-1}),e}finally{q=!1,w=null}})(),w)}async function Q(){if(g)return g;await x();let e=A();if(e){if(e.expires&&z(e.expires)){try{await L()}catch{return}return A()?.token}return e.token}}async function X(e,s){let t=await d(),r=t.sessions.findIndex(i=>i.user.id===e&&i.company===s);if(r===-1)return null;let n=t.sessions[r];return await m({...t,activeSessionIndex:r}),n.expires&&z(n.expires)?(await L(),A()):n}async function Y(){await m({sessions:[],activeSessionIndex:-1}),l(null)}async function Z(e){let s=await d(),t=s.activeSessionIndex;if(t<0||t>=s.sessions.length)return;let r=s.sessions[t],n=[...s.sessions];n[t]={...r,challenges:r.challenges?.filter(i=>i.id!==e)||[]},await m({...s,sessions:n})}return{login:H,register:V,registerCompany:K,logout:M,logoutAll:G,refresh:L,getToken:Q,getActiveSession:A,getSessions:()=>[...y],getSessionsByCompany:e=>y.filter(s=>s.company===e),switchToSession:X,clearAllSessions:Y,deleteChallenge:Z,subscribe(e){return I.push(e),()=>{I=I.filter(s=>s!==e)}},subscribeToErrors(e){return k.push(e),()=>{k=k.filter(s=>s!==e)}},get baseUrl(){return o}}}export{R as a,_ as b,$ as c,ae as d};
|