UNPKG

@arc-publishing/sdk-identity

Version:
45 lines 2.4 kB
import { __assign, __awaiter, __generator } from "tslib"; import JSONResponseHandler from '../../serviceHelpers/JSONResponseHandler'; import SingletonFetch from '../../serviceHelpers/SingletonFetch'; import { isUserProfile } from '../userProfile'; import Identity from '../identity'; import { headers, logPrefix, PROFILE_STORAGE_KEY_EXPIRED_TIME } from '../constants'; export default function getUserProfile() { return __awaiter(this, void 0, void 0, function () { var isLoggedIn, localUserProfileExpireTime; return __generator(this, function (_a) { switch (_a.label) { case 0: return [4, Identity.isLoggedIn()]; case 1: isLoggedIn = _a.sent(); if (!isLoggedIn) { throw new Error("".concat(logPrefix, " Missing or invalid jwt")); } localUserProfileExpireTime = localStorage.getItem(PROFILE_STORAGE_KEY_EXPIRED_TIME); if ((Identity === null || Identity === void 0 ? void 0 : Identity.userProfile) && localUserProfileExpireTime && new Date(localUserProfileExpireTime) > new Date()) { return [2, Identity.userProfile]; } return [2, SingletonFetch("".concat(Identity.apiOrigin, "/identity/public/v1/profile"), { method: 'GET', cache: 'no-cache', headers: __assign({ Authorization: "Bearer ".concat(Identity.userIdentity.accessToken) }, headers) }, 'getUserProfile') .then(JSONResponseHandler) .then(function (json) { if (isUserProfile(json)) { var expiredTime = new Date(Date.now() + 10 * 60 * 1000); localStorage.setItem(PROFILE_STORAGE_KEY_EXPIRED_TIME, expiredTime.toISOString()); Identity.userProfile = json; return Identity.userProfile; } else { throw json; } })]; } }); }); } //# sourceMappingURL=index.js.map