diff --git a/package.json b/package.json index b15f5d45..edc93812 100644 --- a/package.json +++ b/package.json @@ -48,6 +48,7 @@ "koa-mount": "^2.0.0", "koa-query": "0.0.2", "koa-route": "^3.1.0", + "koa-session": "^4.0.0", "koa-static": "^3.0.0", "moment": "^2.18.1", "mongodb": "^2.1.14", diff --git a/src/server.js b/src/server.js index 994dd8e5..b04ebfa8 100644 --- a/src/server.js +++ b/src/server.js @@ -9,6 +9,7 @@ import etag from 'koa-etag' import helmet from 'koa-helmet' import mount from 'koa-mount' import cookie from 'koa-cookie' +import session from 'koa-session' import bodyParser from 'koa-bodyparser' import apiV1 from 'hudozka-api-v1' @@ -44,10 +45,19 @@ const collectiveOrder = [ 'nv-andreeva', ] +const sessionConfig = { + key: 'sid', /** (string) cookie key */ + maxAge: 86400000, /** (number) maxAge in ms */ + overwrite: false, /** (boolean) can overwrite or not */ + httpOnly: false, /** (boolean) httpOnly or not */ + signed: true, /** (boolean) signed or not */ +} + export default function (store) { const $ = convert const app = new Koa() + app.keys = ['1234'] app.proxy = true app.use(bodyParser({ @@ -57,6 +67,7 @@ export default function (store) { })) app.use(logger()) app.use(apis(store)) + app.use(convert(session(sessionConfig, app))) app.use(cookie()) app.use($(conditional())) app.use($(etag())) @@ -65,6 +76,14 @@ export default function (store) { app.use($(helmet())) app.use(queryObject()) + app.use(async (ctx, next) => { + if (ctx.session.isNew) { + ctx.session.startDate = new Date() + } + + await next() + }) + app.use(error.notFound(config.view404)) app.use(sitemap())