Skip to content

Commit

Permalink
Merge pull request #22 from shadowgate15/issue-21
Browse files Browse the repository at this point in the history
feat: dynamic cookie expiry, closes issue #21
  • Loading branch information
niftylettuce authored Jul 25, 2020
2 parents 8afe8f3 + ae233ca commit 1f3d42e
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 13 deletions.
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
"@hapi/boom": "^9.1.0",
"boolean": "3.0.1",
"country-language": "^0.1.7",
"dayjs": "^1.8.30",
"debug": "^4.1.1",
"i18n": "^0.9.1",
"i18n-locales": "^0.0.4",
Expand Down Expand Up @@ -60,7 +59,7 @@
"xo": "0.25"
},
"engines": {
"node": ">=6.4.0"
"node": ">=7.0.0"
},
"files": [
"lib",
Expand Down
18 changes: 12 additions & 6 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ const Boom = require('@hapi/boom');
const debug = require('debug')('ladjs:i18n');
const i18n = require('i18n');
const locales = require('i18n-locales');
const dayjs = require('dayjs');
const multimatch = require('multimatch');
const titleize = require('titleize');
const tlds = require('tlds');
Expand All @@ -27,11 +26,9 @@ class I18N {
cookie: 'locale',
cookieOptions: {
// Disable signed cookies in NODE_ENV=test
signed: process.env.NODE_ENV !== 'test',
expires: dayjs()
.add(1, 'year')
.toDate()
signed: process.env.NODE_ENV !== 'test'
},
expiryMs: 31556952000, // one year in ms
indent: ' ',
defaultLocale: 'en',
syncFiles: boolean(process.env.I18N_SYNC_FILES || true),
Expand Down Expand Up @@ -71,6 +68,12 @@ class I18N {
`Default locale of ${this.config.defaultLocale} must be included in list of locales`
);

// make sure expires is not set in cookieOptions
if (this.config.cookieOptions.expires)
throw new Error(
'Please specify expiryMs config option instead of passing a Date to cookieOptions config'
);

// inherit i18n object
Object.assign(this, i18n);

Expand Down Expand Up @@ -274,7 +277,10 @@ class I18N {
debug('found valid language "%s"', locale);

// set the cookie for future requests
ctx.cookies.set(this.config.cookie, locale, this.config.cookieOptions);
ctx.cookies.set(this.config.cookie, locale, {
...this.config.cookieOptions,
expires: new Date(Date.now() + this.config.expiryMs)
});
debug('set cookies for locale "%s"', locale);

// if the user is logged in and ctx.isAuthenticated() exists,
Expand Down
7 changes: 7 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -628,3 +628,10 @@ test('redirects sets users last_locale', async t => {
t.is(res.status, 200);
t.is(res.body.last_locale, 'en');
});

test('errors if cookieOptions.expires is set', t => {
t.throws(() => new I18N({ cookieOptions: { expires: new Date() } }), {
message:
'Please specify expiryMs config option instead of passing a Date to cookieOptions config'
});
});
5 changes: 0 additions & 5 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2295,11 +2295,6 @@ date-time@^2.1.0:
dependencies:
time-zone "^1.0.0"

dayjs@^1.8.30:
version "1.8.30"
resolved "https://registry.yarnpkg.com/dayjs/-/dayjs-1.8.30.tgz#d3b314d3ccdc179015d915fd3c6e14422c026378"
integrity sha512-5s5IGuP5bVvIbOWkEDcfmXsUj24fZW1NMHVVSdSFF/kW8d+alZcI9SpBKC+baEyBe+z3fUp17y75ulstv5swUw==

debug@*, debug@4, debug@^4.0.0, debug@^4.0.1, debug@^4.1.0, debug@^4.1.1:
version "4.1.1"
resolved "https://registry.yarnpkg.com/debug/-/debug-4.1.1.tgz#3b72260255109c6b589cee050f1d516139664791"
Expand Down

0 comments on commit 1f3d42e

Please sign in to comment.