stellar-drive 1.2.27 → 1.2.29
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 +145 -19
- package/dist/auth/loginGuard.d.ts.map +1 -1
- package/dist/auth/loginGuard.js +18 -24
- package/dist/auth/loginGuard.js.map +1 -1
- package/dist/auth/offlineCredentials.d.ts +3 -5
- package/dist/auth/offlineCredentials.d.ts.map +1 -1
- package/dist/auth/offlineCredentials.js +14 -15
- package/dist/auth/offlineCredentials.js.map +1 -1
- package/dist/auth/offlineSession.d.ts +7 -9
- package/dist/auth/offlineSession.d.ts.map +1 -1
- package/dist/auth/offlineSession.js +18 -31
- package/dist/auth/offlineSession.js.map +1 -1
- package/dist/auth/resolveAuthState.d.ts.map +1 -1
- package/dist/auth/resolveAuthState.js +12 -12
- package/dist/auth/resolveAuthState.js.map +1 -1
- package/dist/auth/singleUser.d.ts.map +1 -1
- package/dist/auth/singleUser.js +28 -49
- package/dist/auth/singleUser.js.map +1 -1
- package/dist/bin/install-pwa.d.ts.map +1 -1
- package/dist/bin/install-pwa.js +3315 -958
- package/dist/bin/install-pwa.js.map +1 -1
- package/dist/config.d.ts +23 -3
- package/dist/config.d.ts.map +1 -1
- package/dist/config.js +29 -8
- package/dist/config.js.map +1 -1
- package/dist/conflicts.d.ts.map +1 -1
- package/dist/conflicts.js +9 -13
- package/dist/conflicts.js.map +1 -1
- package/dist/database.d.ts +13 -0
- package/dist/database.d.ts.map +1 -1
- package/dist/database.js +16 -0
- package/dist/database.js.map +1 -1
- package/dist/demo.d.ts +7 -0
- package/dist/demo.d.ts.map +1 -1
- package/dist/demo.js +51 -3
- package/dist/demo.js.map +1 -1
- package/dist/engine.d.ts.map +1 -1
- package/dist/engine.js +21 -46
- package/dist/engine.js.map +1 -1
- package/dist/entries/toast.d.ts +12 -0
- package/dist/entries/toast.d.ts.map +1 -0
- package/dist/entries/toast.js +11 -0
- package/dist/entries/toast.js.map +1 -0
- package/dist/queue.d.ts.map +1 -1
- package/dist/queue.js +1 -16
- package/dist/queue.js.map +1 -1
- package/dist/realtime.d.ts.map +1 -1
- package/dist/realtime.js +9 -21
- package/dist/realtime.js.map +1 -1
- package/dist/stores/toast.d.ts +40 -0
- package/dist/stores/toast.d.ts.map +1 -0
- package/dist/stores/toast.js +39 -0
- package/dist/stores/toast.js.map +1 -0
- package/package.json +13 -1
- package/src/components/GlobalToast.svelte +251 -0
- package/src/components/OfflineToast.svelte +168 -0
package/README.md
CHANGED
|
@@ -52,7 +52,7 @@ Building offline-first sync is notoriously difficult. stellar-drive handles the
|
|
|
52
52
|
- **Diagnostics** -- Comprehensive runtime diagnostics covering sync, queue, realtime, conflicts, egress, and network state.
|
|
53
53
|
- **Debug utilities** -- Opt-in debug logging and `window` debug utilities for browser console inspection during development.
|
|
54
54
|
- **SvelteKit integration** (optional) -- Layout helpers, server handlers, email confirmation, service worker lifecycle, and auth hydration.
|
|
55
|
-
- **PWA scaffolding CLI** -- `stellar-drive install pwa` generates a
|
|
55
|
+
- **PWA scaffolding CLI** -- `stellar-drive install pwa` generates a fully wired SvelteKit PWA skeleton (51 files) with auth, PIN gate, device verification, profile page, demo mode, adaptive navbar, and PWA plumbing pre-connected.
|
|
56
56
|
|
|
57
57
|
### Use cases
|
|
58
58
|
|
|
@@ -402,31 +402,156 @@ See [API Reference -- Vite Plugin](./API_REFERENCE.md#vite-plugin-stellarpwa) fo
|
|
|
402
402
|
|
|
403
403
|
### Install PWA
|
|
404
404
|
|
|
405
|
-
Scaffold a complete offline-first SvelteKit PWA
|
|
405
|
+
Scaffold a complete offline-first SvelteKit PWA skeleton with an interactive walkthrough:
|
|
406
406
|
|
|
407
407
|
```bash
|
|
408
408
|
npx stellar-drive install pwa
|
|
409
409
|
```
|
|
410
410
|
|
|
411
|
-
The wizard
|
|
411
|
+
Run this in an empty directory. The wizard collects four inputs, installs dependencies, and writes 51 files — a fully wired skeleton that passes `npm run validate` and `npm run cleanup` out of the box.
|
|
412
|
+
|
|
413
|
+
#### Wizard prompts
|
|
412
414
|
|
|
413
415
|
| Prompt | Required | Description |
|
|
414
416
|
|--------|----------|-------------|
|
|
415
|
-
| App Name | Yes | Full app name (e.g., "Stellar Planner") |
|
|
416
|
-
| Short Name | Yes |
|
|
417
|
-
| Prefix | Yes | Lowercase key for localStorage, caches,
|
|
418
|
-
| Description | No |
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
417
|
+
| App Name | Yes | Full app name (e.g., "Stellar Planner"). Used in page titles, manifest, and email templates. |
|
|
418
|
+
| Short Name | Yes | Condensed name for the PWA home-screen icon (12 chars max). |
|
|
419
|
+
| Prefix | Yes | Lowercase key used for localStorage, caches, the service worker scope, and Supabase table prefixes. Auto-suggested from the app name. |
|
|
420
|
+
| Description | No | One-line description shown in the manifest (default: `"A self-hosted offline-first PWA"`). |
|
|
421
|
+
|
|
422
|
+
#### What gets generated — 51 files
|
|
423
|
+
|
|
424
|
+
**Project config (10)**
|
|
425
|
+
|
|
426
|
+
| File | Purpose |
|
|
427
|
+
|------|---------|
|
|
428
|
+
| `package.json` | All deps and scripts pre-configured: `dev`, `build`, `validate`, `cleanup` |
|
|
429
|
+
| `vite.config.ts` | `stellarPWA` plugin wired with your prefix; schema generation enabled |
|
|
430
|
+
| `tsconfig.json` | Extends SvelteKit's generated config with strict mode |
|
|
431
|
+
| `svelte.config.js` | `adapter-auto` + `vitePreprocess` |
|
|
432
|
+
| `eslint.config.js` | TypeScript-aware ESLint with Svelte plugin |
|
|
433
|
+
| `.prettierrc` | Consistent formatting rules |
|
|
434
|
+
| `.prettierignore` | Ignores build artifacts and generated files |
|
|
435
|
+
| `knip.json` | Dead-code detection configured for SvelteKit |
|
|
436
|
+
| `.gitignore` | Node, SvelteKit, and environment file ignores |
|
|
437
|
+
| `.env.example` | Template for `PUBLIC_SUPABASE_URL` and `PUBLIC_SUPABASE_PUBLISHABLE_DEFAULT_KEY` |
|
|
438
|
+
|
|
439
|
+
**Documentation (3)**
|
|
440
|
+
|
|
441
|
+
| File | Purpose |
|
|
442
|
+
|------|---------|
|
|
443
|
+
| `README.md` | Project-level readme linking architecture and framework docs |
|
|
444
|
+
| `ARCHITECTURE.md` | Directory layout, data flow, and module responsibilities |
|
|
445
|
+
| `FRAMEWORKS.md` | Technology choices, rationale, and Svelte 5 rune patterns |
|
|
446
|
+
|
|
447
|
+
**Git hooks (1)**
|
|
448
|
+
|
|
449
|
+
| File | Purpose |
|
|
450
|
+
|------|---------|
|
|
451
|
+
| `.husky/pre-commit` | Runs `npm run cleanup && npm run validate && git add -u` before every commit |
|
|
452
|
+
|
|
453
|
+
**Static assets (12)**
|
|
454
|
+
|
|
455
|
+
| File | Purpose |
|
|
456
|
+
|------|---------|
|
|
457
|
+
| `static/manifest.json` | PWA manifest with all icon sizes and display settings |
|
|
458
|
+
| `static/offline.html` | Offline fallback shown by the service worker |
|
|
459
|
+
| `static/icons/app.svg` | Green primary app icon (letter placeholder) |
|
|
460
|
+
| `static/icons/app-dark.svg` | Dark variant for light-mode context |
|
|
461
|
+
| `static/icons/maskable.svg` | Maskable icon for Android home screens |
|
|
462
|
+
| `static/icons/favicon.svg` | Browser tab favicon |
|
|
463
|
+
| `static/icons/monochrome.svg` | Monochrome icon for notification badges |
|
|
464
|
+
| `static/icons/splash.svg` | Splash screen icon |
|
|
465
|
+
| `static/icons/apple-touch.svg` | iOS Add-to-Home-Screen icon |
|
|
466
|
+
| `static/signup-email.html` | Signup confirmation email template |
|
|
467
|
+
| `static/change-email.html` | Email change confirmation template |
|
|
468
|
+
| `static/device-verification-email.html` | Device trust OTP email template |
|
|
469
|
+
|
|
470
|
+
**App core (2)**
|
|
471
|
+
|
|
472
|
+
| File | Purpose |
|
|
473
|
+
|------|---------|
|
|
474
|
+
| `src/app.html` | PWA shell: iOS meta tags, theme color, service-worker registration script |
|
|
475
|
+
| `src/app.d.ts` | SvelteKit ambient types (`App.Locals`, `App.PageData`) |
|
|
476
|
+
|
|
477
|
+
**Routes (16)**
|
|
478
|
+
|
|
479
|
+
| Route | File(s) | What it does |
|
|
480
|
+
|-------|---------|-------------|
|
|
481
|
+
| Root layout | `+layout.ts`, `+layout.svelte` | Engine bootstrap, auth resolution, adaptive navbar (top on desktop / bottom on mobile), sync status, offline toast, demo banner, PWA update prompt |
|
|
482
|
+
| Home | `+page.svelte` | Protected placeholder — add your app content here |
|
|
483
|
+
| Error | `+error.svelte` | SvelteKit error page with retry and home link |
|
|
484
|
+
| Login | `login/+page.svelte` | PIN-based login, device linking, device verification email flow, BroadcastChannel handshake, persistent lockout countdown |
|
|
485
|
+
| Email confirm | `confirm/+page.svelte` | Verifies Supabase email OTP, broadcasts `AUTH_CONFIRMED` to the login tab, then closes or redirects |
|
|
486
|
+
| Setup (initial) | `setup/+page.ts`, `setup/+page.svelte` | Multi-step wizard: Supabase credentials → validate → deploy schema → create account. Guarded by `resolveSetupAccess()` — only accessible before a user account exists. |
|
|
487
|
+
| Reconfigure | `setup/Reconfigure.svelte` | Single-page re-setup form for changing credentials after initial setup. Accessible from the profile settings. |
|
|
488
|
+
| Profile | `profile/+page.svelte` | Full settings hub: display name, email change (with re-verification), PIN/code change, trusted devices list with revocation, debug mode toggle, diagnostics dashboard (sync, realtime, queue, egress, errors), reset database |
|
|
489
|
+
| Demo | `demo/+page.svelte` | Toggle demo mode on/off with explanation and confirmation; triggers full page reload |
|
|
490
|
+
| Privacy policy | `policy/+page.svelte` | Static placeholder — replace with your actual policy |
|
|
491
|
+
| Config API | `api/config/+server.ts` | Returns `PUBLIC_SUPABASE_URL` and `PUBLIC_SUPABASE_PUBLISHABLE_DEFAULT_KEY` to the client |
|
|
492
|
+
| Setup deploy | `api/setup/deploy/+server.ts` | Writes `.env` during initial setup, creates Supabase auth user + pushes schema SQL |
|
|
493
|
+
| Setup validate | `api/setup/validate/+server.ts` | Validates Supabase credentials without writing anything |
|
|
494
|
+
| Catch-all | `[...catchall]/+page.server.ts` | 302 redirect to `/` for unknown URLs |
|
|
495
|
+
|
|
496
|
+
**Library (7)**
|
|
497
|
+
|
|
498
|
+
| File | Purpose |
|
|
499
|
+
|------|---------|
|
|
500
|
+
| `src/lib/routes.ts` | `ROUTES` constants for all app paths — single source of truth |
|
|
501
|
+
| `src/lib/schema.ts` | Example schema with two tables (`items`, `settings`); replace with your domain schema |
|
|
502
|
+
| `src/lib/types.generated.ts` | Placeholder for Vite-plugin-generated TypeScript interfaces |
|
|
503
|
+
| `src/lib/types.ts` | App-specific type stubs and re-exports |
|
|
504
|
+
| `src/lib/components/UpdatePrompt.svelte` | PWA update prompt that appears when a new service worker is waiting |
|
|
505
|
+
| `src/lib/demo/mockData.ts` | Mock data seeded into the demo database on each page load |
|
|
506
|
+
| `src/lib/demo/config.ts` | Demo configuration wired into `initEngine()` |
|
|
507
|
+
|
|
508
|
+
#### What's pre-wired
|
|
509
|
+
|
|
510
|
+
The skeleton is not just file stubs — the entire auth and engine lifecycle is already connected:
|
|
511
|
+
|
|
512
|
+
- **Engine bootstrap** — `initEngine()` in `+layout.ts` with your prefix, name, and demo config; `initConfig()` pulls Supabase credentials from `/api/config` at runtime
|
|
513
|
+
- **Auth resolution** — `resolveRootLayout()` in the layout load determines `authMode` (`'none'` | `'offline'` | `'demo'`) and redirects unauthenticated users to login
|
|
514
|
+
- **Single-user PIN gate** — login page handles first-time setup detection, `unlockSingleUser`, `setupSingleUser` inside the login flow, device linking, and persistent lockout
|
|
515
|
+
- **Device verification** — email OTP flow fully wired through login → confirm → BroadcastChannel → login tab reaction
|
|
516
|
+
- **Setup wizard** — multi-step Supabase credential entry, live validation, schema deploy, and user account creation; guarded so it only appears before initial setup
|
|
517
|
+
- **Profile page** — change name, email (with re-verification cooldown and resend), PIN, revoke trusted devices, toggle debug mode, full diagnostics panel, and reset database
|
|
518
|
+
- **Demo mode** — sandboxed IndexedDB, zero Supabase calls, mock profile, seeded data; toggle from `/demo` or profile settings
|
|
519
|
+
- **Adaptive navbar** — top bar on ≥768px, fixed bottom bar on mobile; active state driven by SvelteKit's `page` store; Dynamic Island safe area padding
|
|
520
|
+
- **PWA plumbing** — service worker via `stellarPWA` Vite plugin, Web App Manifest, offline fallback, `UpdatePrompt` for background updates, iOS splash/touch icons
|
|
521
|
+
- **Email templates** — Supabase-compatible HTML templates for signup, email change, and device verification; drop-in replacements for the default Supabase emails
|
|
522
|
+
|
|
523
|
+
#### Design theme
|
|
524
|
+
|
|
525
|
+
The skeleton uses a minimal green theme derived from the email templates:
|
|
526
|
+
|
|
527
|
+
| Token | Value | Use |
|
|
528
|
+
|-------|-------|-----|
|
|
529
|
+
| Primary | `#6B9E6B` | Buttons, active nav, focus rings, borders |
|
|
530
|
+
| Card background | `#0f0f1e` | Modal and card surfaces |
|
|
531
|
+
| Page background | `#111116` | App background |
|
|
532
|
+
| Card border | `#3d5a3d` | Card outlines |
|
|
533
|
+
| Text | `#f0f0ff` | Primary text |
|
|
534
|
+
| Text secondary | `#c8c8e0` | Descriptions, labels |
|
|
535
|
+
| Text muted | `#7878a0` | Hints, timestamps |
|
|
536
|
+
|
|
537
|
+
All colors are CSS custom properties — override `:root` in your app's global CSS to adopt any theme.
|
|
538
|
+
|
|
539
|
+
#### Building on the skeleton
|
|
540
|
+
|
|
541
|
+
After scaffolding, the typical customisation path is:
|
|
542
|
+
|
|
543
|
+
1. **Define your schema** — edit `src/lib/schema.ts` to replace the example tables with your domain entities; the Vite plugin auto-generates TypeScript interfaces and pushes Supabase migrations on `npm run dev`
|
|
544
|
+
2. **Add app pages** — create new routes under `src/routes/`; import stores and CRUD helpers from `stellar-drive`
|
|
545
|
+
3. **Wire stores** — in `+page.svelte`, create collection/detail stores with `createCollectionStore` / `createDetailStore` and refresh them with `onSyncComplete`
|
|
546
|
+
4. **Customise the navbar** — the root layout's navbar lists only the home and profile links; add your app's sections to the `navItems` array in `+layout.svelte`
|
|
547
|
+
5. **Replace placeholder content** — swap the privacy policy text, update icon SVGs with your actual branding, and fill in the demo mock data with representative records
|
|
548
|
+
6. **Set environment variables** — copy `.env.example` to `.env` and add your Supabase project URL and publishable key; run the setup wizard on first launch to push the schema
|
|
549
|
+
|
|
550
|
+
#### Prerequisites
|
|
551
|
+
|
|
552
|
+
- Node.js ≥ 18
|
|
553
|
+
- A [Supabase](https://supabase.com) project (free tier is sufficient)
|
|
554
|
+
- `PUBLIC_SUPABASE_URL` and `PUBLIC_SUPABASE_PUBLISHABLE_DEFAULT_KEY` from the Supabase dashboard (Settings → API)
|
|
430
555
|
|
|
431
556
|
---
|
|
432
557
|
|
|
@@ -450,7 +575,8 @@ Import only what you need:
|
|
|
450
575
|
| `stellar-drive/config` | Runtime config management (`initConfig`, `getConfig`, `setConfig`, `getDexieTableFor`) |
|
|
451
576
|
| `stellar-drive/vite` | Vite plugin (`stellarPWA`) for service worker builds, asset manifests, and schema auto-generation |
|
|
452
577
|
| `stellar-drive/kit` | SvelteKit helpers: server route factories, layout loaders, email confirmation, SW lifecycle, auth hydration |
|
|
453
|
-
| `stellar-drive/
|
|
578
|
+
| `stellar-drive/toast` | Toast notifications: `addToast`, `dismissToast`, `toastStore`, `ToastVariant` type |
|
|
579
|
+
| `stellar-drive/components/*` | Svelte components: `SyncStatus`, `DeferredChangesBanner`, `DemoBanner`, `DemoBlockedMessage`, `OfflineToast`, `GlobalToast` |
|
|
454
580
|
|
|
455
581
|
### Key categories at a glance
|
|
456
582
|
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"loginGuard.d.ts","sourceRoot":"","sources":["../../src/auth/loginGuard.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA6CG;
|
|
1
|
+
{"version":3,"file":"loginGuard.d.ts","sourceRoot":"","sources":["../../src/auth/loginGuard.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA6CG;AAgDH;;;;;;;GAOG;AACH,MAAM,MAAM,gBAAgB,GAAG,aAAa,GAAG,UAAU,CAAC;AAE1D;;;;;;;GAOG;AACH,MAAM,MAAM,cAAc,GACtB;IAAE,OAAO,EAAE,IAAI,CAAC;IAAC,QAAQ,EAAE,gBAAgB,CAAA;CAAE,GAC7C;IAAE,OAAO,EAAE,KAAK,CAAC;IAAC,KAAK,EAAE,MAAM,CAAC;IAAC,YAAY,CAAC,EAAE,MAAM,CAAA;CAAE,CAAC;AAuG7D;;;;;;;;;;;;;;;;;;GAkBG;AACH,wBAAsB,sBAAsB,IAAI,OAAO,CAAC,MAAM,CAAC,CAQ9D;AAED;;;;;;;;;;;;;;;;;;;;;;;;;GAyBG;AACH,wBAAsB,aAAa,CAAC,KAAK,EAAE,MAAM,GAAG,OAAO,CAAC,cAAc,CAAC,CAmE1E;AAED;;;;;;;;;;;GAWG;AACH,wBAAsB,cAAc,IAAI,OAAO,CAAC,IAAI,CAAC,CAUpD;AAED;;;;;;;;;;;;;;;;;;;;;;GAsBG;AACH,wBAAsB,cAAc,CAAC,QAAQ,EAAE,gBAAgB,GAAG,OAAO,CAAC,IAAI,CAAC,CAgC9E;AAED;;;;;;;;;;;GAWG;AACH,wBAAsB,eAAe,IAAI,OAAO,CAAC,IAAI,CAAC,CASrD"}
|
package/dist/auth/loginGuard.js
CHANGED
|
@@ -45,7 +45,8 @@
|
|
|
45
45
|
* @module auth/loginGuard
|
|
46
46
|
*/
|
|
47
47
|
import { hashValue } from './crypto';
|
|
48
|
-
import {
|
|
48
|
+
import { waitForDb } from '../config';
|
|
49
|
+
import { getDb, TABLE } from '../database';
|
|
49
50
|
import { debugLog, debugWarn } from '../debug';
|
|
50
51
|
// =============================================================================
|
|
51
52
|
// CONSTANTS
|
|
@@ -92,17 +93,14 @@ let consecutiveLocalFailures = 0;
|
|
|
92
93
|
*/
|
|
93
94
|
async function invalidateCachedHash() {
|
|
94
95
|
try {
|
|
95
|
-
const
|
|
96
|
-
const
|
|
97
|
-
if (
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
});
|
|
104
|
-
debugLog('[LoginGuard] Invalidated single-user gateHash');
|
|
105
|
-
}
|
|
96
|
+
const db = getDb();
|
|
97
|
+
const record = await db.table(TABLE.SINGLE_USER_CONFIG).get('config');
|
|
98
|
+
if (record && record.gateHash) {
|
|
99
|
+
await db.table(TABLE.SINGLE_USER_CONFIG).update('config', {
|
|
100
|
+
gateHash: undefined,
|
|
101
|
+
updatedAt: new Date().toISOString()
|
|
102
|
+
});
|
|
103
|
+
debugLog('[LoginGuard] Invalidated single-user gateHash');
|
|
106
104
|
}
|
|
107
105
|
}
|
|
108
106
|
catch (e) {
|
|
@@ -123,10 +121,8 @@ async function readPersistentLockout() {
|
|
|
123
121
|
};
|
|
124
122
|
try {
|
|
125
123
|
await waitForDb();
|
|
126
|
-
const db =
|
|
127
|
-
|
|
128
|
-
return zero;
|
|
129
|
-
const record = await db.table('singleUserConfig').get(PIN_LOCKOUT_KEY);
|
|
124
|
+
const db = getDb();
|
|
125
|
+
const record = await db.table(TABLE.SINGLE_USER_CONFIG).get(PIN_LOCKOUT_KEY);
|
|
130
126
|
return record ?? zero;
|
|
131
127
|
}
|
|
132
128
|
catch {
|
|
@@ -141,10 +137,7 @@ async function readPersistentLockout() {
|
|
|
141
137
|
*/
|
|
142
138
|
async function writePersistentLockout(record) {
|
|
143
139
|
try {
|
|
144
|
-
|
|
145
|
-
if (db) {
|
|
146
|
-
await db.table('singleUserConfig').put(record);
|
|
147
|
-
}
|
|
140
|
+
await getDb().table(TABLE.SINGLE_USER_CONFIG).put(record);
|
|
148
141
|
}
|
|
149
142
|
catch (e) {
|
|
150
143
|
debugWarn('[LoginGuard] Failed to write persistent lockout:', e);
|
|
@@ -239,12 +232,13 @@ export async function preCheckLogin(input) {
|
|
|
239
232
|
};
|
|
240
233
|
}
|
|
241
234
|
let cachedHash;
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
if (db) {
|
|
245
|
-
const record = await db.table('singleUserConfig').get('config');
|
|
235
|
+
try {
|
|
236
|
+
const record = await getDb().table(TABLE.SINGLE_USER_CONFIG).get('config');
|
|
246
237
|
cachedHash = record?.gateHash;
|
|
247
238
|
}
|
|
239
|
+
catch {
|
|
240
|
+
/* DB not ready yet — fall through to no-cache path */
|
|
241
|
+
}
|
|
248
242
|
if (cachedHash) {
|
|
249
243
|
/* We have a cached hash -- compare locally before touching the network. */
|
|
250
244
|
const inputHash = await hashValue(input);
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"loginGuard.js","sourceRoot":"","sources":["../../src/auth/loginGuard.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA6CG;AAEH,OAAO,EAAE,SAAS,EAAE,MAAM,UAAU,CAAC;AACrC,OAAO,EAAE,
|
|
1
|
+
{"version":3,"file":"loginGuard.js","sourceRoot":"","sources":["../../src/auth/loginGuard.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA6CG;AAEH,OAAO,EAAE,SAAS,EAAE,MAAM,UAAU,CAAC;AACrC,OAAO,EAAE,SAAS,EAAE,MAAM,WAAW,CAAC;AACtC,OAAO,EAAE,KAAK,EAAE,KAAK,EAAE,MAAM,aAAa,CAAC;AAC3C,OAAO,EAAE,QAAQ,EAAE,SAAS,EAAE,MAAM,UAAU,CAAC;AAE/C,gFAAgF;AAChF,YAAY;AACZ,gFAAgF;AAEhF;;;GAGG;AACH,MAAM,uBAAuB,GAAG,CAAC,CAAC;AAElC;;;;;;GAMG;AACH,MAAM,wBAAwB,GAAoD;IAChF,EAAE,QAAQ,EAAE,CAAC,EAAE,UAAU,EAAE,CAAC,GAAG,KAAM,EAAE,EAAE,4BAA4B;IACrE,EAAE,QAAQ,EAAE,EAAE,EAAE,UAAU,EA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QAAC,OAAO,CAAC,EAAE,CAAC;YACX,SAAS,CAAC,mDAAmD,EAAE,CAAC,CAAC,CAAC;QACpE,CAAC;IACH,CAAC;AACH,CAAC;AAED;;;;;;;;;;;GAWG;AACH,MAAM,CAAC,KAAK,UAAU,eAAe;IACnC,wBAAwB,GAAG,CAAC,CAAC;IAC7B,MAAM,sBAAsB,CAAC;QAC3B,EAAE,EAAE,eAAe;QACnB,YAAY,EAAE,CAAC;QACf,YAAY,EAAE,CAAC;QACf,SAAS,EAAE,IAAI,IAAI,EAAE,CAAC,WAAW,EAAE;KACpC,CAAC,CAAC;IACH,QAAQ,CAAC,0BAA0B,CAAC,CAAC;AACvC,CAAC"}
|
|
@@ -25,7 +25,7 @@
|
|
|
25
25
|
* @module auth/offlineCredentials
|
|
26
26
|
*/
|
|
27
27
|
import type { OfflineCredentials } from '../types';
|
|
28
|
-
import type { User
|
|
28
|
+
import type { User } from '@supabase/supabase-js';
|
|
29
29
|
/**
|
|
30
30
|
* Cache user credentials for offline login.
|
|
31
31
|
*
|
|
@@ -36,8 +36,6 @@ import type { User, Session } from '@supabase/supabase-js';
|
|
|
36
36
|
* @param email - The user's email address (used for offline identity matching).
|
|
37
37
|
* @param password - The user's plaintext password. Will be SHA-256-hashed before storage.
|
|
38
38
|
* @param user - The Supabase `User` object, used to extract `userId` and profile data.
|
|
39
|
-
* @param _session - The Supabase `Session` object. Currently unused but accepted for
|
|
40
|
-
* API symmetry with the online auth flow (reserved for future use).
|
|
41
39
|
*
|
|
42
40
|
* @throws {Error} If `email` or `password` is empty (prevents storing incomplete credentials).
|
|
43
41
|
* @throws {Error} If the write-back verification fails (password not persisted in IndexedDB).
|
|
@@ -46,14 +44,14 @@ import type { User, Session } from '@supabase/supabase-js';
|
|
|
46
44
|
* ```ts
|
|
47
45
|
* const { data } = await supabase.auth.signInWithPassword({ email, password });
|
|
48
46
|
* if (data.user && data.session) {
|
|
49
|
-
* await cacheOfflineCredentials(email, password, data.user
|
|
47
|
+
* await cacheOfflineCredentials(email, password, data.user);
|
|
50
48
|
* }
|
|
51
49
|
* ```
|
|
52
50
|
*
|
|
53
51
|
* @see {@link getOfflineCredentials} to retrieve the cached credentials.
|
|
54
52
|
* @see {@link clearOfflineCredentials} to remove them on logout.
|
|
55
53
|
*/
|
|
56
|
-
export declare function cacheOfflineCredentials(email: string, password: string, user: User
|
|
54
|
+
export declare function cacheOfflineCredentials(email: string, password: string, user: User): Promise<void>;
|
|
57
55
|
/**
|
|
58
56
|
* Get cached offline credentials from IndexedDB.
|
|
59
57
|
*
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"offlineCredentials.d.ts","sourceRoot":"","sources":["../../src/auth/offlineCredentials.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;GAyBG;
|
|
1
|
+
{"version":3,"file":"offlineCredentials.d.ts","sourceRoot":"","sources":["../../src/auth/offlineCredentials.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;GAyBG;AAIH,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,UAAU,CAAC;AACnD,OAAO,KAAK,EAAE,IAAI,EAAE,MAAM,uBAAuB,CAAC;AAmBlD;;;;;;;;;;;;;;;;;;;;;;;;GAwBG;AACH,wBAAsB,uBAAuB,CAC3C,KAAK,EAAE,MAAM,EACb,QAAQ,EAAE,MAAM,EAChB,IAAI,EAAE,IAAI,GACT,OAAO,CAAC,IAAI,CAAC,CA0Cf;AAED;;;;;;;;;;;;;;;;GAgBG;AACH,wBAAsB,qBAAqB,IAAI,OAAO,CAAC,kBAAkB,GAAG,IAAI,CAAC,CAShF;AAMD;;;;;;;;;;;;;GAaG;AACH,wBAAsB,+BAA+B,CACnD,OAAO,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAC/B,OAAO,CAAC,IAAI,CAAC,CAWf;AAED;;;;;;;;;;;;;GAaG;AACH,wBAAsB,uBAAuB,IAAI,OAAO,CAAC,IAAI,CAAC,CAI7D"}
|
|
@@ -25,6 +25,7 @@
|
|
|
25
25
|
* @module auth/offlineCredentials
|
|
26
26
|
*/
|
|
27
27
|
import { getEngineConfig, waitForDb } from '../config';
|
|
28
|
+
import { getDb, TABLE } from '../database';
|
|
28
29
|
import { debugError } from '../debug';
|
|
29
30
|
import { hashValue } from './crypto';
|
|
30
31
|
// =============================================================================
|
|
@@ -35,7 +36,7 @@ import { hashValue } from './crypto';
|
|
|
35
36
|
* in the `offlineCredentials` IndexedDB table. Only one credential set is
|
|
36
37
|
* cached at any time.
|
|
37
38
|
*/
|
|
38
|
-
const
|
|
39
|
+
const CREDENTIALS_KEY = 'current_user';
|
|
39
40
|
// =============================================================================
|
|
40
41
|
// PUBLIC API -- Cache & Retrieve
|
|
41
42
|
// =============================================================================
|
|
@@ -49,8 +50,6 @@ const CREDENTIALS_ID = 'current_user';
|
|
|
49
50
|
* @param email - The user's email address (used for offline identity matching).
|
|
50
51
|
* @param password - The user's plaintext password. Will be SHA-256-hashed before storage.
|
|
51
52
|
* @param user - The Supabase `User` object, used to extract `userId` and profile data.
|
|
52
|
-
* @param _session - The Supabase `Session` object. Currently unused but accepted for
|
|
53
|
-
* API symmetry with the online auth flow (reserved for future use).
|
|
54
53
|
*
|
|
55
54
|
* @throws {Error} If `email` or `password` is empty (prevents storing incomplete credentials).
|
|
56
55
|
* @throws {Error} If the write-back verification fails (password not persisted in IndexedDB).
|
|
@@ -59,14 +58,14 @@ const CREDENTIALS_ID = 'current_user';
|
|
|
59
58
|
* ```ts
|
|
60
59
|
* const { data } = await supabase.auth.signInWithPassword({ email, password });
|
|
61
60
|
* if (data.user && data.session) {
|
|
62
|
-
* await cacheOfflineCredentials(email, password, data.user
|
|
61
|
+
* await cacheOfflineCredentials(email, password, data.user);
|
|
63
62
|
* }
|
|
64
63
|
* ```
|
|
65
64
|
*
|
|
66
65
|
* @see {@link getOfflineCredentials} to retrieve the cached credentials.
|
|
67
66
|
* @see {@link clearOfflineCredentials} to remove them on logout.
|
|
68
67
|
*/
|
|
69
|
-
export async function cacheOfflineCredentials(email, password, user
|
|
68
|
+
export async function cacheOfflineCredentials(email, password, user) {
|
|
70
69
|
/* Validate inputs to prevent storing incomplete credentials that would
|
|
71
70
|
cause confusing verification failures later. */
|
|
72
71
|
if (!email || !password) {
|
|
@@ -75,7 +74,7 @@ export async function cacheOfflineCredentials(email, password, user, _session) {
|
|
|
75
74
|
}
|
|
76
75
|
await waitForDb();
|
|
77
76
|
const config = getEngineConfig();
|
|
78
|
-
const db =
|
|
77
|
+
const db = getDb();
|
|
79
78
|
/* Extract a normalized profile using the host app's profileExtractor,
|
|
80
79
|
or fall back to raw Supabase user_metadata. This allows the host app
|
|
81
80
|
to control which fields are available offline (e.g., firstName, role). */
|
|
@@ -84,7 +83,7 @@ export async function cacheOfflineCredentials(email, password, user, _session) {
|
|
|
84
83
|
: user.user_metadata || {};
|
|
85
84
|
const hashedPassword = await hashValue(password);
|
|
86
85
|
const credentials = {
|
|
87
|
-
id:
|
|
86
|
+
id: CREDENTIALS_KEY,
|
|
88
87
|
userId: user.id,
|
|
89
88
|
email: email,
|
|
90
89
|
password: hashedPassword,
|
|
@@ -92,12 +91,12 @@ export async function cacheOfflineCredentials(email, password, user, _session) {
|
|
|
92
91
|
cachedAt: new Date().toISOString()
|
|
93
92
|
};
|
|
94
93
|
/* Use put (upsert) to insert or update the singleton record. */
|
|
95
|
-
await db.table(
|
|
94
|
+
await db.table(TABLE.OFFLINE_CREDENTIALS).put(credentials);
|
|
96
95
|
/* Paranoid read-back: verify the credentials were stored correctly.
|
|
97
96
|
IndexedDB writes can silently fail in quota-exceeded or private-browsing
|
|
98
97
|
scenarios; catching this early gives a clear error instead of a mysterious
|
|
99
98
|
"wrong password" on the next offline login. */
|
|
100
|
-
const stored = await db.table(
|
|
99
|
+
const stored = await db.table(TABLE.OFFLINE_CREDENTIALS).get(CREDENTIALS_KEY);
|
|
101
100
|
if (!stored || !stored.password) {
|
|
102
101
|
debugError('[Auth] Credentials were not stored correctly - password missing');
|
|
103
102
|
throw new Error('Failed to store credentials: password not persisted');
|
|
@@ -122,8 +121,8 @@ export async function cacheOfflineCredentials(email, password, user, _session) {
|
|
|
122
121
|
*/
|
|
123
122
|
export async function getOfflineCredentials() {
|
|
124
123
|
await waitForDb();
|
|
125
|
-
const db =
|
|
126
|
-
const credentials = await db.table(
|
|
124
|
+
const db = getDb();
|
|
125
|
+
const credentials = await db.table(TABLE.OFFLINE_CREDENTIALS).get(CREDENTIALS_KEY);
|
|
127
126
|
if (!credentials) {
|
|
128
127
|
return null;
|
|
129
128
|
}
|
|
@@ -151,8 +150,8 @@ export async function updateOfflineCredentialsProfile(profile) {
|
|
|
151
150
|
if (!credentials) {
|
|
152
151
|
return;
|
|
153
152
|
}
|
|
154
|
-
const db =
|
|
155
|
-
await db.table(
|
|
153
|
+
const db = getDb();
|
|
154
|
+
await db.table(TABLE.OFFLINE_CREDENTIALS).update(CREDENTIALS_KEY, {
|
|
156
155
|
profile,
|
|
157
156
|
cachedAt: new Date().toISOString()
|
|
158
157
|
});
|
|
@@ -173,7 +172,7 @@ export async function updateOfflineCredentialsProfile(profile) {
|
|
|
173
172
|
*/
|
|
174
173
|
export async function clearOfflineCredentials() {
|
|
175
174
|
await waitForDb();
|
|
176
|
-
const db =
|
|
177
|
-
await db.table(
|
|
175
|
+
const db = getDb();
|
|
176
|
+
await db.table(TABLE.OFFLINE_CREDENTIALS).delete(CREDENTIALS_KEY);
|
|
178
177
|
}
|
|
179
178
|
//# sourceMappingURL=offlineCredentials.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"offlineCredentials.js","sourceRoot":"","sources":["../../src/auth/offlineCredentials.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;GAyBG;AAEH,OAAO,EAAE,eAAe,EAAE,SAAS,EAAE,MAAM,WAAW,CAAC;
|
|
1
|
+
{"version":3,"file":"offlineCredentials.js","sourceRoot":"","sources":["../../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C,MAAM,CAAC,eAAe,EAAE;QAChE,OAAO;QACP,QAAQ,EAAE,IAAI,IAAI,EAAE,CAAC,WAAW,EAAE;KACnC,CAAC,CAAC;AACL,CAAC;AAED;;;;;;;;;;;;;GAaG;AACH,MAAM,CAAC,KAAK,UAAU,uBAAuB;IAC3C,MAAM,SAAS,EAAE,CAAC;IAClB,MAAM,EAAE,GAAG,KAAK,EAAE,CAAC;IACnB,MAAM,EAAE,CAAC,KAAK,CAAC,KAAK,CAAC,mBAAmB,CAAC,CAAC,MAAM,CAAC,eAAe,CAAC,CAAC;AACpE,CAAC"}
|
|
@@ -51,23 +51,21 @@ import type { OfflineSession } from '../types';
|
|
|
51
51
|
* console.log('Offline token:', session.offlineToken);
|
|
52
52
|
* ```
|
|
53
53
|
*
|
|
54
|
-
* @see {@link
|
|
54
|
+
* @see {@link getOfflineSession} to retrieve the current session.
|
|
55
55
|
* @see {@link clearOfflineSession} to revoke the session on logout.
|
|
56
56
|
*/
|
|
57
57
|
export declare function createOfflineSession(userId: string): Promise<OfflineSession>;
|
|
58
58
|
/**
|
|
59
|
-
* Get
|
|
59
|
+
* Get the current offline session from IndexedDB.
|
|
60
60
|
*
|
|
61
|
-
*
|
|
62
|
-
*
|
|
63
|
-
* adding expiration, rotation, or other validation logic without changing
|
|
64
|
-
* the public API contract.
|
|
61
|
+
* Sessions do not expire — they are only revoked explicitly on re-authentication
|
|
62
|
+
* or logout. This function is the single source of truth for offline session state.
|
|
65
63
|
*
|
|
66
|
-
* @returns The current
|
|
64
|
+
* @returns The current offline session, or `null` if none exists.
|
|
67
65
|
*
|
|
68
66
|
* @example
|
|
69
67
|
* ```ts
|
|
70
|
-
* const session = await
|
|
68
|
+
* const session = await getOfflineSession();
|
|
71
69
|
* if (session) {
|
|
72
70
|
* console.log('User is authenticated offline:', session.userId);
|
|
73
71
|
* }
|
|
@@ -75,7 +73,7 @@ export declare function createOfflineSession(userId: string): Promise<OfflineSes
|
|
|
75
73
|
*
|
|
76
74
|
* @see {@link createOfflineSession} to create a new session after verification.
|
|
77
75
|
*/
|
|
78
|
-
export declare function
|
|
76
|
+
export declare function getOfflineSession(): Promise<OfflineSession | null>;
|
|
79
77
|
/**
|
|
80
78
|
* Clear the offline session from IndexedDB.
|
|
81
79
|
*
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"offlineSession.d.ts","sourceRoot":"","sources":["../../src/auth/offlineSession.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA4BG;
|
|
1
|
+
{"version":3,"file":"offlineSession.d.ts","sourceRoot":"","sources":["../../src/auth/offlineSession.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA4BG;AAIH,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,UAAU,CAAC;AAgB/C;;;;;;;;;;;;;;;;;;;;;;;;;GAyBG;AACH,wBAAsB,oBAAoB,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,cAAc,CAAC,CAwBlF;AAED;;;;;;;;;;;;;;;;;GAiBG;AACH,wBAAsB,iBAAiB,IAAI,OAAO,CAAC,cAAc,GAAG,IAAI,CAAC,CAKxE;AAED;;;;;;;;;;;;;;;GAeG;AACH,wBAAsB,mBAAmB,IAAI,OAAO,CAAC,IAAI,CAAC,CAIzD"}
|
|
@@ -27,7 +27,8 @@
|
|
|
27
27
|
*
|
|
28
28
|
* @module auth/offlineSession
|
|
29
29
|
*/
|
|
30
|
-
import {
|
|
30
|
+
import { waitForDb } from '../config';
|
|
31
|
+
import { getDb, TABLE } from '../database';
|
|
31
32
|
// =============================================================================
|
|
32
33
|
// CONSTANTS
|
|
33
34
|
// =============================================================================
|
|
@@ -35,7 +36,7 @@ import { getEngineConfig, waitForDb } from '../config';
|
|
|
35
36
|
* Singleton key for the offline session record in IndexedDB.
|
|
36
37
|
* Only one offline session exists at any given time.
|
|
37
38
|
*/
|
|
38
|
-
const
|
|
39
|
+
const SESSION_KEY = 'current_session';
|
|
39
40
|
// =============================================================================
|
|
40
41
|
// PUBLIC API
|
|
41
42
|
// =============================================================================
|
|
@@ -62,25 +63,25 @@ const SESSION_ID = 'current_session';
|
|
|
62
63
|
* console.log('Offline token:', session.offlineToken);
|
|
63
64
|
* ```
|
|
64
65
|
*
|
|
65
|
-
* @see {@link
|
|
66
|
+
* @see {@link getOfflineSession} to retrieve the current session.
|
|
66
67
|
* @see {@link clearOfflineSession} to revoke the session on logout.
|
|
67
68
|
*/
|
|
68
69
|
export async function createOfflineSession(userId) {
|
|
69
70
|
await waitForDb();
|
|
70
71
|
const now = new Date();
|
|
71
|
-
const db =
|
|
72
|
+
const db = getDb();
|
|
72
73
|
const session = {
|
|
73
|
-
id:
|
|
74
|
+
id: SESSION_KEY,
|
|
74
75
|
userId: userId,
|
|
75
76
|
offlineToken: crypto.randomUUID(),
|
|
76
77
|
createdAt: now.toISOString()
|
|
77
78
|
};
|
|
78
79
|
/* Use put (upsert) to insert or update the singleton record. */
|
|
79
|
-
await db.table(
|
|
80
|
+
await db.table(TABLE.OFFLINE_SESSION).put(session);
|
|
80
81
|
/* Verify the session was persisted by reading it back. Without this check,
|
|
81
82
|
a silent write failure would leave the user in a "logged in" state with
|
|
82
83
|
no session record, causing downstream auth checks to fail. */
|
|
83
|
-
const verified = await db.table(
|
|
84
|
+
const verified = await db.table(TABLE.OFFLINE_SESSION).get(SESSION_KEY);
|
|
84
85
|
if (!verified) {
|
|
85
86
|
throw new Error('Failed to persist offline session');
|
|
86
87
|
}
|
|
@@ -89,31 +90,14 @@ export async function createOfflineSession(userId) {
|
|
|
89
90
|
/**
|
|
90
91
|
* Get the current offline session from IndexedDB.
|
|
91
92
|
*
|
|
92
|
-
*
|
|
93
|
-
*
|
|
94
|
-
* validation in the future (e.g., expiration checks).
|
|
93
|
+
* Sessions do not expire — they are only revoked explicitly on re-authentication
|
|
94
|
+
* or logout. This function is the single source of truth for offline session state.
|
|
95
95
|
*
|
|
96
96
|
* @returns The current offline session, or `null` if none exists.
|
|
97
|
-
*/
|
|
98
|
-
async function getOfflineSession() {
|
|
99
|
-
await waitForDb();
|
|
100
|
-
const db = getEngineConfig().db;
|
|
101
|
-
const session = await db.table('offlineSession').get(SESSION_ID);
|
|
102
|
-
return session || null;
|
|
103
|
-
}
|
|
104
|
-
/**
|
|
105
|
-
* Get a valid offline session.
|
|
106
|
-
*
|
|
107
|
-
* Currently equivalent to `getOfflineSession()` (sessions do not expire),
|
|
108
|
-
* but exists as a separate function to serve as the future hook for
|
|
109
|
-
* adding expiration, rotation, or other validation logic without changing
|
|
110
|
-
* the public API contract.
|
|
111
|
-
*
|
|
112
|
-
* @returns The current valid offline session, or `null` if no session exists.
|
|
113
97
|
*
|
|
114
98
|
* @example
|
|
115
99
|
* ```ts
|
|
116
|
-
* const session = await
|
|
100
|
+
* const session = await getOfflineSession();
|
|
117
101
|
* if (session) {
|
|
118
102
|
* console.log('User is authenticated offline:', session.userId);
|
|
119
103
|
* }
|
|
@@ -121,8 +105,11 @@ async function getOfflineSession() {
|
|
|
121
105
|
*
|
|
122
106
|
* @see {@link createOfflineSession} to create a new session after verification.
|
|
123
107
|
*/
|
|
124
|
-
export async function
|
|
125
|
-
|
|
108
|
+
export async function getOfflineSession() {
|
|
109
|
+
await waitForDb();
|
|
110
|
+
const db = getDb();
|
|
111
|
+
const session = await db.table(TABLE.OFFLINE_SESSION).get(SESSION_KEY);
|
|
112
|
+
return session || null;
|
|
126
113
|
}
|
|
127
114
|
/**
|
|
128
115
|
* Clear the offline session from IndexedDB.
|
|
@@ -142,7 +129,7 @@ export async function getValidOfflineSession() {
|
|
|
142
129
|
*/
|
|
143
130
|
export async function clearOfflineSession() {
|
|
144
131
|
await waitForDb();
|
|
145
|
-
const db =
|
|
146
|
-
await db.table(
|
|
132
|
+
const db = getDb();
|
|
133
|
+
await db.table(TABLE.OFFLINE_SESSION).delete(SESSION_KEY);
|
|
147
134
|
}
|
|
148
135
|
//# sourceMappingURL=offlineSession.js.map
|