From b7e65c16ea44b23180b5137e7cf5e80afd38f32b Mon Sep 17 00:00:00 2001 From: Nathan Chapman Date: Tue, 22 Mar 2022 16:05:26 -0600 Subject: [PATCH] Add cookie script --- src/ptcoffee/config.py | 6 +++--- src/static/scripts/cookie.js | 24 +++++++++++++++++++++ src/static/scripts/initializers/timezone.js | 2 +- src/static/scripts/payment.js | 2 +- src/static/styles/main.css | 2 +- 5 files changed, 30 insertions(+), 6 deletions(-) create mode 100644 src/static/scripts/cookie.js diff --git a/src/ptcoffee/config.py b/src/ptcoffee/config.py index 0dc9ad2..39df7e8 100644 --- a/src/ptcoffee/config.py +++ b/src/ptcoffee/config.py @@ -30,6 +30,6 @@ SERVER_EMAIL = os.environ.get('SERVER_EMAIL', '') DEFAULT_FROM_EMAIL = os.environ.get('DEFAULT_FROM_EMAIL', '') SECURE_HSTS_SECONDS = os.environ.get('SECURE_HSTS_SECONDS', 3600) -SECURE_SSL_REDIRECT = os.environ.get('SECURE_SSL_REDIRECT', 'True') == 'True' -SESSION_COOKIE_SECURE = os.environ.get('SESSION_COOKIE_SECURE', 'True') == 'True' -CSRF_COOKIE_SECURE = os.environ.get('CSRF_COOKIE_SECURE', 'True') == 'True' +SECURE_SSL_REDIRECT = os.environ.get('SECURE_SSL_REDIRECT', 'False') == 'True' +SESSION_COOKIE_SECURE = os.environ.get('SESSION_COOKIE_SECURE', 'False') == 'True' +CSRF_COOKIE_SECURE = os.environ.get('CSRF_COOKIE_SECURE', 'False') == 'True' diff --git a/src/static/scripts/cookie.js b/src/static/scripts/cookie.js new file mode 100644 index 0000000..fcc94bc --- /dev/null +++ b/src/static/scripts/cookie.js @@ -0,0 +1,24 @@ +export function getCookie(name) { + let cookieValue = null + if (document.cookie && document.cookie !== '') { + const cookies = document.cookie.split(';') + for (let i = 0; i < cookies.length; i++) { + const cookie = cookies[i].trim() + // Does this cookie string begin with the name we want? + if (cookie.substring(0, name.length + 1) === (name + '=')) { + cookieValue = decodeURIComponent(cookie.substring(name.length + 1)) + break + } + } + } + return cookieValue +} + +const twentyYears = 20 * 365 * 24 * 60 * 60 * 1000 + +export function setCookie(name, value) { + const body = [ name, value ].map(encodeURIComponent).join("=") + const expires = new Date(Date.now() + twentyYears).toUTCString() + const cookie = `${body}; domain=; path=/; SameSite=Lax; expires=${expires}` + document.cookie = cookie +} diff --git a/src/static/scripts/initializers/timezone.js b/src/static/scripts/initializers/timezone.js index a0a793a..2b63d14 100644 --- a/src/static/scripts/initializers/timezone.js +++ b/src/static/scripts/initializers/timezone.js @@ -1,4 +1,4 @@ -import { setCookie } from '../lib/cookie.js' +import { setCookie } from '../cookie.js' const { timeZone } = new Intl.DateTimeFormat().resolvedOptions() setCookie('timezone', timeZone) diff --git a/src/static/scripts/payment.js b/src/static/scripts/payment.js index 7929099..1ba1e47 100644 --- a/src/static/scripts/payment.js +++ b/src/static/scripts/payment.js @@ -1,4 +1,4 @@ -import { getCookie } from "./lib/cookie.js" +import { getCookie } from "./cookie.js" let form = document.querySelector('form.order__form') diff --git a/src/static/styles/main.css b/src/static/styles/main.css index 59b44f4..9142f15 100644 --- a/src/static/styles/main.css +++ b/src/static/styles/main.css @@ -1,6 +1,6 @@ :root { --fg-color: #333; - --bg-color: #f6ebd3; + --bg-color: #f5f5f5; --gray-color: #9d9d9d; --yellow-color: #f8a911; --yellow-alt-color: #ffce6f;