-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindexDB.ts
More file actions
26 lines (21 loc) · 711 Bytes
/
indexDB.ts
File metadata and controls
26 lines (21 loc) · 711 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
import { get, set, del } from 'idb-keyval';
import { useUserStore } from './stores/userStore';
// All logic here should synchronize between indexDB and the user store
const keyName = 'userKey';
// Store base64-encoded raw key
export async function saveUserKey(key: CryptoKey) {
console.log('Saving user key');
useUserStore.getState().setKey(key);
return set(keyName, key);
}
export async function loadUserKey() {
console.log('Loading user key');
const key = await get(keyName);
if (!(key instanceof CryptoKey)) return;
useUserStore.getState().setKey(key);
}
export function clearUserKey() {
console.log('Clearing user key');
useUserStore.getState().setKey(null);
return del(keyName);
}