Skip to content

Commit

Permalink
Add cookie banner component
Browse files Browse the repository at this point in the history
Avoid using JavaScript by using server side cookie setting.

JavaScript is only necessary for www.GOV.UK since it can't easily set cookies.
  • Loading branch information
NickColley committed Jun 26, 2018
1 parent de73133 commit 5f3a671
Show file tree
Hide file tree
Showing 10 changed files with 77 additions and 0 deletions.
3 changes: 3 additions & 0 deletions app/assets/sass/application.scss
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,7 @@ $govuk-global-styles: true;
@import "patterns/task-list";
@import "patterns/related-items";

// Components that aren't in Frontend
@import "components/cookie-banner";

// Add extra styles here, or re-organise the Sass files in whichever way makes most sense to you
24 changes: 24 additions & 0 deletions app/assets/sass/components/_cookie-banner.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
.app-cookie-banner {
@include govuk-font(16);
@include govuk-text-colour;

box-sizing: border-box;
width: 100%;

padding-top: govuk-spacing(3);
padding-right: govuk-spacing(3);
padding-bottom: govuk-spacing(3);
padding-left: govuk-spacing(3);
background-color: lighten(desaturate(govuk-colour("light-blue"), 8.46), 42.55);
}

.app-cookie-banner__message {
margin: 0;
@include govuk-width-container;
}

@include govuk-media-query($media-type: print) {
.app-cookie-banner {
display: none !important;
}
}
7 changes: 7 additions & 0 deletions app/views/includes/cookie-banner.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{% if shouldShowCookieMessage %}
<div class="app-cookie-banner">
<p class="app-cookie-banner__message">
{{cookieText | safe }}
</p>
</div>
{% endif %}
1 change: 1 addition & 0 deletions app/views/layout.html
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
{% endblock %}

{% block header %}
{% include "includes/cookie-banner.html" %}
{# Set serviceName in config.js. #}
{{ govukHeader({
homepageUrl: "/",
Expand Down
1 change: 1 addition & 0 deletions docs/assets/sass/docs.scss
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
@import "node_modules/govuk-frontend/all";

// Add Sass specific to /docs here
@import "../../../app/assets/sass/components/cookie-banner";

// adjust code block font-size to 19px
.hljs {
Expand Down
7 changes: 7 additions & 0 deletions docs/views/includes/cookie-banner.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{% if shouldShowCookieMessage %}
<div class="app-cookie-banner">
<p class="app-cookie-banner__message">
{{cookieText | safe }}
</p>
</div>
{% endif %}
1 change: 1 addition & 0 deletions docs/views/layout.html
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
{% endblock %}

{% block header %}
{% include "includes/cookie-banner.html" %}
{# Set serviceName in config.js. #}
{{ govukHeader({
"homepageUrl": "/",
Expand Down
22 changes: 22 additions & 0 deletions lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -284,3 +284,25 @@ exports.autoStoreData = function (req, res, next) {

next()
}

exports.handleCookies = function (app) {
return function handleCookies (req, res, next) {
const COOKIE_NAME = 'seen_cookie_message'
let cookie = req.cookies[COOKIE_NAME]

if (cookie === 'yes') {
app.locals.shouldShowCookieMessage = false
return next()
}

let maxAgeInDays = 28
res.cookie(COOKIE_NAME, 'yes', {
maxAge: maxAgeInDays * 24 * 60 * 60 * 1000,
httpOnly: true
})

app.locals.shouldShowCookieMessage = true

next()
}
}
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,5 +46,8 @@
"ignore": [
"nunjucks"
]
},
"devDependencies": {
"cookie-parser": "^1.4.3"
}
}
8 changes: 8 additions & 0 deletions server.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const dotenv = require('dotenv')
const express = require('express')
const nunjucks = require('nunjucks')
const session = require('express-session')
const cookieParser = require('cookie-parser')

// Local dependencies
const config = require('./app/config.js')
Expand All @@ -21,6 +22,13 @@ const app = express()
const documentationApp = express()
dotenv.config()

// Set cookies for use in cookie banner.
app.use(cookieParser())
documentationApp.use(cookieParser())
const handleCookies = utils.handleCookies(app)
app.use(handleCookies)
documentationApp.use(handleCookies)

// Set up configuration variables
var releaseVersion = packageJson.version
var username = process.env.USERNAME
Expand Down

0 comments on commit 5f3a671

Please sign in to comment.