msal
Version:
Microsoft Authentication Library for js
108 lines • 3.65 kB
JavaScript
/*
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License.
*/
import { ClientConfigurationError } from "../error/ClientConfigurationError";
import { AuthError } from "../error/AuthError";
/**
* @hidden
*/
var BrowserStorage = /** @class */ (function () {
function BrowserStorage(cacheLocation) {
if (!window) {
throw AuthError.createNoWindowObjectError("Browser storage class could not find window object");
}
var storageSupported = typeof window[cacheLocation] !== "undefined" && window[cacheLocation] !== null;
if (!storageSupported) {
throw ClientConfigurationError.createStorageNotSupportedError(cacheLocation);
}
this.cacheLocation = cacheLocation;
}
/**
* add value to storage
* @param key
* @param value
* @param enableCookieStorage
*/
BrowserStorage.prototype.setItem = function (key, value, enableCookieStorage) {
window[this.cacheLocation].setItem(key, value);
if (enableCookieStorage) {
this.setItemCookie(key, value);
}
};
/**
* get one item by key from storage
* @param key
* @param enableCookieStorage
*/
BrowserStorage.prototype.getItem = function (key, enableCookieStorage) {
if (enableCookieStorage && this.getItemCookie(key)) {
return this.getItemCookie(key);
}
return window[this.cacheLocation].getItem(key);
};
/**
* remove value from storage
* @param key
*/
BrowserStorage.prototype.removeItem = function (key) {
return window[this.cacheLocation].removeItem(key);
};
/**
* clear storage (remove all items from it)
*/
BrowserStorage.prototype.clear = function () {
return window[this.cacheLocation].clear();
};
/**
* add value to cookies
* @param cName
* @param cValue
* @param expires
*/
BrowserStorage.prototype.setItemCookie = function (cName, cValue, expires) {
var cookieStr = encodeURIComponent(cName) + "=" + encodeURIComponent(cValue) + ";path=/;";
if (expires) {
var expireTime = this.getCookieExpirationTime(expires);
cookieStr += "expires=" + expireTime + ";";
}
document.cookie = cookieStr;
};
/**
* get one item by key from cookies
* @param cName
*/
BrowserStorage.prototype.getItemCookie = function (cName) {
var name = encodeURIComponent(cName) + "=";
var ca = document.cookie.split(";");
for (var i = 0; i < ca.length; i++) {
var c = ca[i];
while (c.charAt(0) === " ") {
c = c.substring(1);
}
if (c.indexOf(name) === 0) {
return decodeURIComponent(c.substring(name.length, c.length));
}
}
return "";
};
/**
* Clear an item in the cookies by key
* @param cName
*/
BrowserStorage.prototype.clearItemCookie = function (cName) {
this.setItemCookie(cName, "", -1);
};
/**
* Get cookie expiration time
* @param cookieLifeDays
*/
BrowserStorage.prototype.getCookieExpirationTime = function (cookieLifeDays) {
var today = new Date();
var expr = new Date(today.getTime() + cookieLifeDays * 24 * 60 * 60 * 1000);
return expr.toUTCString();
};
return BrowserStorage;
}());
export { BrowserStorage };
//# sourceMappingURL=BrowserStorage.js.map