52 lines
6.3 KiB
JavaScript
52 lines
6.3 KiB
JavaScript
|
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
||
|
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
||
|
return new (P || (P = Promise))(function (resolve, reject) {
|
||
|
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
||
|
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
||
|
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
||
|
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
||
|
});
|
||
|
};
|
||
|
import ApplicationComponent from "../ApplicationComponent";
|
||
|
import onFinished from "on-finished";
|
||
|
import Logger from "../Logger";
|
||
|
import { ServerError } from "../HttpError";
|
||
|
export default class RedirectBackComponent extends ApplicationComponent {
|
||
|
start(app, router) {
|
||
|
return __awaiter(this, void 0, void 0, function* () {
|
||
|
router.use((req, res, next) => {
|
||
|
if (!req.session) {
|
||
|
throw new Error('Session is unavailable.');
|
||
|
}
|
||
|
onFinished(res, (err) => {
|
||
|
if (!err && res.statusCode === 200) {
|
||
|
req.session.previousUrl = req.originalUrl;
|
||
|
Logger.debug('Prev url set to', req.session.previousUrl);
|
||
|
req.session.save((err) => {
|
||
|
if (err) {
|
||
|
Logger.error(err, 'Error while saving session');
|
||
|
}
|
||
|
});
|
||
|
}
|
||
|
});
|
||
|
res.redirectBack = (defaultUrl) => {
|
||
|
if (req.session && typeof req.session.previousUrl === 'string') {
|
||
|
res.redirect(req.session.previousUrl);
|
||
|
}
|
||
|
else if (typeof defaultUrl === 'string') {
|
||
|
res.redirect(defaultUrl);
|
||
|
}
|
||
|
else {
|
||
|
throw new ServerError('There is no previous url and no default redirection url was provided.');
|
||
|
}
|
||
|
};
|
||
|
next();
|
||
|
});
|
||
|
});
|
||
|
}
|
||
|
stop() {
|
||
|
return __awaiter(this, void 0, void 0, function* () {
|
||
|
});
|
||
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XBvcnQgTG9nZ2VyIGZyb20gXCIuLi9Mb2dnZXJcIjtcbmltcG9ydCB7U2VydmVyRXJyb3J9IGZyb20gXCIuLi9IdHRwRXJyb3JcIjtcblxuZXhwb3J0IGRlZmF1bHQgY2xhc3MgUmVkaXJlY3RCYWNrQ29tcG9uZW50IGV4dGVuZHMgQXBwbGljYXRpb25Db21wb25lbnQ8dm9pZD4ge1xuICAgIHB1YmxpYyBhc3luYyBzdGFydChhcHA6IEV4cHJlc3MsIHJvdXRlcjogUm91dGVyKTogUHJvbWlzZTx2b2lkPiB7XG4gICAgICAgIHJvdXRlci51c2UoKHJlcSwgcmVzLCBuZXh0KSA9PiB7XG4gICAgICAgICAgICBpZiAoIXJlcS5zZXNzaW9uKSB7XG4gICAgICAgICAgICAgICAgdGhyb3cgbmV3IEVycm9yKCdTZXNzaW9uIGlzIHVuYXZhaWxhYmxlLicpO1xuICAgICAgICAgICAgfVxuXG4gICAgICAgICAgICBvbkZpbmlzaGVkKHJlcywgKGVycikgPT4ge1xuICAgICAgICAgICAgICAgIGlmICghZXJyICYmIHJlcy5zdGF0dXNDb2RlID09PSAyMDApIHtcbiAgICAgICAgICAgICAgICAgICAgcmVxLnNlc3Npb24hLnByZXZpb3VzVXJsID0gcmVxLm9yaWdpbmFsVXJsO1xuICAgICAgICAgICAgICAgICAgICBMb2dnZXIuZGVidWcoJ1ByZXYgdXJsIHNldCB0bycsIHJlcS5zZXNzaW9uIS5wcmV2aW91c1VybCk7XG4gICAgICAgICAgICAgICAgICAgIHJlcS5zZXNzaW9uIS5zYXZlKChlcnIpID0+IHtcbiAgICAgICAgICAgICAgICAgICAgICAgIGlmIChlcnIpIHtcbiAgICAgICAgICAgICAgICAgICAgICAgICAgICBMb2dnZXIuZXJyb3IoZXJyLCAnRXJyb3Igd2hpbGUgc2F2aW5nIHNlc3Npb24nKTtcbiAgICAgICAgICAgICAgICAgICAgICAgIH1cbiAgICAgICAgICAgICAgICAgICAgfSk7XG4gICAgICAgICAgICAgICAgfVxuICAgICAgICAgICAgfSk7XG5cbiAgICAgICAgICAgIHJlcy5yZWRpcmVjdEJhY2sgPSAoZGVmYXVsdFVybD86IHN0cmluZykgPT4ge1xuICAgICAgICAgICAgICAgIGlmIChyZXEuc2Vzc2lvbiAmJiB0eXBlb2YgcmVxLnNlc3Npb24ucHJldmlvdXNVcmwgPT09ICdzdHJpbmcnKSB7XG4gICAgICAgICAgICAgICAgICAgIHJlcy5yZWRpcmVjdChyZXEuc2Vzc2lvbi5wcmV2aW91c1VybCk7XG4gICAgICAgICAgICAgICAgfSBlbHNlIGlmICh0eXBlb2YgZGVmYXVsdFVybCA9PT0gJ3N0cmluZycpIHtcbiAgICAgICAgICAgICAgICAgICAgcmVzLnJlZGlyZWN0KGRlZmF1bHRVcmwpO1xuICAgICAgICAgICAgICAgIH0gZWxzZSB7XG4gICAgICAgICAgICAgICAgICAgIHRocm93IG5ldyBTZXJ2ZXJFcnJvcignVGhlcmUgaXMgbm8gcHJldmlvdXMgdXJsIGFuZCBubyBkZWZhdWx0IHJlZGlyZWN0aW9uIHVybCB3YXMgcHJvdmlkZWQuJyk7XG4gICAgICAgICAgICAgICAgfVxuICAgICAgICAgICAgfTtcblxuICAgICAgICAgICAgbmV4dCgpO1xuICAgICAgICB9KTtcbiAgICB9XG5cbiAgICBwdWJsaWMgYXN5bmMgc3RvcCgpOiBQcm9taXNlPHZvaWQ+IHtcbiAgICB9XG5cbn0iXX0=
|