From 58140fc0af5af9e3f8c86015d888f1a93e1d4a0b Mon Sep 17 00:00:00 2001 From: Jim O'Halloran Date: Thu, 31 Oct 2019 09:49:31 +1030 Subject: [PATCH] Validate cookie contains known locale when initialising the store --- resources/js/store/modules/lang.js | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/resources/js/store/modules/lang.js b/resources/js/store/modules/lang.js index 7ac18231c..4a913a3a2 100644 --- a/resources/js/store/modules/lang.js +++ b/resources/js/store/modules/lang.js @@ -3,9 +3,19 @@ import * as types from '../mutation-types' const { locale, locales } = window.config +function validateCookieLocale () { + const cookieLocale = Cookies.get('locale') || locale + if (locales.hasOwnProperty(cookieLocale)) { + return cookieLocale + } else { + Cookies.set('locale', locale, { expires: 365 }) + return locale + } +} + // state export const state = { - locale: Cookies.get('locale') || locale, + locale: validateCookieLocale(), locales: locales }