@dunite/check-access
Version:
Handles authentication for the corresponding AuthMiddle-nuget
120 lines (118 loc) • 5.81 kB
JavaScript
import { __awaiter, __decorate, __generator, __metadata } from "tslib";
import { bindable, inject, LogManager } from "aurelia-framework";
import { HttpClient } from "aurelia-fetch-client";
import { EventAggregator } from "aurelia-event-aggregator";
var CheckAccess = (function () {
function CheckAccess(eventAggregator, httpClient) {
var _this = this;
this.log = 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 __awaiter(this, void 0, void 0, function () {
var response, data, error_1;
var _this = this;
return __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 });
};
__decorate([
bindable,
__metadata("design:type", String)
], CheckAccess.prototype, "ProjectId", void 0);
__decorate([
bindable,
__metadata("design:type", String)
], CheckAccess.prototype, "CheckAccessUrl", void 0);
CheckAccess = __decorate([
inject(EventAggregator, "CheckAccessHttpClient"),
__metadata("design:paramtypes", [EventAggregator, HttpClient])
], CheckAccess);
return CheckAccess;
}());
export { CheckAccess };
//# sourceMappingURL=check-access.js.map