UNPKG

@vmutafov/firebase-auth-node-persistence

Version:

Configurable file-based Firebase Authentication NodeJS persistence

50 lines (49 loc) 1.36 kB
// src/index.ts import { existsSync, readFileSync } from "fs"; import { writeFile } from "fs/promises"; function createNodeFilePersistence(opts) { var _a; function defaultGetStorage() { if (!existsSync(opts.filePath)) { return {}; } const storageContent = readFileSync(opts.filePath, { encoding: "utf-8" }); return JSON.parse(storageContent); } async function defaultSetStorage(storage) { await writeFile(opts.filePath, JSON.stringify(storage, null, 2), { encoding: "utf-8" }); } return _a = class { constructor() { this.type = "NONE"; this.getStorage = opts.getStorage || defaultGetStorage; this.setStorage = opts.setStorage || defaultSetStorage; this.filePath = opts.filePath; this.storage = this.getStorage(); } async _isAvailable() { return true; } async _set(key, value) { this.storage[key] = value; await this.setStorage(this.storage); } async _get(key) { const value = this.storage[key]; return value === void 0 ? null : value; } async _remove(key) { delete this.storage[key]; await this.setStorage(this.storage); } _addListener(_key, _listener) { return; } _removeListener(_key, _listener) { return; } }, _a.type = "NONE", _a; } export { createNodeFilePersistence };