From d491c1bad64a538d670cc7631fe659df64ae867d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Wolfgang=20Hu=C3=9F?= Date: Wed, 19 Jan 2022 09:23:08 +0100 Subject: [PATCH 1/7] Add expiration date and sameSite attribute to lacale cookie --- webapp/plugins/i18n.js | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/webapp/plugins/i18n.js b/webapp/plugins/i18n.js index 8c64ef0c3..44005db62 100644 --- a/webapp/plugins/i18n.js +++ b/webapp/plugins/i18n.js @@ -29,7 +29,13 @@ export default ({ app, req, cookie, store }) => { return } - app.$cookies.set(key, newLocale) + const expires = new Date() + expires.setDate(expires.getDate() + app.$env.COOKIE_EXPIRE_TIME) + app.$cookies.set(key, newLocale, { + expires, + // maxAge: app.$env.COOKIE_EXPIRE_TIME * 60 * 60 * 24, // days to seconds + sameSite: 'lax', + }) if (!app.$i18n.localeExists(newLocale)) { import(`~/locales/${newLocale}.json`).then((res) => { app.$i18n.add(newLocale, res.default) From 33f04598837f35bf3e386d754ffd592823d57ed4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Wolfgang=20Hu=C3=9F?= Date: Thu, 20 Jan 2022 13:31:43 +0100 Subject: [PATCH 2/7] Get browser language --- webapp/plugins/i18n.js | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/webapp/plugins/i18n.js b/webapp/plugins/i18n.js index 44005db62..b396a789e 100644 --- a/webapp/plugins/i18n.js +++ b/webapp/plugins/i18n.js @@ -22,7 +22,10 @@ export default ({ app, req, cookie, store }) => { if (process.server) return const newLocale = mutation.payload.locale - const currentLocale = await app.$cookies.get(key) + let currentLocale = await app.$cookies.get(key) + if (!currentLocale) { + currentLocale = navigator.language.split('-')[0] // get browser language + } const isDifferent = newLocale !== currentLocale if (!isDifferent) { From 81ffed475841ab1c912bec226799c826a21978e9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Wolfgang=20Hu=C3=9F?= Date: Thu, 20 Jan 2022 13:43:49 +0100 Subject: [PATCH 3/7] Add 'sameSite' option to login cookie --- webapp/nuxt.config.js | 1 + 1 file changed, 1 insertion(+) diff --git a/webapp/nuxt.config.js b/webapp/nuxt.config.js index b4fc3aa7c..90f142145 100644 --- a/webapp/nuxt.config.js +++ b/webapp/nuxt.config.js @@ -201,6 +201,7 @@ export default { /** * A Boolean indicating if the cookie transmission requires a * secure protocol (https). Defaults to false. */ secure: CONFIG.COOKIE_HTTPS_ONLY, + sameSite: 'lax', }, // includeNodeModules: true, // optional, default: false (this includes graphql-tag for node_modules folder) From d0985ca804a3e53e24cd497fc2539bf0f0e9102b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Wolfgang=20Hu=C3=9F?= Date: Thu, 20 Jan 2022 18:16:15 +0100 Subject: [PATCH 4/7] Add comment for 'sameSite' cookie option --- webapp/nuxt.config.js | 2 +- webapp/plugins/i18n.js | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/webapp/nuxt.config.js b/webapp/nuxt.config.js index 90f142145..717aa0339 100644 --- a/webapp/nuxt.config.js +++ b/webapp/nuxt.config.js @@ -201,7 +201,7 @@ export default { /** * A Boolean indicating if the cookie transmission requires a * secure protocol (https). Defaults to false. */ secure: CONFIG.COOKIE_HTTPS_ONLY, - sameSite: 'lax', + sameSite: 'lax', // for the meaning see https://www.thinktecture.com/de/identity/samesite/samesite-in-a-nutshell/ }, // includeNodeModules: true, // optional, default: false (this includes graphql-tag for node_modules folder) diff --git a/webapp/plugins/i18n.js b/webapp/plugins/i18n.js index b396a789e..76713de97 100644 --- a/webapp/plugins/i18n.js +++ b/webapp/plugins/i18n.js @@ -37,7 +37,7 @@ export default ({ app, req, cookie, store }) => { app.$cookies.set(key, newLocale, { expires, // maxAge: app.$env.COOKIE_EXPIRE_TIME * 60 * 60 * 24, // days to seconds - sameSite: 'lax', + sameSite: 'lax', // for the meaning see https://www.thinktecture.com/de/identity/samesite/samesite-in-a-nutshell/ }) if (!app.$i18n.localeExists(newLocale)) { import(`~/locales/${newLocale}.json`).then((res) => { From 8a20b994cfe94faa1482656f33672c110b279ad0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Wolfgang=20Hu=C3=9F?= Date: Fri, 21 Jan 2022 21:09:01 +0100 Subject: [PATCH 5/7] Fix Cypress test --- webapp/plugins/i18n.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/webapp/plugins/i18n.js b/webapp/plugins/i18n.js index 76713de97..dbb31eb9e 100644 --- a/webapp/plugins/i18n.js +++ b/webapp/plugins/i18n.js @@ -22,13 +22,15 @@ export default ({ app, req, cookie, store }) => { if (process.server) return const newLocale = mutation.payload.locale + let isCookie = true let currentLocale = await app.$cookies.get(key) if (!currentLocale) { + isCookie = false currentLocale = navigator.language.split('-')[0] // get browser language } const isDifferent = newLocale !== currentLocale - if (!isDifferent) { + if (isCookie && !isDifferent) { // cookie has to be set, otherwise Cypress test does not work return } From ac16345defdae627cf3208b8db40622cc2147998 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Wolfgang=20Hu=C3=9F?= Date: Fri, 21 Jan 2022 21:11:06 +0100 Subject: [PATCH 6/7] Adjust comment --- webapp/plugins/i18n.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/webapp/plugins/i18n.js b/webapp/plugins/i18n.js index dbb31eb9e..643d9371c 100644 --- a/webapp/plugins/i18n.js +++ b/webapp/plugins/i18n.js @@ -30,7 +30,8 @@ export default ({ app, req, cookie, store }) => { } const isDifferent = newLocale !== currentLocale - if (isCookie && !isDifferent) { // cookie has to be set, otherwise Cypress test does not work + // cookie has to be set, otherwise Cypress test does not work + if (isCookie && !isDifferent) { return } From 652665d1fb8e77e11ae174d6699e702ba60fc774 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Wolfgang=20Hu=C3=9F?= Date: Fri, 6 May 2022 10:50:38 +0200 Subject: [PATCH 7/7] Change naming and negation for better readability --- webapp/plugins/i18n.js | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/webapp/plugins/i18n.js b/webapp/plugins/i18n.js index 643d9371c..f162408f6 100644 --- a/webapp/plugins/i18n.js +++ b/webapp/plugins/i18n.js @@ -21,30 +21,30 @@ export default ({ app, req, cookie, store }) => { const changeHandler = async (mutation) => { if (process.server) return - const newLocale = mutation.payload.locale - let isCookie = true - let currentLocale = await app.$cookies.get(key) - if (!currentLocale) { - isCookie = false - currentLocale = navigator.language.split('-')[0] // get browser language + const localeInStore = mutation.payload.locale + let cookieExists = true + let localeInCookies = await app.$cookies.get(key) + if (!localeInCookies) { + cookieExists = false + localeInCookies = navigator.language.split('-')[0] // get browser language } - const isDifferent = newLocale !== currentLocale + const isLocaleStoreSameAsCookies = localeInStore === localeInCookies // cookie has to be set, otherwise Cypress test does not work - if (isCookie && !isDifferent) { + if (cookieExists && isLocaleStoreSameAsCookies) { return } const expires = new Date() expires.setDate(expires.getDate() + app.$env.COOKIE_EXPIRE_TIME) - app.$cookies.set(key, newLocale, { + app.$cookies.set(key, localeInStore, { expires, // maxAge: app.$env.COOKIE_EXPIRE_TIME * 60 * 60 * 24, // days to seconds sameSite: 'lax', // for the meaning see https://www.thinktecture.com/de/identity/samesite/samesite-in-a-nutshell/ }) - if (!app.$i18n.localeExists(newLocale)) { - import(`~/locales/${newLocale}.json`).then((res) => { - app.$i18n.add(newLocale, res.default) + if (!app.$i18n.localeExists(localeInStore)) { + import(`~/locales/${localeInStore}.json`).then((res) => { + app.$i18n.add(localeInStore, res.default) }) } @@ -54,7 +54,7 @@ export default ({ app, req, cookie, store }) => { if (user && user._id && token) { // TODO: SAVE LOCALE // store.dispatch('usersettings/patch', { - // uiLanguage: newLocale + // uiLanguage: localeInStore // }, { root: true }) } }