UNPKG

@brionmario-experimental/asgardeo-auth-spa

Version:

Asgardeo Auth SPA SDK to be used in Single-Page Applications.

73 lines 3.45 kB
/** * Copyright (c) 2020, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. * * WSO2 Inc. licenses this file to you under the Apache License, * Version 2.0 (the "License"); you may not use this file except * in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, * software distributed under the License is distributed on an * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * KIND, either express or implied. See the License for the * specific language governing permissions and limitations * under the License. */ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; import { REFRESH_TOKEN_TIMER } from "@asgardeo/auth-js"; export class SPAHelper { constructor(authClient) { this._authenticationClient = authClient; this._dataLayer = this._authenticationClient.getDataLayer(); } refreshAccessTokenAutomatically(authenticationHelper) { var _a, _b; return __awaiter(this, void 0, void 0, function* () { const shouldRefreshAutomatically = (_b = (_a = (yield this._dataLayer.getConfigData())) === null || _a === void 0 ? void 0 : _a.periodicTokenRefresh) !== null && _b !== void 0 ? _b : false; if (!shouldRefreshAutomatically) { return; } const sessionData = yield this._dataLayer.getSessionData(); if (sessionData.refresh_token) { // Refresh 10 seconds before the expiry time const expiryTime = parseInt(sessionData.expires_in); const time = expiryTime <= 10 ? expiryTime : expiryTime - 10; const timer = setTimeout(() => __awaiter(this, void 0, void 0, function* () { yield authenticationHelper.refreshAccessToken(); }), time * 1000); yield this._dataLayer.setTemporaryDataParameter(REFRESH_TOKEN_TIMER, JSON.stringify(timer)); } }); } getRefreshTimeoutTimer() { return __awaiter(this, void 0, void 0, function* () { if (yield this._dataLayer.getTemporaryDataParameter(REFRESH_TOKEN_TIMER)) { return JSON.parse((yield this._dataLayer.getTemporaryDataParameter(REFRESH_TOKEN_TIMER))); } return -1; }); } clearRefreshTokenTimeout(timer) { return __awaiter(this, void 0, void 0, function* () { if (timer) { clearTimeout(timer); return; } const refreshTimer = yield this.getRefreshTimeoutTimer(); if (refreshTimer !== -1) { clearTimeout(refreshTimer); } }); } } //# sourceMappingURL=spa-helper.js.map