@arc-publishing/sdk-identity
Version:
JS Identity SDK for working with Identity API
57 lines • 2.37 kB
JavaScript
import { __assign } from "tslib";
import { headers, logPrefix } from './constants';
import JSONResponseHandler from '../serviceHelpers/JSONResponseHandler';
import SingletonFetch from '../serviceHelpers/SingletonFetch';
import { isUserIdentity } from './userIdentity';
import Identity from './identity';
import { isJwtExpired, jwtHasExp } from '../utils/jwt';
export function extendSession() {
if (Identity.userIdentity.refreshToken &&
Identity.userIdentity.accessToken &&
!isJwtExpired(Identity.userIdentity.refreshToken)) {
return SingletonFetch("".concat(Identity.apiOrigin, "/identity/public/v1/auth/token"), {
method: 'POST',
headers: headers,
body: JSON.stringify({
token: Identity.userIdentity.refreshToken,
grantType: 'refresh-token'
})
}, 'extendSession')
.then(JSONResponseHandler)
.then(function (json) {
if (isUserIdentity(json)) {
var userIdentity = __assign(__assign({}, Identity.userIdentity), { accessToken: json.accessToken, refreshToken: json.refreshToken || Identity.userIdentity.refreshToken });
Identity.userIdentity = userIdentity;
return Identity.userIdentity;
}
else {
if (json.code) {
console.warn("".concat(logPrefix, " code:\"").concat(json.code, "\", message:\"").concat(json.message, "\""));
}
Identity.clearSession();
throw json;
}
});
}
else if (Identity.userIdentity.refreshToken &&
isJwtExpired(Identity.userIdentity.refreshToken)) {
Identity.clearSession();
throw new Error("".concat(logPrefix, " Refresh token is expired"));
}
else {
Identity.clearSession();
throw new Error("".concat(logPrefix, " No refresh token found for this session"));
}
}
export function heartbeat() {
if (Identity.userIdentity.refreshToken &&
Identity.userIdentity.accessToken &&
(isJwtExpired(Identity.userIdentity.accessToken) ||
!jwtHasExp(Identity.userIdentity.accessToken))) {
return Identity.extendSession();
}
else {
return Promise.resolve(Identity.userIdentity);
}
}
//# sourceMappingURL=extendSession.js.map