@c8y/client
Version:
Client application programming interface to access the Cumulocity IoT-Platform REST services.
44 lines • 1.71 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.BearerAuthFromSessionStorage = void 0;
const BearerAuth_1 = require("./BearerAuth");
class BearerAuthFromSessionStorage extends BearerAuth_1.BearerAuth {
/**
* Key for the session storage used to retrieve the bearer token from.
*/
static { this.sessionStorageKey = 'bearerAuthToken'; }
/**
* If the session storage key 'bearerAuthTokenKeep' is set to true, the token will be kept in session storage after it was found.
* Allows you to still refresh the page.
*/
static { this.sessionStorageKeyToKeepToken = `bearerAuthTokenKeep`; }
constructor(throwIfNoTokenPresent = true) {
super();
const token = this.getToken();
if (!token && throwIfNoTokenPresent) {
throw new Error('No token present in session storage');
}
}
updateCredentials({ token }) {
if (!token) {
return;
}
super.updateCredentials({ token });
return;
}
getToken() {
const token = sessionStorage.getItem(BearerAuthFromSessionStorage.sessionStorageKey);
if (token) {
const shouldKeepToken = sessionStorage.getItem(BearerAuthFromSessionStorage.sessionStorageKeyToKeepToken) ===
'true';
if (!shouldKeepToken) {
sessionStorage.removeItem(BearerAuthFromSessionStorage.sessionStorageKey);
}
this.updateCredentials({ token });
return token;
}
return super.getToken();
}
}
exports.BearerAuthFromSessionStorage = BearerAuthFromSessionStorage;
//# sourceMappingURL=BearerAuthFromSessionStorage.js.map