@emblemvault/auth-sdk 2.3.2 → 2.3.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +56 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -103,6 +103,17 @@ Retrieves or generates the vault API key.
|
|
|
103
103
|
#### `logout(): void`
|
|
104
104
|
Clears the current session.
|
|
105
105
|
|
|
106
|
+
#### `hydrateSession(session: AuthSession): void`
|
|
107
|
+
Restores a session from external storage. Useful for Node.js environments where `localStorage` is not available. This activates the auto-refresh timer.
|
|
108
|
+
|
|
109
|
+
```typescript
|
|
110
|
+
// Restore session from file/database
|
|
111
|
+
const saved = await loadSessionFromFile();
|
|
112
|
+
if (saved) {
|
|
113
|
+
auth.hydrateSession(saved);
|
|
114
|
+
}
|
|
115
|
+
```
|
|
116
|
+
|
|
106
117
|
#### `on(event, handler): void`
|
|
107
118
|
Subscribe to authentication events.
|
|
108
119
|
|
|
@@ -143,6 +154,51 @@ const auth = new EmblemAuthSDK({
|
|
|
143
154
|
- Calling `logout()` clears the persisted session
|
|
144
155
|
- Works gracefully in private browsing mode (falls back to memory-only)
|
|
145
156
|
|
|
157
|
+
### Node.js Usage
|
|
158
|
+
|
|
159
|
+
In Node.js environments, `localStorage` is not available, so you must implement your own session persistence. The SDK's auto-refresh timer still works—use `hydrateSession()` to restore sessions and subscribe to events to keep your storage in sync.
|
|
160
|
+
|
|
161
|
+
```typescript
|
|
162
|
+
import { EmblemAuthSDK } from '@emblemvault/auth-sdk';
|
|
163
|
+
import fs from 'fs/promises';
|
|
164
|
+
|
|
165
|
+
const SESSION_FILE = './session.json';
|
|
166
|
+
|
|
167
|
+
const auth = new EmblemAuthSDK({
|
|
168
|
+
appId: 'your-app-id',
|
|
169
|
+
persistSession: false // No effect in Node.js, but explicit
|
|
170
|
+
});
|
|
171
|
+
|
|
172
|
+
// Restore session from file on startup
|
|
173
|
+
async function restoreSession() {
|
|
174
|
+
try {
|
|
175
|
+
const data = await fs.readFile(SESSION_FILE, 'utf-8');
|
|
176
|
+
const session = JSON.parse(data);
|
|
177
|
+
auth.hydrateSession(session); // Activates auto-refresh timer
|
|
178
|
+
} catch {
|
|
179
|
+
// No saved session
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
// Keep file in sync with SDK's auto-refresh
|
|
184
|
+
auth.on('sessionRefreshed', async (session) => {
|
|
185
|
+
await fs.writeFile(SESSION_FILE, JSON.stringify(session));
|
|
186
|
+
});
|
|
187
|
+
|
|
188
|
+
auth.on('sessionExpired', async () => {
|
|
189
|
+
await fs.unlink(SESSION_FILE).catch(() => {});
|
|
190
|
+
});
|
|
191
|
+
|
|
192
|
+
// Initialize
|
|
193
|
+
await restoreSession();
|
|
194
|
+
```
|
|
195
|
+
|
|
196
|
+
**Key points:**
|
|
197
|
+
- `hydrateSession()` restores the session and starts the auto-refresh timer
|
|
198
|
+
- The SDK will automatically refresh tokens ~60 seconds before expiry
|
|
199
|
+
- Subscribe to `sessionRefreshed` to persist the new session after refresh
|
|
200
|
+
- Subscribe to `sessionExpired` to clear your storage when refresh fails
|
|
201
|
+
|
|
146
202
|
### Types
|
|
147
203
|
|
|
148
204
|
#### `VaultInfo`
|