Skip to content

Commit

Permalink
Merge pull request #2021 from alphagov/1733_getting_errors_about_sess…
Browse files Browse the repository at this point in the history
…ion_file_store

Fixing errors about session file store (issue 1733)
  • Loading branch information
HannahJMWood authored Mar 8, 2023
2 parents b9037da + e85656e commit 1154112
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 6 deletions.
14 changes: 8 additions & 6 deletions lib/session.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,26 +117,28 @@ function autoStoreData (req, res, next) {
next()
}

function getSessionNameFromServiceName (serviceName) {
return 'govuk-prototype-kit-' + (Buffer.from(serviceName, 'utf8')).toString('hex')
function getSessionNameFromWorkingDirectory (workingDirectory) {
return 'govuk-prototype-kit-' + (Buffer.from(workingDirectory, 'utf8')).toString('hex')
}

function getSessionMiddleware () {
const config = getConfig()
const workingDirectory = process.cwd()

// Session uses service name to avoid clashes with other prototypes
const sessionName = getSessionNameFromServiceName(config.serviceName)
// Session uses working directory path to avoid clashes with other prototypes
const sessionName = getSessionNameFromWorkingDirectory(workingDirectory)
const sessionHours = 4
const sessionOptions = {
secret: sessionName,
cookie: {
maxAge: 1000 * 60 * 60 * sessionHours,
secure: getConfig().isSecure
secure: config.isSecure
}
}

const fileStoreOptions = {
path: sessionStoreDir
path: sessionStoreDir,
retries: 1
}

if (config.isDevelopment) {
Expand Down
6 changes: 6 additions & 0 deletions lib/utils/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,12 @@ function sessionFileStoreQuietLogFn (message) {
// but this isn't useful for our users, so let's just swallow those messages
return
}

// Handling case where a user has multiple prototypes in the same working directory by giving a more useful error message
if (message.includes('ENOENT')) {
console.error('Warning: Please use different working directories for your prototypes to avoid session clashes')
return
}
console.log(message)
}

Expand Down

0 comments on commit 1154112

Please sign in to comment.