UNPKG

@bitblit/ratchet-warden-common

Version:

Typescript library to simplify using simplewebauthn and secondary auth methods over GraphQL

51 lines 1.83 kB
import { RequireRatchet } from "@bitblit/ratchet-common/lang/require-ratchet"; import { StringRatchet } from "@bitblit/ratchet-common/lang/string-ratchet"; import { Logger } from "@bitblit/ratchet-common/logger/logger"; export class WardenClientStorageBasedLoggedInUserProvider { storageProv; storageKey; constructor(storageProv, storageKey) { this.storageProv = storageProv; this.storageKey = storageKey; RequireRatchet.notNullUndefinedOrOnlyWhitespaceString(this.storageKey, 'storageKey'); RequireRatchet.notNullOrUndefined(this.storageProv, 'storageProv'); } get storage() { if (typeof this.storageProv === 'function') { return this.storageProv(); } else { return this.storageProv; } } fetchLoggedInUserWrapper() { const storage = this.storage; if (storage) { const asString = storage.getItem(this.storageKey); const rval = StringRatchet.trimToNull(asString) ? JSON.parse(asString) : null; return rval; } else { Logger.debug('Tried to fetch logged in user before storage ready - returning null'); return null; } } logOutUser() { this.setLoggedInUserWrapper(null); } setLoggedInUserWrapper(wrapper) { const storage = this.storage; if (storage) { if (wrapper) { storage.setItem(this.storageKey, JSON.stringify(wrapper)); } else { storage.removeItem(this.storageKey); } } else { Logger.warn('Tried to set logged in user before storage was ready, ignoring : %j', wrapper); } } } //# sourceMappingURL=warden-client-storage-based-logged-in-user-provider.js.map