29 lines
797 B
JavaScript
29 lines
797 B
JavaScript
const { randomUUID } = require('node:crypto');
|
|
|
|
function createAccountStore(secureStore, { createId = randomUUID } = {}) {
|
|
return {
|
|
async load() {
|
|
try {
|
|
const stored = await secureStore.load();
|
|
if (Array.isArray(stored.accounts)) return stored;
|
|
if (stored.email) {
|
|
const account = { ...stored, id: stored.id || createId() };
|
|
const migrated = { accounts: [account], activeId: account.id };
|
|
await secureStore.save(migrated);
|
|
return migrated;
|
|
}
|
|
return { accounts: [], activeId: null };
|
|
} catch (error) {
|
|
if (error.code === 'ENOENT') return null;
|
|
throw error;
|
|
}
|
|
},
|
|
|
|
save(store) {
|
|
return secureStore.save(store);
|
|
}
|
|
};
|
|
}
|
|
|
|
module.exports = { createAccountStore };
|