From 1971208aa0d6797acf394f2a8e802dd7ff5083df Mon Sep 17 00:00:00 2001 From: rdbsolutions Date: Fri, 12 Nov 2021 11:40:54 +0000 Subject: [PATCH] added ga switch on cookie options --- .env.local | 3 +- components/case/summary.js | 4 +-- components/cookieBanner.js | 15 +++++++-- components/cookiemanager/index.js | 14 +++++++++ components/homepage/login.js | 6 ++++ package.json | 1 + pages/_document.js | 52 ++++++++++++++++++++----------- 7 files changed, 71 insertions(+), 24 deletions(-) diff --git a/.env.local b/.env.local index 385f7a0d..167508e9 100644 --- a/.env.local +++ b/.env.local @@ -31,5 +31,6 @@ I18N_DOMAIN = "cymru.local" GOOGLE_TAG_MANAGER = GTM-T78CBC3 -SHOWLOGIN = "true" +SHOWLOGIN = "false" SHOWREPRESENTATIONS = "false" +BASIC_AUTH_CREDENTIALS = pinswg:password|pinswg2:password2 \ No newline at end of file diff --git a/components/case/summary.js b/components/case/summary.js index 3ec51ccb..970f47e2 100644 --- a/components/case/summary.js +++ b/components/case/summary.js @@ -215,7 +215,7 @@ const CaseSummary = (props) => { )} - { //spinnerState(); router.back(); @@ -223,7 +223,7 @@ const CaseSummary = (props) => { className="govuk-button govuk-button--secondary" > {t("case:summary-back-button-label")} - + */} diff --git a/components/cookieBanner.js b/components/cookieBanner.js index 5a921075..fdd30e14 100644 --- a/components/cookieBanner.js +++ b/components/cookieBanner.js @@ -17,11 +17,22 @@ const CookieBanner = () => { let expDate = new Date(now); expDate.setDate(now.getDate() + 365); - - console.log(expDate); cookie.set("planning_casework", true, { expires: expDate, }); + + let cookieStr = { + "essential": "accepted", + "usage": "accepted", + "communications": "accepted", + "settings": "accepted", + }; + + cookie.set("CookiePolicy", JSON.stringify(cookieStr).toString(), { + expires: expDate, + path: "/", + }); + setBannerVisible(false); }; diff --git a/components/cookiemanager/index.js b/components/cookiemanager/index.js index 0106f181..52766403 100644 --- a/components/cookiemanager/index.js +++ b/components/cookiemanager/index.js @@ -30,9 +30,23 @@ const CookieManagementMain = (props) => { cookie.set("CookiePolicy", JSON.stringify(values).toString(), { expires: expDate, + path: "/", }); + + function delete_cookie(name, path, domain) { + document.cookie = + name + + "=" + + (path ? ";path=" + path : "") + + (domain ? ";domain=" + domain : "") + + ";expires=Thu, 01 Jan 1970 00:00:01 GMT"; + } + + usageState == "revoked" && delete_cookie("CookiePolicy", "/"); + cookie.set("planning_casework", true, { expires: expDate, + path: "/", }); }; diff --git a/components/homepage/login.js b/components/homepage/login.js index 7e707b96..656c5e1a 100644 --- a/components/homepage/login.js +++ b/components/homepage/login.js @@ -126,12 +126,18 @@ let Login = (props) => { const onHandleSubmit = (values) => { //spinnerState(); + let now = new Date(); + let expDate = new Date(now); + + expDate.setDate(now.getDate() + 0.41); + getLogin(values.loginUserID, values.loginUserPassword).then((data) => { data.value.length != 0 ? (setValidLoginID(true), setLoggedInUserId(data.value[0].contactid), setCookie(null, "pinsUser", data.value[0].contactid, { path: "/", + expires: expDate, }), // spinnerState(), router.replace({ diff --git a/package.json b/package.json index 6b1daead..c31d95d9 100644 --- a/package.json +++ b/package.json @@ -29,6 +29,7 @@ "next": "^11.0.1", "next-redux-wrapper": "^7.0.2", "next-translate": "^1.0.7", + "nextjs-basic-auth-middleware": "^0.2.1", "nookies": "^2.5.2", "path": "^0.12.7", "pm2": "^5.1.0", diff --git a/pages/_document.js b/pages/_document.js index 1415e727..26ce09df 100644 --- a/pages/_document.js +++ b/pages/_document.js @@ -1,35 +1,49 @@ import Document, { Html, Head, Main, NextScript } from "next/document"; import SkipLink from "../components/skiplink"; import { Tracking, TrackingNoScript } from "../components/tracking"; -//import basicAuthMiddleware from "nextjs-basic-auth-middleware"; +import basicAuthMiddleware from "nextjs-basic-auth-middleware"; class MyDocument extends Document { - // static async getInitialProps(ctx) { - // const initialProps = await Document.getInitialProps(ctx); - // if (ctx.req && ctx.res) { - // await basicAuthMiddleware(ctx.req, ctx.res, { - // users: [ - // { - // name: "rehab", - // password: "preventTesting", - // }, - // ], - // }); - // } - // return { ...initialProps }; - // } + static async getInitialProps(ctx) { + const initialProps = await Document.getInitialProps(ctx); + + let useGATracking = false; + + if (ctx.req && ctx.req.headers.cookie) { + typeof ctx.req.cookies.CookiePolicy != "undefined" + ? JSON.parse(ctx.req.cookies.CookiePolicy).usage == "accepted" + ? (useGATracking = true) + : (useGATracking = false) + : (useGATracking = true); + } else { + useGATracking = false; + } + + // if (ctx.req && ctx.res) { + // await basicAuthMiddleware(ctx.req, ctx.res, { + // users: process.env.BASIC_AUTH_CREDENTIALS, + // }); + // } + + return { ...initialProps, useGATracking }; + } render() { const googleTagManagerID = process.env.GOOGLE_TAG_MANAGER || null; - + const { useGATracking } = this.props; return ( - + {useGATracking && ( + + )} - - + {useGATracking && ( + + )} {!process.browser && (