From d9bfa33535ea3a4a0c08822e242ca6a4f77aae60 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ivan=20Kir=C3=A1=C4=BE?= <54802833+IvanKiral@users.noreply.github.com> Date: Tue, 7 Mar 2023 15:14:07 +0100 Subject: [PATCH 1/6] fix spotlight to use samesite and secure --- src/Client.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Client.ts b/src/Client.ts index e2df775..3f286b8 100644 --- a/src/Client.ts +++ b/src/Client.ts @@ -17,7 +17,7 @@ const cookies = new Cookies(document.cookie); let currentProjectId = projectId || cookies.get(selectedProjectCookieName); if (currentProjectId) { - cookies.set(selectedProjectCookieName, currentProjectId, { path: '/' }); + cookies.set(selectedProjectCookieName, currentProjectId, { path: '/', sameSite:'none', secure:true }); } else { currentProjectId = defaultProjectId; } @@ -59,8 +59,8 @@ const resetClient = (newProjectId: string): void => { ], propertyNameResolver: camelCasePropertyNameResolver, }); - const cookies = new Cookies(document.cookie); - cookies.set(selectedProjectCookieName, newProjectId, { path: '/' }); + + cookies.set(selectedProjectCookieName, newProjectId, { path: '/', sameSite:'none', secure:true }); }; export { Client, resetClient }; From 710f872ca14bbde85c484b219b7b21af9cb97973 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ivan=20Kir=C3=A1=C4=BE?= <54802833+IvanKiral@users.noreply.github.com> Date: Thu, 9 Mar 2023 09:03:44 +0100 Subject: [PATCH 2/6] Update projectId flow to not save it to cookie when get from environment --- src/App.tsx | 6 ++---- src/Client.ts | 38 ++++++++++++++++++++++++++------------ 2 files changed, 28 insertions(+), 16 deletions(-) diff --git a/src/App.tsx b/src/App.tsx index 7efed90..80ed196 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -17,20 +17,18 @@ import Cafes from './Pages/Cafes'; import Contact from './Pages/Contacts'; import Coffee from './Pages/Coffee'; import Brewer from './Pages/Brewer'; -import Cookies from 'universal-cookie'; import { SetLanguageType } from './LocalizedApp'; import { NotFound } from './Pages/NotFound'; +import { getProjectId } from './Client'; interface AppProps { changeLanguage: SetLanguageType; } const App: React.FC = ({ changeLanguage }) => { - const cookies = new Cookies(document.cookie); - const cookie = cookies.get(selectedProjectCookieName); const { formatMessage } = useIntl(); - if (!cookie) { + if (!getProjectId()) { return ; } diff --git a/src/Client.ts b/src/Client.ts index 3f286b8..f8de4ca 100644 --- a/src/Client.ts +++ b/src/Client.ts @@ -5,22 +5,32 @@ import { } from '@kontent-ai/delivery-sdk'; import packageInfo from '../package.json'; import { selectedProjectCookieName } from './const'; -import { defaultProjectId } from './Utilities/SelectedProject'; +import validator from 'validator'; const sourceTrackingHeaderName = 'X-KC-SOURCE'; -// environment variables -const projectId = process.env.REACT_APP_PROJECT_ID || ''; const previewApiKey = process.env.REACT_APP_PREVIEW_API_KEY || ''; const cookies = new Cookies(document.cookie); -let currentProjectId = projectId || cookies.get(selectedProjectCookieName); -if (currentProjectId) { - cookies.set(selectedProjectCookieName, currentProjectId, { path: '/', sameSite:'none', secure:true }); -} else { - currentProjectId = defaultProjectId; -} +const getProjectId = (): string => { + const projectId = process.env.REACT_APP_PROJECT_ID || ''; + + let currentProjectId = projectId; + + if (projectId) { + if (!validator.isUUID(projectId)) { + console.error( + `Your projectId (${projectId}) given in your environment variables is not a valid GUID.` + ); + } + return projectId; + } + currentProjectId = cookies.get(selectedProjectCookieName); + return currentProjectId ? currentProjectId : ''; +}; + +const currentProjectId = getProjectId(); const isPreview = (): boolean => previewApiKey !== ''; @@ -59,8 +69,12 @@ const resetClient = (newProjectId: string): void => { ], propertyNameResolver: camelCasePropertyNameResolver, }); - - cookies.set(selectedProjectCookieName, newProjectId, { path: '/', sameSite:'none', secure:true }); + + cookies.set(selectedProjectCookieName, newProjectId, { + path: '/', + sameSite: 'none', + secure: true, + }); }; -export { Client, resetClient }; +export { Client, resetClient, getProjectId }; From 49008c4dae380182b28bd96fe5c10b27de60b528 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ivan=20Kir=C3=A1=C4=BE?= <54802833+IvanKiral@users.noreply.github.com> Date: Thu, 9 Mar 2023 09:16:00 +0100 Subject: [PATCH 3/6] Fix build --- src/App.tsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/App.tsx b/src/App.tsx index 80ed196..722d8c5 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -1,7 +1,7 @@ import React from 'react'; import './App.css'; import { Navigate, Route, Routes } from 'react-router-dom'; -import { projectConfigurationPath, selectedProjectCookieName } from './const'; +import { projectConfigurationPath } from './const'; import SpinnerLayout from './Components/SpinnerLayout'; import Metadata from './Components/Metadata'; import qs from 'qs'; From fefdf3a2b56e534be89c28ca4954e7d67a5fe804 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ivan=20Kir=C3=A1=C4=BE?= <54802833+IvanKiral@users.noreply.github.com> Date: Fri, 10 Mar 2023 09:03:42 +0100 Subject: [PATCH 4/6] Refactor getProjectId function --- src/App.tsx | 9 ++++++--- src/Client.ts | 51 +++++++++++++++++++++++++++++++++++---------------- 2 files changed, 41 insertions(+), 19 deletions(-) diff --git a/src/App.tsx b/src/App.tsx index 722d8c5..b6aaf94 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -1,7 +1,6 @@ import React from 'react'; import './App.css'; import { Navigate, Route, Routes } from 'react-router-dom'; -import { projectConfigurationPath } from './const'; import SpinnerLayout from './Components/SpinnerLayout'; import Metadata from './Components/Metadata'; import qs from 'qs'; @@ -19,7 +18,8 @@ import Coffee from './Pages/Coffee'; import Brewer from './Pages/Brewer'; import { SetLanguageType } from './LocalizedApp'; import { NotFound } from './Pages/NotFound'; -import { getProjectId } from './Client'; +import { getProjectIdFromCookies, getProjectIdFromEnvironment } from './Client'; +import { projectConfigurationPath } from './const'; interface AppProps { changeLanguage: SetLanguageType; @@ -28,7 +28,10 @@ interface AppProps { const App: React.FC = ({ changeLanguage }) => { const { formatMessage } = useIntl(); - if (!getProjectId()) { + if ( + getProjectIdFromEnvironment() === undefined && + !getProjectIdFromCookies() + ) { return ; } diff --git a/src/Client.ts b/src/Client.ts index f8de4ca..4743725 100644 --- a/src/Client.ts +++ b/src/Client.ts @@ -13,24 +13,38 @@ const previewApiKey = process.env.REACT_APP_PREVIEW_API_KEY || ''; const cookies = new Cookies(document.cookie); -const getProjectId = (): string => { - const projectId = process.env.REACT_APP_PROJECT_ID || ''; - - let currentProjectId = projectId; - - if (projectId) { - if (!validator.isUUID(projectId)) { - console.error( - `Your projectId (${projectId}) given in your environment variables is not a valid GUID.` - ); - } - return projectId; +const getProjectIdFromEnvironment = (): string | null | undefined => { + const projectIdFromEnv = process.env.REACT_APP_PROJECT_ID; + + if (projectIdFromEnv === undefined) { + return undefined; + } + + if (projectIdFromEnv && !validator.isUUID(projectIdFromEnv)) { + console.error( + `Your projectId (${projectIdFromEnv}) given in your environment variables is not a valid GUID.` + ); + return null; + } + + return projectIdFromEnv; +}; + +const getProjectIdFromCookies = (): string | null => { + const projectIdFromCookie = cookies.get(selectedProjectCookieName); + + if (projectIdFromCookie && !validator.isUUID(projectIdFromCookie)) { + console.error( + `Your projectId (${projectIdFromCookie}) from cookies is not a valid GUID.` + ); + return null; } - currentProjectId = cookies.get(selectedProjectCookieName); - return currentProjectId ? currentProjectId : ''; + + return projectIdFromCookie; }; -const currentProjectId = getProjectId(); +const currentProjectId = + getProjectIdFromEnvironment() ?? getProjectIdFromCookies() ?? ''; const isPreview = (): boolean => previewApiKey !== ''; @@ -77,4 +91,9 @@ const resetClient = (newProjectId: string): void => { }); }; -export { Client, resetClient, getProjectId }; +export { + Client, + resetClient, + getProjectIdFromEnvironment, + getProjectIdFromCookies, +}; From b600e98123f77d2c8d658f58408b6d1cba96827a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ivan=20Kir=C3=A1=C4=BE?= <54802833+IvanKiral@users.noreply.github.com> Date: Mon, 13 Mar 2023 07:58:53 +0100 Subject: [PATCH 5/6] Add message page when projectId is malformed --- src/App.tsx | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/App.tsx b/src/App.tsx index b6aaf94..497aa14 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -28,6 +28,14 @@ interface AppProps { const App: React.FC = ({ changeLanguage }) => { const { formatMessage } = useIntl(); + if (getProjectIdFromEnvironment() === null) { + return ( +
+ Your projectId given in your environment variables is not a valid GUID. +
+ ); + } + if ( getProjectIdFromEnvironment() === undefined && !getProjectIdFromCookies() From b0b854a201d23466b8afdab24460854c96e7a468 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ivan=20Kir=C3=A1=C4=BE?= <54802833+IvanKiral@users.noreply.github.com> Date: Mon, 13 Mar 2023 10:39:57 +0100 Subject: [PATCH 6/6] Remove unnecessary if in getProjectIdFromEnvironment --- src/Client.ts | 4 ---- 1 file changed, 4 deletions(-) diff --git a/src/Client.ts b/src/Client.ts index 4743725..d58e792 100644 --- a/src/Client.ts +++ b/src/Client.ts @@ -16,10 +16,6 @@ const cookies = new Cookies(document.cookie); const getProjectIdFromEnvironment = (): string | null | undefined => { const projectIdFromEnv = process.env.REACT_APP_PROJECT_ID; - if (projectIdFromEnv === undefined) { - return undefined; - } - if (projectIdFromEnv && !validator.isUUID(projectIdFromEnv)) { console.error( `Your projectId (${projectIdFromEnv}) given in your environment variables is not a valid GUID.`