swaf/dist/components/SessionComponent.js
2020-04-22 17:49:58 +02:00

59 lines
6.6 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 session from "express-session";
import config from "config";
import flash from "connect-flash";
export default class SessionComponent extends ApplicationComponent {
constructor(storeComponent) {
super();
this.storeComponent = storeComponent;
}
start(app, router) {
return __awaiter(this, void 0, void 0, function* () {
router.use(session({
saveUninitialized: true,
secret: config.get('session.secret'),
store: this.storeComponent.getStore(),
resave: true,
cookie: {
httpOnly: true,
secure: config.get('session.cookie.secure'),
},
rolling: true,
}));
router.use(flash());
router.use((req, res, next) => {
if (!req.session) {
throw new Error('Session is unavailable.');
}
res.locals.session = req.session;
let _flash = null;
res.locals.flash = () => {
if (!_flash) {
_flash = {
info: req.flash('info'),
success: req.flash('success'),
warning: req.flash('warning'),
error: req.flash('error'),
};
}
return _flash;
};
next();
});
});
}
stop() {
return __awaiter(this, void 0, void 0, function* () {
});
}
}
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiU2Vzc2lvbkNvbXBvbmVudC5qcyIsInNvdXJjZVJvb3QiOiIuLyIsInNvdXJjZXMiOlsiY29tcG9uZW50cy9TZXNzaW9uQ29tcG9uZW50LnRzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiI7Ozs7Ozs7OztBQUFBLE9BQU8sb0JBQW9CLE1BQU0seUJBQXlCLENBQUM7QUFDM0QsT0FBTyxPQUFPLE1BQU0saUJBQWlCLENBQUM7QUFDdEMsT0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AgICAgIH1cblxuICAgICAgICAgICAgcmVzLmxvY2Fscy5zZXNzaW9uID0gcmVxLnNlc3Npb247XG5cbiAgICAgICAgICAgIGxldCBfZmxhc2g6IGFueSA9IG51bGw7XG4gICAgICAgICAgICByZXMubG9jYWxzLmZsYXNoID0gKCkgPT4ge1xuICAgICAgICAgICAgICAgIGlmICghX2ZsYXNoKSB7XG4gICAgICAgICAgICAgICAgICAgIF9mbGFzaCA9IHtcbiAgICAgICAgICAgICAgICAgICAgICAgIGluZm86IHJlcS5mbGFzaCgnaW5mbycpLFxuICAgICAgICAgICAgICAgICAgICAgICAgc3VjY2VzczogcmVxLmZsYXNoKCdzdWNjZXNzJyksXG4gICAgICAgICAgICAgICAgICAgICAgICB3YXJuaW5nOiByZXEuZmxhc2goJ3dhcm5pbmcnKSxcbiAgICAgICAgICAgICAgICAgICAgICAgIGVycm9yOiByZXEuZmxhc2goJ2Vycm9yJyksXG4gICAgICAgICAgICAgICAgICAgIH07XG4gICAgICAgICAgICAgICAgfVxuICAgICAgICAgICAgICAgIHJldHVybiBfZmxhc2g7XG4gICAgICAgICAgICB9O1xuICAgICAgICAgICAgbmV4dCgpO1xuICAgICAgICB9KTtcbiAgICB9XG5cbiAgICBwdWJsaWMgYXN5bmMgc3RvcCgpOiBQcm9taXNlPHZvaWQ+IHtcbiAgICB9XG59Il19