♻️ Drop legacy browser storage on sign-in
Browsers of users of the Dexie based version still hold its token in the auth_token key of localStorage and its database named wotra, next to the per user databases of this version. resolveGate() now removes both whenever it lets the user into the app, the first open included. The removal resolves on success, error and block, like wipeLocal, and never throws, so a browser that denies storage cannot keep the user out.
This commit is contained in:
@@ -451,6 +451,20 @@ export async function wipeLocal(uid: string): Promise<void> {
|
||||
});
|
||||
}
|
||||
|
||||
const LEGACY_TOKEN = 'auth_token';
|
||||
const LEGACY_DB = 'wotra';
|
||||
|
||||
/** Token and database of the Dexie based app; removed on sign-in, whatever the browser allows. */
|
||||
export async function removeLegacyStorage(): Promise<void> {
|
||||
try {
|
||||
localStorage.removeItem(LEGACY_TOKEN);
|
||||
await new Promise<void>((resolve) => {
|
||||
const req = indexedDB.deleteDatabase(LEGACY_DB);
|
||||
req.onsuccess = req.onerror = req.onblocked = () => resolve();
|
||||
});
|
||||
} catch {}
|
||||
}
|
||||
|
||||
export function currentUserId(): string {
|
||||
return userId;
|
||||
}
|
||||
|
||||
+33
-1
@@ -1,7 +1,7 @@
|
||||
import { Collection } from '@signaldb/core';
|
||||
import { tick } from 'svelte';
|
||||
import { afterAll, beforeEach, describe, expect, it, vi } from 'vitest';
|
||||
import { all, closeDb, get, openDb, pendingCount, remove, save, status, syncNow, toRecord, update } from './db.svelte';
|
||||
import { all, closeDb, get, openDb, pendingCount, remove, removeLegacyStorage, save, status, syncNow, toRecord, update } from './db.svelte';
|
||||
import { watch } from './testing.svelte';
|
||||
|
||||
const mocks = vi.hoisted(() => {
|
||||
@@ -162,6 +162,38 @@ describe('read scopes', () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe('legacy storage', () => {
|
||||
const open = (name: string) =>
|
||||
new Promise<void>((resolve, reject) => {
|
||||
const req = indexedDB.open(name);
|
||||
req.onsuccess = () => {
|
||||
req.result.close();
|
||||
resolve();
|
||||
};
|
||||
req.onerror = () => reject(req.error);
|
||||
});
|
||||
const names = async () => (await indexedDB.databases()).map((d) => d.name);
|
||||
|
||||
it('removeLegacyStorage drops the token and the Dexie database', async () => {
|
||||
localStorage.setItem('auth_token', 'secret');
|
||||
await open('wotra');
|
||||
expect(await names()).toContain('wotra');
|
||||
await removeLegacyStorage();
|
||||
expect(localStorage.getItem('auth_token')).toBeNull();
|
||||
expect(await names()).not.toContain('wotra');
|
||||
expect(await names()).toContain(`wotra-user-${n}`);
|
||||
});
|
||||
|
||||
it('removeLegacyStorage never throws', async () => {
|
||||
vi.stubGlobal('indexedDB', undefined);
|
||||
try {
|
||||
await expect(removeLegacyStorage()).resolves.toBeUndefined();
|
||||
} finally {
|
||||
vi.unstubAllGlobals();
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
describe('sync boundary', () => {
|
||||
it('toRecord turns end_time 0 into null and coerces the metadata', () => {
|
||||
const doc = toRecord('entries', {
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { closeDb, currentUserId, openDb, wipeLocal } from '$lib/db.svelte';
|
||||
import { closeDb, currentUserId, openDb, removeLegacyStorage, wipeLocal } from '$lib/db.svelte';
|
||||
import { errorMessage, pb } from '$lib/pb';
|
||||
|
||||
export const session = $state({
|
||||
@@ -45,6 +45,7 @@ export async function resolveGate(): Promise<Gate> {
|
||||
return 'login';
|
||||
}
|
||||
await activate();
|
||||
void removeLegacyStorage();
|
||||
return 'app';
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,55 @@
|
||||
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
|
||||
import { closeDb, status } from './db.svelte';
|
||||
import { resolveGate, session } from './session.svelte';
|
||||
|
||||
const mocks = vi.hoisted(() => ({
|
||||
legacy: vi.fn(),
|
||||
record: null as null | { id: string; email?: string }
|
||||
}));
|
||||
|
||||
vi.mock('./db.svelte', async (importOriginal) => {
|
||||
const actual = await importOriginal<typeof import('./db.svelte')>();
|
||||
return { ...actual, removeLegacyStorage: mocks.legacy };
|
||||
});
|
||||
|
||||
vi.mock('$lib/pb', () => ({
|
||||
errorMessage: (e: unknown) => String(e),
|
||||
pb: {
|
||||
authStore: {
|
||||
get isValid() {
|
||||
return mocks.record !== null;
|
||||
},
|
||||
get record() {
|
||||
return mocks.record;
|
||||
},
|
||||
clear: () => (mocks.record = null)
|
||||
},
|
||||
collection: () => ({ authRefresh: async () => undefined })
|
||||
}
|
||||
}));
|
||||
|
||||
beforeEach(() => {
|
||||
status.online = false;
|
||||
mocks.legacy.mockClear();
|
||||
});
|
||||
|
||||
afterEach(async () => {
|
||||
mocks.record = null;
|
||||
await closeDb();
|
||||
});
|
||||
|
||||
describe('resolveGate', () => {
|
||||
it('resolves to the login page without a session and leaves the browser storage alone', async () => {
|
||||
expect(await resolveGate()).toBe('login');
|
||||
expect(session.checked).toBe(true);
|
||||
expect(mocks.legacy).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('opens the database and drops the legacy storage when it resolves to the app', async () => {
|
||||
mocks.record = { id: 'gate-user', email: 'me@example.com' };
|
||||
expect(await resolveGate()).toBe('app');
|
||||
expect(session).toMatchObject({ userId: 'gate-user', email: 'me@example.com', checked: true });
|
||||
expect(status.ready).toBe(true);
|
||||
expect(mocks.legacy).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user