UNPKG

mpp-sdk

Version:

SDK to talk to the Memento Payments Platform

50 lines (49 loc) 1.77 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.LocalStorage = void 0; const tslib_1 = require("tslib"); const in_memory_storage_1 = require("./in_memory_storage"); const DEFAULT_LOCAL_STORAGE_KEY = "mpp-auth-token"; /** * This class is for testing purposes only. Please read into how to * securely store your auth tokens. */ class LocalStorage { constructor(localStorageKey = DEFAULT_LOCAL_STORAGE_KEY) { this.localStorageKey = localStorageKey; this.cache = new in_memory_storage_1.InMemoryStorage(); } storeAuthToken(authToken) { return tslib_1.__awaiter(this, void 0, void 0, function* () { if (authToken == null) { localStorage.removeItem(this.localStorageKey); } else { localStorage.setItem(this.localStorageKey, JSON.stringify(authToken)); } yield this.cache.storeAuthToken(authToken); }); } clearAuthToken() { return tslib_1.__awaiter(this, void 0, void 0, function* () { localStorage.removeItem(this.localStorageKey); yield this.cache.clearAuthToken(); }); } retrieveAuthToken() { return tslib_1.__awaiter(this, void 0, void 0, function* () { // Check the cache for the auth token first const token = yield this.cache.retrieveAuthToken(); if (token != null) { return token; } // Otherwise we fall back to local storage const val = localStorage.getItem(this.localStorageKey); if (!val) { return null; } return JSON.parse(val); }); } } exports.LocalStorage = LocalStorage;