Files
moreminimore-website/src/components/consent/CookieBanner.astro
Kunthawat Greethong b485320afc feat: Add full PDPA compliance with cookie consent, admin dashboard, and conditional analytics
Features implemented:
 Cookie consent banner (Accept/Reject) with localStorage storage
 Conditional Umami Analytics (loads only with consent)
 Admin dashboard at /admin/consent-logs (password protected)
 API endpoints for consent logging (POST/GET/DELETE)
 Astro DB integration with consent logging schema
 Production-ready Dockerfile with Node.js server adapter
 Node.js 20+ requirement for Astro 5.x compatibility

Files added:
- src/components/consent/CookieBanner.astro
- src/pages/api/consent/index.ts (POST/GET endpoints)
- src/pages/api/consent/[sessionId]/index.ts (DELETE endpoint)
- src/pages/admin/consent-logs.astro (admin dashboard)
- db/schema.ts (ConsentLog table schema)

Files modified:
- src/layouts/Layout.astro (CookieBanner + conditional Umami)
- astro.config.mjs (Node adapter + DB integration)
- package.json (start script, engines field, dependencies)
- Dockerfile (custom deployment with Node.js server)

Configuration:
- Umami Analytics: Conditional loading based on consent
- Admin password: 'changeme' (MUST change in production)
- Database: SQLite file (data/consent.db)
- Server: Node.js standalone adapter

Deployment:
- Docker build with SQLite runtime support
- Custom Dockerfile for Easypanel
- Start command: node dist/server/entry.mjs

Security notes:
⚠️  CHANGE ADMIN_PASSWORD before production deployment
⚠️  Enable HTTPS for secure cookie consent
⚠️  Consider server-side authentication for admin dashboard
2026-03-10 21:25:49 +07:00

207 lines
5.0 KiB
Plaintext

---
// Cookie consent banner for PDPA compliance
---
<div id="cookie-banner" class="cookie-banner">
<div class="cookie-content">
<p class="cookie-message">
เราใช้คุกกี้เพื่อปรับปรุงประสบการณ์การใช้งานเว็บไซต์ หากคุณยอมรับ เราจะใช้คุกกี้เพื่อวัตถุประสงค์ในการวิเคราะห์และการตลาด
<a href="/privacy-policy" class="cookie-link">อ่านนโยบายความเป็นส่วนตัว</a>
</p>
<div class="cookie-buttons">
<button id="cookie-reject" class="btn-cookie-reject">ปฏิเสธทั้งหมด</button>
<button id="cookie-accept" class="btn-cookie-accept">ยอมรับทั้งหมด</button>
</div>
</div>
</div>
<style>
.cookie-banner {
position: fixed;
bottom: -100%;
left: 0;
right: 0;
background: white;
box-shadow: 0 -4px 20px rgba(0, 0, 0, 0.15);
padding: 1.5rem;
z-index: 9999;
transition: bottom 0.3s ease-in-out;
border-top: 4px solid #fed400;
}
.cookie-banner.show {
bottom: 0;
}
.cookie-content {
max-width: 1200px;
margin: 0 auto;
display: flex;
flex-direction: column;
gap: 1rem;
}
@media (min-width: 768px) {
.cookie-content {
flex-direction: row;
align-items: center;
justify-content: space-between;
}
}
.cookie-message {
font-family: 'Noto Sans Thai', sans-serif;
font-size: 1rem;
color: #333;
line-height: 1.6;
margin: 0;
flex: 1;
}
.cookie-link {
color: #000;
text-decoration: underline;
font-weight: 600;
}
.cookie-link:hover {
color: #fed400;
}
.cookie-buttons {
display: flex;
gap: 0.75rem;
flex-shrink: 0;
}
.btn-cookie-accept,
.btn-cookie-reject {
font-family: 'Noto Sans Thai', sans-serif;
font-size: 1rem;
font-weight: 600;
padding: 0.75rem 1.5rem;
border: none;
border-radius: 9999px;
cursor: pointer;
transition: all 0.2s ease;
white-space: nowrap;
}
.btn-cookie-accept {
background-color: #fed400;
color: #000;
}
.btn-cookie-accept:hover {
background-color: #e5c000;
transform: translateY(-2px);
box-shadow: 0 4px 12px rgba(254, 212, 0, 0.3);
}
.btn-cookie-reject {
background-color: #000;
color: #fff;
}
.btn-cookie-reject:hover {
background-color: #333;
transform: translateY(-2px);
box-shadow: 0 4px 12px rgba(0, 0, 0, 0.3);
}
@media (max-width: 767px) {
.cookie-banner {
padding: 1rem;
}
.cookie-buttons {
flex-direction: column;
width: 100%;
}
.btn-cookie-accept,
.btn-cookie-reject {
width: 100%;
text-align: center;
}
}
</style>
<script client:load>
const POLICY_VERSION = '1.0.0';
function checkConsent() {
const consent = localStorage.getItem('consent-preferences');
return consent ? JSON.parse(consent) : null;
}
function saveConsent(consent) {
localStorage.setItem('consent-preferences', JSON.stringify(consent));
window.dispatchEvent(new CustomEvent('consentGiven', { detail: consent }));
const sessionId = localStorage.getItem('consent-session-id') || crypto.randomUUID();
if (!localStorage.getItem('consent-session-id')) {
localStorage.setItem('consent-session-id', sessionId);
}
fetch('/api/consent', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
sessionId,
...consent,
policyVersion: POLICY_VERSION,
userAgent: navigator.userAgent
})
}).catch(err => console.error('Failed to log consent:', err));
}
function showBanner() {
const banner = document.getElementById('cookie-banner');
if (banner) {
banner.classList.add('show');
}
}
function hideBanner() {
const banner = document.getElementById('cookie-banner');
if (banner) {
banner.classList.remove('show');
}
}
const existingConsent = checkConsent();
if (!existingConsent) {
setTimeout(() => showBanner(), 500);
}
document.addEventListener('DOMContentLoaded', () => {
const acceptBtn = document.getElementById('cookie-accept');
const rejectBtn = document.getElementById('cookie-reject');
const banner = document.getElementById('cookie-banner');
acceptBtn?.addEventListener('click', () => {
const consent = {
essential: true,
analytics: true,
marketing: true,
timestamp: new Date().toISOString()
};
saveConsent(consent);
hideBanner();
});
rejectBtn?.addEventListener('click', () => {
const consent = {
essential: true,
analytics: false,
marketing: false,
timestamp: new Date().toISOString()
};
saveConsent(consent);
hideBanner();
});
});
</script>