Add cookie script

This commit is contained in:
Nathan Chapman 2022-03-22 16:05:26 -06:00
parent da5f474fee
commit b7e65c16ea
5 changed files with 30 additions and 6 deletions

View File

@ -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'

View File

@ -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
}

View File

@ -1,4 +1,4 @@
import { setCookie } from '../lib/cookie.js'
import { setCookie } from '../cookie.js'
const { timeZone } = new Intl.DateTimeFormat().resolvedOptions()
setCookie('timezone', timeZone)

View File

@ -1,4 +1,4 @@
import { getCookie } from "./lib/cookie.js"
import { getCookie } from "./cookie.js"
let form = document.querySelector('form.order__form')

View File

@ -1,6 +1,6 @@
:root {
--fg-color: #333;
--bg-color: #f6ebd3;
--bg-color: #f5f5f5;
--gray-color: #9d9d9d;
--yellow-color: #f8a911;
--yellow-alt-color: #ffce6f;