@dunite/check-access
Version:
Handles authentication for the corresponding AuthMiddle-nuget
123 lines (121 loc) • 6.13 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.CheckAccess = void 0;
var tslib_1 = require("tslib");
var aurelia_framework_1 = require("aurelia-framework");
var aurelia_fetch_client_1 = require("aurelia-fetch-client");
var aurelia_event_aggregator_1 = require("aurelia-event-aggregator");
var CheckAccess = (function () {
function CheckAccess(eventAggregator, httpClient) {
var _this = this;
this.log = aurelia_framework_1.LogManager.getLogger('dualite');
this.cacheKey = "NoCacheKey";
this.src = "";
this.messageListener = function (event) {
_this.log.info("in message from Host. origin=" + event.origin + " appdomain=" + _this.CheckAccessUrl);
if ((_this.CheckAccessUrl.indexOf(event.origin) >= 0)) {
_this.log.info("origin ok, message:" + JSON.stringify(event.data));
if (event.data.status === "OK") {
try {
window.localStorage.setItem("cacheKey" + _this.ProjectId, event.data.cacheKey);
_this.messageBar.show("Du är nu inloggad", "", "info");
setTimeout(function () { console.log(_this); _this.messageBar.hide(); }, 5000);
_this.PublishUserIsLoggedIn();
}
catch (error) {
_this.log.info("Localstorage could not be used");
}
window.removeEventListener("message", _this.messageListener, false);
}
else {
_this.messageBar.show("Det gick inte att logga in. Kontakta administratör.", "", "error");
}
}
};
if (typeof httpClient == 'string') {
throw Error("httpClient is null in CheckAccess. Make sure the plugin is initiated. If under test call aurelia.container.autoRegister('CheckAccessHttpClient', with proper mock.");
}
this.httpClient = httpClient;
this.eventAggregator = eventAggregator;
this.log.info("CheckAccess constructor");
window.addEventListener("message", this.messageListener);
}
CheckAccess.prototype.showAccessDenied = function (url) {
this.log.info("in showAccessDenied");
this.messageBar.show("Du loggas nu in mot Dualite", "Strax klart", "info");
this.src = url;
};
CheckAccess.prototype.attached = function () {
return tslib_1.__awaiter(this, void 0, void 0, function () {
var response, data, error_1;
var _this = this;
return tslib_1.__generator(this, function (_a) {
switch (_a.label) {
case 0:
this.log.info("check-access attached");
this.cacheKey = window.localStorage.getItem("cacheKey" + this.ProjectId);
this.log.info("cacheKey from storage: " + this.cacheKey);
this.httpClient.configure(function (config) {
config
.withBaseUrl(_this.CheckAccessUrl)
.withDefaults({
headers: {
"Accept": "application/json",
"CacheKey": _this.cacheKey == null ? "" : _this.cacheKey,
"ProjectID": _this.ProjectId
}
});
});
_a.label = 1;
case 1:
_a.trys.push([1, 6, , 7]);
return [4, this.httpClient.post("api/token")];
case 2:
response = _a.sent();
this.log.info("fetch('response')" + JSON.stringify(response));
if (!response.ok) return [3, 4];
return [4, response.json()];
case 3:
data = _a.sent();
if (data.ErrorMessage != null || data.ErrorMessage === "") {
this.log.info("checkaccess failed, will redirect");
this.showAccessDenied(data.RedirectUrl);
}
else {
this.log.info("fetch('checkaccess succeed')");
this.PublishUserIsLoggedIn();
}
return [3, 5];
case 4:
this.log.info("checkaccess failed, can not redirect");
_a.label = 5;
case 5: return [3, 7];
case 6:
error_1 = _a.sent();
this.log.info("fetch('checkaccess failed')" + JSON.stringify(error_1));
this.messageBar.show("Det går inte att logga in. Kontakta administratör.", "Får inte kontakt svar på servern.", "error");
return [3, 7];
case 7: return [2];
}
});
});
};
CheckAccess.prototype.PublishUserIsLoggedIn = function () {
this.eventAggregator.publish("UserIsLoggedIn", { cacheKey: this.cacheKey });
};
tslib_1.__decorate([
aurelia_framework_1.bindable,
tslib_1.__metadata("design:type", String)
], CheckAccess.prototype, "ProjectId", void 0);
tslib_1.__decorate([
aurelia_framework_1.bindable,
tslib_1.__metadata("design:type", String)
], CheckAccess.prototype, "CheckAccessUrl", void 0);
CheckAccess = tslib_1.__decorate([
(0, aurelia_framework_1.inject)(aurelia_event_aggregator_1.EventAggregator, "CheckAccessHttpClient"),
tslib_1.__metadata("design:paramtypes", [aurelia_event_aggregator_1.EventAggregator, aurelia_fetch_client_1.HttpClient])
], CheckAccess);
return CheckAccess;
}());
exports.CheckAccess = CheckAccess;
//# sourceMappingURL=check-access.js.map