@emblemvault/auth-sdk 2.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +180 -0
- package/dist/EmblemAuthSDK.d.ts +150 -0
- package/dist/EmblemAuthSDK.d.ts.map +1 -0
- package/dist/SessionManager.d.ts +57 -0
- package/dist/SessionManager.d.ts.map +1 -0
- package/dist/emblem-auth.min.js +15 -0
- package/dist/emblem-auth.min.js.map +1 -0
- package/dist/emblem-auth.umd.js +15 -0
- package/dist/emblem-auth.umd.js.map +1 -0
- package/dist/index.d.ts +8 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +2 -0
- package/dist/index.js.map +1 -0
- package/dist/index.mjs +2 -0
- package/dist/index.mjs.map +1 -0
- package/dist/signers/ethers.d.ts +20 -0
- package/dist/signers/ethers.d.ts.map +1 -0
- package/dist/signers/ethers.js +2 -0
- package/dist/signers/ethers.js.map +1 -0
- package/dist/signers/ethers.mjs +2 -0
- package/dist/signers/ethers.mjs.map +1 -0
- package/dist/signers/http.d.ts +4 -0
- package/dist/signers/http.d.ts.map +1 -0
- package/dist/signers/index.d.ts +9 -0
- package/dist/signers/index.d.ts.map +1 -0
- package/dist/signers/index.js +2 -0
- package/dist/signers/index.js.map +1 -0
- package/dist/signers/index.mjs +2 -0
- package/dist/signers/index.mjs.map +1 -0
- package/dist/signers/solana.d.ts +32 -0
- package/dist/signers/solana.d.ts.map +1 -0
- package/dist/signers/solana.js +2 -0
- package/dist/signers/solana.js.map +1 -0
- package/dist/signers/solana.mjs +2 -0
- package/dist/signers/solana.mjs.map +1 -0
- package/dist/signers/utils.d.ts +10 -0
- package/dist/signers/utils.d.ts.map +1 -0
- package/dist/signers/validation.d.ts +43 -0
- package/dist/signers/validation.d.ts.map +1 -0
- package/dist/signers/vault.d.ts +3 -0
- package/dist/signers/vault.d.ts.map +1 -0
- package/dist/signers/viem.d.ts +15 -0
- package/dist/signers/viem.d.ts.map +1 -0
- package/dist/signers/viem.js +2 -0
- package/dist/signers/viem.js.map +1 -0
- package/dist/signers/viem.mjs +2 -0
- package/dist/signers/viem.mjs.map +1 -0
- package/dist/signers/web3.d.ts +13 -0
- package/dist/signers/web3.d.ts.map +1 -0
- package/dist/signers/web3.js +2 -0
- package/dist/signers/web3.js.map +1 -0
- package/dist/signers/web3.mjs +2 -0
- package/dist/signers/web3.mjs.map +1 -0
- package/dist/types/auth.d.ts +96 -0
- package/dist/types/auth.d.ts.map +1 -0
- package/dist/types/config.d.ts +43 -0
- package/dist/types/config.d.ts.map +1 -0
- package/dist/types/events.d.ts +27 -0
- package/dist/types/events.d.ts.map +1 -0
- package/dist/types/index.d.ts +6 -0
- package/dist/types/index.d.ts.map +1 -0
- package/dist/types/session.d.ts +63 -0
- package/dist/types/session.d.ts.map +1 -0
- package/dist/types/signers.d.ts +37 -0
- package/dist/types/signers.d.ts.map +1 -0
- package/package.json +145 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Emblem Vault
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,180 @@
|
|
|
1
|
+
# @emblemvault/auth-sdk
|
|
2
|
+
|
|
3
|
+
Official TypeScript SDK for Emblem Vault authentication.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install @emblemvault/auth-sdk
|
|
9
|
+
# or
|
|
10
|
+
yarn add @emblemvault/auth-sdk
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
## Quick Start
|
|
14
|
+
|
|
15
|
+
```typescript
|
|
16
|
+
import { EmblemAuthSDK } from '@emblemvault/auth-sdk';
|
|
17
|
+
|
|
18
|
+
// Minimal setup - only appId is required
|
|
19
|
+
const auth = new EmblemAuthSDK({
|
|
20
|
+
appId: 'your-app-id',
|
|
21
|
+
onSuccess: (session) => {
|
|
22
|
+
console.log('Authenticated!', session);
|
|
23
|
+
},
|
|
24
|
+
onError: (error) => {
|
|
25
|
+
console.error('Auth failed:', error);
|
|
26
|
+
}
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
// Open authentication modal
|
|
30
|
+
auth.openAuthModal();
|
|
31
|
+
|
|
32
|
+
// Get current session
|
|
33
|
+
const session = auth.getSession();
|
|
34
|
+
|
|
35
|
+
// Use JWT for API calls
|
|
36
|
+
fetch('https://api.emblemvault.ai/protected-endpoint', {
|
|
37
|
+
headers: {
|
|
38
|
+
'Authorization': `Bearer ${session.authToken}`
|
|
39
|
+
}
|
|
40
|
+
});
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
## Features
|
|
44
|
+
|
|
45
|
+
- 🔐 **Multiple Auth Methods** - Wallet (EVM, Solana, Hedera) and OAuth (Twitter, Google)
|
|
46
|
+
- 🔑 **JWT-based Sessions** - Secure token management with auto-refresh
|
|
47
|
+
- 💾 **Session Persistence** - Stay logged in across page reloads (enabled by default)
|
|
48
|
+
- 🎨 **Flexible UI** - Iframe or popup modal modes
|
|
49
|
+
- 📦 **TypeScript** - Full type definitions included
|
|
50
|
+
- 🌐 **Browser & Node** - Works in browsers and Node.js environments
|
|
51
|
+
- ⚡ **Lightweight** - < 15KB minified and gzipped
|
|
52
|
+
|
|
53
|
+
## API Reference
|
|
54
|
+
|
|
55
|
+
### Constructor
|
|
56
|
+
|
|
57
|
+
```typescript
|
|
58
|
+
new EmblemAuthSDK(config: EmblemAuthConfig)
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
#### Config Options
|
|
62
|
+
|
|
63
|
+
| Option | Type | Required | Default | Description |
|
|
64
|
+
|--------|------|----------|---------|-------------|
|
|
65
|
+
| `appId` | string | ✅ | - | Your application ID |
|
|
66
|
+
| `authUrl` | string | ❌ | `https://auth.emblemvault.ai` | Auth service URL for modal, init, bootstrap, refresh |
|
|
67
|
+
| `apiUrl` | string | ❌ | `https://api.emblemvault.ai` | Backend API for vault operations and signing only |
|
|
68
|
+
| `modalMode` | 'iframe' \| 'popup' \| 'auto' | ❌ | `'auto'` | Modal display mode |
|
|
69
|
+
| `persistSession` | boolean | ❌ | `true` | Persist session to localStorage (stay logged in) |
|
|
70
|
+
| `onSuccess` | (session) => void | ❌ | - | Success callback |
|
|
71
|
+
| `onError` | (error) => void | ❌ | - | Error callback |
|
|
72
|
+
|
|
73
|
+
**URL Configuration:**
|
|
74
|
+
- `authUrl` handles: auth modal (`/connect`), `/api/auth/init`, `/auth/bootstrap`, `/api/auth/refresh`
|
|
75
|
+
- `apiUrl` handles: vault info, signing, decryption, and other vault operations
|
|
76
|
+
|
|
77
|
+
### Methods
|
|
78
|
+
|
|
79
|
+
#### `openAuthModal(): Promise<void>`
|
|
80
|
+
Opens the authentication modal for users to sign in.
|
|
81
|
+
|
|
82
|
+
#### `getSession(): AuthSession | null`
|
|
83
|
+
Returns the current authentication session.
|
|
84
|
+
|
|
85
|
+
#### `refreshSession(): Promise<AuthSession | null>`
|
|
86
|
+
Manually refreshes the authentication token.
|
|
87
|
+
|
|
88
|
+
#### `authenticateWallet(params): Promise<AuthSession | null>`
|
|
89
|
+
Programmatically authenticate with wallet signature.
|
|
90
|
+
|
|
91
|
+
#### `getVaultInfo(): Promise<VaultInfo>`
|
|
92
|
+
Retrieves information about the user's vault. Caches the result for the session duration.
|
|
93
|
+
|
|
94
|
+
```typescript
|
|
95
|
+
const vaultInfo = await auth.getVaultInfo();
|
|
96
|
+
console.log(vaultInfo.vaultId);
|
|
97
|
+
console.log(vaultInfo.evmAddress);
|
|
98
|
+
```
|
|
99
|
+
|
|
100
|
+
#### `getVaultApiKey(): Promise<string>`
|
|
101
|
+
Retrieves or generates the vault API key.
|
|
102
|
+
|
|
103
|
+
#### `logout(): void`
|
|
104
|
+
Clears the current session.
|
|
105
|
+
|
|
106
|
+
#### `on(event, handler): void`
|
|
107
|
+
Subscribe to authentication events.
|
|
108
|
+
|
|
109
|
+
#### `off(event, handler): void`
|
|
110
|
+
Unsubscribe from authentication events.
|
|
111
|
+
|
|
112
|
+
### Events
|
|
113
|
+
|
|
114
|
+
- `session` - Fired when session is established or updated
|
|
115
|
+
- `sessionExpired` - Session has expired
|
|
116
|
+
- `sessionRefreshed` - Session was refreshed
|
|
117
|
+
- `sessionWillRefresh` - Session will refresh soon
|
|
118
|
+
- `authError` - Authentication error occurred
|
|
119
|
+
|
|
120
|
+
### Session Persistence
|
|
121
|
+
|
|
122
|
+
By default, sessions are persisted to `localStorage`, allowing users to stay logged in across page reloads and browser sessions. This follows the industry-standard "stay logged in" behavior.
|
|
123
|
+
|
|
124
|
+
```typescript
|
|
125
|
+
// Default behavior - sessions are persisted
|
|
126
|
+
const auth = new EmblemAuthSDK({
|
|
127
|
+
appId: 'your-app-id'
|
|
128
|
+
});
|
|
129
|
+
|
|
130
|
+
// Sessions will automatically restore on page load
|
|
131
|
+
const session = auth.getSession(); // Returns persisted session if valid
|
|
132
|
+
|
|
133
|
+
// To disable persistence (session only lasts until page closes)
|
|
134
|
+
const auth = new EmblemAuthSDK({
|
|
135
|
+
appId: 'your-app-id',
|
|
136
|
+
persistSession: false
|
|
137
|
+
});
|
|
138
|
+
```
|
|
139
|
+
|
|
140
|
+
**Notes:**
|
|
141
|
+
- Sessions are stored with the key `emblem_session_{appId}`
|
|
142
|
+
- Expired sessions are automatically cleared on load
|
|
143
|
+
- Calling `logout()` clears the persisted session
|
|
144
|
+
- Works gracefully in private browsing mode (falls back to memory-only)
|
|
145
|
+
|
|
146
|
+
### Types
|
|
147
|
+
|
|
148
|
+
#### `VaultInfo`
|
|
149
|
+
```typescript
|
|
150
|
+
interface VaultInfo {
|
|
151
|
+
vaultId: string;
|
|
152
|
+
evmAddress?: string;
|
|
153
|
+
solanaAddress?: string;
|
|
154
|
+
hederaAccountId?: string;
|
|
155
|
+
createdAt?: string;
|
|
156
|
+
metadata?: Record<string, any>;
|
|
157
|
+
}
|
|
158
|
+
```
|
|
159
|
+
|
|
160
|
+
## CDN Usage
|
|
161
|
+
|
|
162
|
+
```html
|
|
163
|
+
<script src="https://unpkg.com/@emblemvault/auth-sdk@latest/dist/emblem-auth.min.js"></script>
|
|
164
|
+
<script>
|
|
165
|
+
// Only appId is required - authUrl and apiUrl have production defaults
|
|
166
|
+
const auth = new EmblemAuth.EmblemAuthSDK({
|
|
167
|
+
appId: 'your-app-id'
|
|
168
|
+
});
|
|
169
|
+
|
|
170
|
+
auth.openAuthModal();
|
|
171
|
+
</script>
|
|
172
|
+
```
|
|
173
|
+
|
|
174
|
+
Alternative CDNs:
|
|
175
|
+
- jsDelivr: `https://cdn.jsdelivr.net/npm/@emblemvault/auth-sdk@latest/dist/emblem-auth.min.js`
|
|
176
|
+
- unpkg (specific version): `https://unpkg.com/@emblemvault/auth-sdk@2.0.0/dist/emblem-auth.min.js`
|
|
177
|
+
|
|
178
|
+
## License
|
|
179
|
+
|
|
180
|
+
MIT
|
|
@@ -0,0 +1,150 @@
|
|
|
1
|
+
import type { EmblemAuthConfig, AuthSession, WalletAuthParams, AuthEventKey, AuthEventHandler, VaultInfo, Hex } from './types';
|
|
2
|
+
import { type EmblemEthersWallet } from './signers/ethers';
|
|
3
|
+
import { EmblemSolanaSigner } from './signers/solana';
|
|
4
|
+
import { EmblemWeb3Adapter } from './signers/web3';
|
|
5
|
+
export declare class EmblemAuthSDK {
|
|
6
|
+
private readonly config;
|
|
7
|
+
private session;
|
|
8
|
+
private pendingNonce;
|
|
9
|
+
private readonly messageHandler;
|
|
10
|
+
private overlayEl;
|
|
11
|
+
private _iframeEl;
|
|
12
|
+
private overlayCleanup;
|
|
13
|
+
private readonly sessionMgr;
|
|
14
|
+
private readonly events;
|
|
15
|
+
private _cachedVaultInfo;
|
|
16
|
+
private readonly storageKey;
|
|
17
|
+
constructor(config: EmblemAuthConfig);
|
|
18
|
+
/**
|
|
19
|
+
* Authenticate with a wallet signature (programmatic/headless auth)
|
|
20
|
+
* Uses the external verification endpoint for cross-origin compatibility
|
|
21
|
+
*/
|
|
22
|
+
authenticateWallet(params: WalletAuthParams): Promise<AuthSession | null>;
|
|
23
|
+
/**
|
|
24
|
+
* Open the authentication modal
|
|
25
|
+
*/
|
|
26
|
+
openAuthModal(): Promise<void>;
|
|
27
|
+
/**
|
|
28
|
+
* Get the current session
|
|
29
|
+
*/
|
|
30
|
+
getSession(): AuthSession | null;
|
|
31
|
+
/**
|
|
32
|
+
* Refresh the current session
|
|
33
|
+
* Uses the registration site's refresh endpoint with cookies for secure token rotation
|
|
34
|
+
*/
|
|
35
|
+
refreshSession(): Promise<AuthSession | null>;
|
|
36
|
+
/**
|
|
37
|
+
* Get vault information
|
|
38
|
+
*/
|
|
39
|
+
getVaultInfo(): Promise<VaultInfo>;
|
|
40
|
+
/**
|
|
41
|
+
* Get or create the vault API key
|
|
42
|
+
*/
|
|
43
|
+
getVaultApiKey(): Promise<string>;
|
|
44
|
+
/**
|
|
45
|
+
* Hydrate a previously saved session
|
|
46
|
+
*/
|
|
47
|
+
hydrateSession(session: AuthSession): void;
|
|
48
|
+
/**
|
|
49
|
+
* Logout and clear the session
|
|
50
|
+
*/
|
|
51
|
+
logout(): void;
|
|
52
|
+
/**
|
|
53
|
+
* Subscribe to auth events
|
|
54
|
+
*/
|
|
55
|
+
on<K extends AuthEventKey>(event: K, handler: AuthEventHandler<K>): void;
|
|
56
|
+
/**
|
|
57
|
+
* Unsubscribe from auth events
|
|
58
|
+
*/
|
|
59
|
+
off<K extends AuthEventKey>(event: K, handler: AuthEventHandler<K>): void;
|
|
60
|
+
/**
|
|
61
|
+
* Helper to get signer config and vault info
|
|
62
|
+
*/
|
|
63
|
+
private getSignerContext;
|
|
64
|
+
/**
|
|
65
|
+
* Create a viem-compatible account adapter for EVM signing
|
|
66
|
+
* Requires an active session with authToken
|
|
67
|
+
*
|
|
68
|
+
* @example
|
|
69
|
+
* ```typescript
|
|
70
|
+
* const account = await sdk.toViemAccount();
|
|
71
|
+
* const signature = await account.signMessage({ message: 'Hello' });
|
|
72
|
+
* ```
|
|
73
|
+
*/
|
|
74
|
+
toViemAccount(): Promise<{
|
|
75
|
+
address: Hex;
|
|
76
|
+
signMessage: (args: {
|
|
77
|
+
message: unknown;
|
|
78
|
+
}) => Promise<Hex>;
|
|
79
|
+
signTypedData: (typedData: unknown) => Promise<Hex>;
|
|
80
|
+
signTransaction: (tx: unknown, opts?: unknown) => Promise<Hex>;
|
|
81
|
+
type: 'local';
|
|
82
|
+
source: 'custom';
|
|
83
|
+
}>;
|
|
84
|
+
/**
|
|
85
|
+
* Create an ethers v6 compatible wallet adapter for EVM signing
|
|
86
|
+
* Requires an active session with authToken
|
|
87
|
+
*
|
|
88
|
+
* @param provider - Optional ethers Provider for transaction population
|
|
89
|
+
*
|
|
90
|
+
* @example
|
|
91
|
+
* ```typescript
|
|
92
|
+
* const wallet = await sdk.toEthersWallet(provider);
|
|
93
|
+
* const signature = await wallet.signMessage('Hello');
|
|
94
|
+
* const tx = await wallet.sendTransaction({ to, value });
|
|
95
|
+
* ```
|
|
96
|
+
*/
|
|
97
|
+
toEthersWallet(provider?: unknown | null): Promise<EmblemEthersWallet>;
|
|
98
|
+
/**
|
|
99
|
+
* Create a Web3.js compatible adapter for EVM signing
|
|
100
|
+
* Requires an active session with authToken
|
|
101
|
+
*
|
|
102
|
+
* @example
|
|
103
|
+
* ```typescript
|
|
104
|
+
* const adapter = await sdk.toWeb3Adapter();
|
|
105
|
+
* const signature = await adapter.signMessage('Hello');
|
|
106
|
+
* ```
|
|
107
|
+
*/
|
|
108
|
+
toWeb3Adapter(): Promise<EmblemWeb3Adapter>;
|
|
109
|
+
/**
|
|
110
|
+
* Create a Solana Web3.js compatible signer
|
|
111
|
+
* Requires an active session with authToken
|
|
112
|
+
*
|
|
113
|
+
* @example
|
|
114
|
+
* ```typescript
|
|
115
|
+
* const signer = await sdk.toSolanaWeb3Signer();
|
|
116
|
+
* const signature = await signer.signMessage('Hello');
|
|
117
|
+
* const signedTx = await signer.signTransaction(tx);
|
|
118
|
+
* ```
|
|
119
|
+
*/
|
|
120
|
+
toSolanaWeb3Signer(): Promise<EmblemSolanaSigner>;
|
|
121
|
+
/**
|
|
122
|
+
* Create a Solana Kit compatible signer
|
|
123
|
+
* Requires an active session with authToken
|
|
124
|
+
*
|
|
125
|
+
* @example
|
|
126
|
+
* ```typescript
|
|
127
|
+
* const signer = await sdk.toSolanaKitSigner();
|
|
128
|
+
* const signature = await signer.signMessage('Hello');
|
|
129
|
+
* ```
|
|
130
|
+
*/
|
|
131
|
+
toSolanaKitSigner(): Promise<EmblemSolanaSigner>;
|
|
132
|
+
/**
|
|
133
|
+
* Clean up event listeners
|
|
134
|
+
*/
|
|
135
|
+
destroy(): void;
|
|
136
|
+
private resolveModalUrl;
|
|
137
|
+
private onMessage;
|
|
138
|
+
private emit;
|
|
139
|
+
private emitError;
|
|
140
|
+
private popupFeatures;
|
|
141
|
+
private randomId;
|
|
142
|
+
private tryGetOriginFromConfig;
|
|
143
|
+
private getAuthInit;
|
|
144
|
+
private openIframeModal;
|
|
145
|
+
private closeOverlay;
|
|
146
|
+
private safeJson;
|
|
147
|
+
private loadPersistedSession;
|
|
148
|
+
private persistSession;
|
|
149
|
+
}
|
|
150
|
+
//# sourceMappingURL=EmblemAuthSDK.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"EmblemAuthSDK.d.ts","sourceRoot":"","sources":["../src/EmblemAuthSDK.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EACV,gBAAgB,EAChB,WAAW,EACX,gBAAgB,EAKhB,YAAY,EACZ,gBAAgB,EAChB,SAAS,EACT,GAAG,EAEJ,MAAM,SAAS,CAAC;AAIjB,OAAO,EAAkB,KAAK,kBAAkB,EAAE,MAAM,kBAAkB,CAAC;AAC3E,OAAO,EAAyC,kBAAkB,EAAE,MAAM,kBAAkB,CAAC;AAC7F,OAAO,EAAiB,iBAAiB,EAAE,MAAM,gBAAgB,CAAC;AAElE,qBAAa,aAAa;IACxB,OAAO,CAAC,QAAQ,CAAC,MAAM,CAAkF;IACzG,OAAO,CAAC,OAAO,CAA4B;IAC3C,OAAO,CAAC,YAAY,CAAuB;IAC3C,OAAO,CAAC,QAAQ,CAAC,cAAc,CAAgC;IAC/D,OAAO,CAAC,SAAS,CAA4B;IAC7C,OAAO,CAAC,SAAS,CAAkC;IACnD,OAAO,CAAC,cAAc,CAA6B;IACnD,OAAO,CAAC,QAAQ,CAAC,UAAU,CAAiB;IAC5C,OAAO,CAAC,QAAQ,CAAC,MAAM,CAA8D;IACrF,OAAO,CAAC,gBAAgB,CAA0B;IAClD,OAAO,CAAC,QAAQ,CAAC,UAAU,CAAS;gBAExB,MAAM,EAAE,gBAAgB;IAwCpC;;;OAGG;IACG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,cAAc,CAAC,QAAQ,CAAC,EAAE,OAAO,GAAG,IAAI,GAAG,OAAO,CAAC,kBAAkB,CAAC;IAK5E;;;;;;;;;OASG;IACG,aAAa,IAAI,OAAO,CAAC,iBAAiB,CAAC;IAKjD;;;;;;;;;;OAUG;IACG,kBAAkB,IAAI,OAAO,CAAC,kBAAkB,CAAC;IAKvD;;;;;;;;;OASG;IACG,iBAAiB,IAAI,OAAO,CAAC,kBAAkB,CAAC;IAKtD;;OAEG;IACH,OAAO,IAAI,IAAI;IASf,OAAO,CAAC,eAAe;IAavB,OAAO,CAAC,SAAS;IAqCjB,OAAO,CAAC,IAAI;IAaZ,OAAO,CAAC,SAAS;IAKjB,OAAO,CAAC,aAAa;IAQrB,OAAO,CAAC,QAAQ;IAYhB,OAAO,CAAC,sBAAsB;YAUhB,WAAW;IAuBzB,OAAO,CAAC,eAAe;IA0EvB,OAAO,CAAC,YAAY;YAgBN,QAAQ;IAQtB,OAAO,CAAC,oBAAoB;IAqB5B,OAAO,CAAC,cAAc;CAavB"}
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import type { AuthSession, SessionRefreshInfo } from './types/session';
|
|
2
|
+
interface SessionManagerConfig {
|
|
3
|
+
/**
|
|
4
|
+
* Callback to refresh an expiring session
|
|
5
|
+
*/
|
|
6
|
+
onRefresh?: (session: AuthSession) => Promise<AuthSession | null>;
|
|
7
|
+
/**
|
|
8
|
+
* Time before expiry to trigger refresh (milliseconds)
|
|
9
|
+
* @default 60000 (1 minute)
|
|
10
|
+
*/
|
|
11
|
+
refreshSkewMs?: number;
|
|
12
|
+
}
|
|
13
|
+
type SessionEventMap = {
|
|
14
|
+
session: (session: AuthSession | null) => void;
|
|
15
|
+
sessionExpired: (session: AuthSession) => void;
|
|
16
|
+
sessionRefreshed: (session: AuthSession) => void;
|
|
17
|
+
sessionWillRefresh: (info: SessionRefreshInfo) => void;
|
|
18
|
+
};
|
|
19
|
+
type SessionEventKey = keyof SessionEventMap;
|
|
20
|
+
type SessionEventHandler<K extends SessionEventKey> = SessionEventMap[K];
|
|
21
|
+
export declare class SessionManager {
|
|
22
|
+
private session;
|
|
23
|
+
private timer;
|
|
24
|
+
private readonly onRefresh?;
|
|
25
|
+
private readonly events;
|
|
26
|
+
private readonly refreshSkewMs;
|
|
27
|
+
constructor(config?: SessionManagerConfig);
|
|
28
|
+
/**
|
|
29
|
+
* Set the current session and schedule refresh
|
|
30
|
+
*/
|
|
31
|
+
setSession(session: AuthSession | null): void;
|
|
32
|
+
/**
|
|
33
|
+
* Get the current session
|
|
34
|
+
*/
|
|
35
|
+
getSession(): AuthSession | null;
|
|
36
|
+
/**
|
|
37
|
+
* Clear the current session
|
|
38
|
+
*/
|
|
39
|
+
clear(): void;
|
|
40
|
+
/**
|
|
41
|
+
* Subscribe to session events
|
|
42
|
+
*/
|
|
43
|
+
on<K extends SessionEventKey>(event: K, handler: SessionEventHandler<K>): void;
|
|
44
|
+
/**
|
|
45
|
+
* Unsubscribe from session events
|
|
46
|
+
*/
|
|
47
|
+
off<K extends SessionEventKey>(event: K, handler: SessionEventHandler<K>): void;
|
|
48
|
+
/**
|
|
49
|
+
* Destroy the session manager
|
|
50
|
+
*/
|
|
51
|
+
destroy(): void;
|
|
52
|
+
private emit;
|
|
53
|
+
private cancel;
|
|
54
|
+
private schedule;
|
|
55
|
+
}
|
|
56
|
+
export {};
|
|
57
|
+
//# sourceMappingURL=SessionManager.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"SessionManager.d.ts","sourceRoot":"","sources":["../src/SessionManager.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,WAAW,EAAE,kBAAkB,EAAE,MAAM,iBAAiB,CAAC;AAEvE,UAAU,oBAAoB;IAC5B;;OAEG;IACH,SAAS,CAAC,EAAE,CAAC,OAAO,EAAE,WAAW,KAAK,OAAO,CAAC,WAAW,GAAG,IAAI,CAAC,CAAC;IAElE;;;OAGG;IACH,aAAa,CAAC,EAAE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