@dudadev/ms-client-auth
Version:
Client to authenticate Duda's microservices' clients
136 lines (106 loc) • 3.33 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.create = create;
function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } }
function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; }
class AuthService {
constructor({
autoAuthInterval,
siteAlias,
retries = 2
} = {}) {
this._listeners = [];
this._interval = 0;
this._timeout = 0;
this._errorRetries = retries;
this._erroredCount = 0;
this._siteAlias = siteAlias;
this._ready = new Promise(() => {});
if (autoAuthInterval > 0) {
this._ready = this.setAutoAuthInterval(autoAuthInterval);
}
}
get token() {
if (!this.authDetails) {
return null;
}
return this.authDetails.value;
}
get ready() {
return this._ready;
}
setAutoAuthInterval(interval) {
var _this = this;
return _asyncToGenerator(function* () {
clearTimeout(_this._timeout);
_this._interval = interval;
_this._registerNextAuth();
return _this.authenticateServices();
})();
}
_registerNextAuth() {
this._timeout = setTimeout(() => {
if (this._erroredCount < this._errorRetries) {
this.authenticateServices();
this._registerNextAuth();
} else {
clearTimeout(this._timeout);
}
}, this._interval);
}
authenticateServices() {
var _this2 = this;
return _asyncToGenerator(function* () {
if (_this2._destroyed) {
throw new Error('called `authenticate` on a destroyed instance');
}
_this2.authDetails = yield _this2._fetchAuthDetails();
_this2._listeners.forEach(listener => listener(_this2.authDetails));
if (_this2._interval > 0) {
clearTimeout(_this2._timeout);
_this2._registerNextAuth();
}
return _this2.authDetails;
})();
}
onAuth(listener) {
this._listeners = [...this._listeners, listener];
listener(this.authDetails);
return () => {
this._listeners = this._listeners.filter(fn => fn !== listener);
};
}
destroy() {
this._listeners.forEach(listener => listener(null));
this._listeners = [];
this._destroyed = true;
}
_fetchAuthDetails() {
var _this3 = this;
return _asyncToGenerator(function* () {
const query = _this3._siteAlias ? `?siteAlias=${_this3._siteAlias}` : '';
const url = `/api/ms/auth${query}`;
const response = yield window.fetch(url, {
credentials: 'include'
});
if (response.ok) {
return response.json();
}
_this3._erroredCount++;
return null;
})();
}
}
function create({
autoAuthInterval,
siteAlias,
retries
} = {}) {
return new AuthService({
autoAuthInterval,
siteAlias,
retries
});
}