Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add session support #197

Merged
merged 3 commits into from
Jun 15, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,4 @@ Installation guide for developers (technical):
- [Updating the kit to the latest version](updating-the-kit.md)
- [Tips and tricks](tips-and-tricks.md)
- [Creating routes (server-side programming)](creating-routes.md)
- [Storing data in session](session.md)
23 changes: 23 additions & 0 deletions docs/session.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Storing data in session

**Advanced topic**

If you need to store data for each user, the best way to do it is using session data.

This means that if more than one person is using your prototype, their data will not get mixed up.

The easiest way to clear session data is to use 'Incognito mode' for each user, and close that window when you're done.

## How to use

In a route function, refer to `req.session`.

For example you might have `req.session.over18` or `req.session.firstName`.

You can see a full example here:

[https://github.com/expressjs/session#example](https://github.com/expressjs/session#example)

You can read more about Express Session here:

[https://github.com/expressjs/session](https://github.com/expressjs/session)
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"consolidate": "0.x",
"express": "4.13.3",
"express-nunjucks": "^0.9.3",
"express-session": "^1.13.0",
"express-writer": "0.0.4",
"govuk-elements-sass": "1.2.0",
"govuk_frontend_toolkit": "^4.12.0",
Expand Down
10 changes: 9 additions & 1 deletion server.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
var path = require('path'),
express = require('express'),
browserSync = require('browser-sync'),
session = require('express-session'),
nunjucks = require('express-nunjucks'),
routes = require(__dirname + '/app/routes.js'),
favicon = require('serve-favicon'),
app = express(),
basicAuth = require('basic-auth'),
bodyParser = require('body-parser'),
browserSync = require('browser-sync'),
config = require(__dirname + '/app/config.js'),
port = (process.env.PORT || config.port),
utils = require(__dirname + '/lib/utils.js'),
Expand Down Expand Up @@ -66,6 +67,13 @@ app.use(bodyParser.urlencoded({
extended: true
}));

// Support session data
app.use(session({
resave: false,
saveUninitialized: false,
secret: Math.round(Math.random()*100000).toString()
}));

// send assetPath to all views
app.use(function (req, res, next) {
res.locals.asset_path="/public/";
Expand Down