diff --git a/package.json b/package.json index 06d9451..acbf77a 100644 --- a/package.json +++ b/package.json @@ -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", @@ -60,7 +59,7 @@ "xo": "0.25" }, "engines": { - "node": ">=6.4.0" + "node": ">=7.0.0" }, "files": [ "lib", diff --git a/src/index.js b/src/index.js index 889baf9..710bb30 100644 --- a/src/index.js +++ b/src/index.js @@ -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'); @@ -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), @@ -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); @@ -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, diff --git a/test/test.js b/test/test.js index 6c00756..c20f4ab 100644 --- a/test/test.js +++ b/test/test.js @@ -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' + }); +}); diff --git a/yarn.lock b/yarn.lock index 9d34205..a9f6a49 100644 --- a/yarn.lock +++ b/yarn.lock @@ -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"