Skip to content

Commit

Permalink
Merge pull request #197 from alphagov/session
Browse files Browse the repository at this point in the history
add session support
  • Loading branch information
joelanman authored Jun 15, 2016
2 parents 4d476b4 + e9d5a39 commit ffb1011
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 1 deletion.
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

0 comments on commit ffb1011

Please sign in to comment.