@arc-publishing/sdk-identity
Version:
JS Identity SDK for working with Identity API
48 lines • 2.72 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
var tslib_1 = require("tslib");
var JSONResponseHandler_1 = tslib_1.__importDefault(require("../../serviceHelpers/JSONResponseHandler"));
var SingletonFetch_1 = tslib_1.__importDefault(require("../../serviceHelpers/SingletonFetch"));
var userProfile_1 = require("../userProfile");
var identity_1 = tslib_1.__importDefault(require("../identity"));
var constants_1 = require("../constants");
function getUserProfile() {
return tslib_1.__awaiter(this, void 0, void 0, function () {
var isLoggedIn, localUserProfileExpireTime;
return tslib_1.__generator(this, function (_a) {
switch (_a.label) {
case 0: return [4, identity_1.default.isLoggedIn()];
case 1:
isLoggedIn = _a.sent();
if (!isLoggedIn) {
throw new Error("".concat(constants_1.logPrefix, " Missing or invalid jwt"));
}
localUserProfileExpireTime = localStorage.getItem(constants_1.PROFILE_STORAGE_KEY_EXPIRED_TIME);
if ((identity_1.default === null || identity_1.default === void 0 ? void 0 : identity_1.default.userProfile) &&
localUserProfileExpireTime &&
new Date(localUserProfileExpireTime) > new Date()) {
return [2, identity_1.default.userProfile];
}
return [2, (0, SingletonFetch_1.default)("".concat(identity_1.default.apiOrigin, "/identity/public/v1/profile"), {
method: 'GET',
cache: 'no-cache',
headers: tslib_1.__assign({ Authorization: "Bearer ".concat(identity_1.default.userIdentity.accessToken) }, constants_1.headers)
}, 'getUserProfile')
.then(JSONResponseHandler_1.default)
.then(function (json) {
if ((0, userProfile_1.isUserProfile)(json)) {
var expiredTime = new Date(Date.now() + 10 * 60 * 1000);
localStorage.setItem(constants_1.PROFILE_STORAGE_KEY_EXPIRED_TIME, expiredTime.toISOString());
identity_1.default.userProfile = json;
return identity_1.default.userProfile;
}
else {
throw json;
}
})];
}
});
});
}
exports.default = getUserProfile;
//# sourceMappingURL=index.js.map