Skip to content
All essays
CraftFebruary 22, 202511 min

GDPR Compliance: Data Privacy for Developers

Understand GDPR requirements. Implement privacy-by-design in your applications.

Ü
Ümit Uz
Mobile & Full Stack Developer

What is GDPR?

General Data Protection Regulation - EU law protecting personal data.

Key Principles

  1. 1Lawfulness, fairness, transparency
  2. 2Purpose limitation: Collect for specific, explicit purposes
  3. 3Data minimization: Only what's necessary
  4. 4Accuracy: Keep data up-to-date
  5. 5Storage limitation: Don't keep longer than needed
  6. 6Integrity and confidentiality: Security
  7. 7Accountability: Demonstrate compliance

Data Subject Rights

javascript
// Right to Access
// User can request copy of their data
async function exportUserData(userId) {
  const user = await db.users.findById(userId);
  const logs = await db.logs.findByUser(userId);

  return {
    personalData: user,
    activityLogs: logs,
    exportDate: new Date()
  };
}

// Right to Rectification
// User can correct inaccurate data
async function updateUserData(userId, newData) {
  await db.users.update(userId, newData);
  logDataChange(userId, 'UPDATE', newData);
}

// Right to Erasure (Right to be Forgotten)
async function deleteUserData(userId) {
  await db.users.delete(userId);
  await db.logs.deleteByUser(userId);
  await db.backups.removeUser(userId);
  logDataDeletion(userId);
}

// Right to Portability
// User can get data in machine-readable format
async function exportUserJSON(userId) {
  const data = await exportUserData(userId);
  return JSON.stringify(data, null, 2);
}
javascript
// Consent tracking
class ConsentManager {
  constructor() {
    this.consents = new Map();
  }

  requestConsent(userId, purposes) {
    // Show consent dialog
    return {
      analytics: false,
      marketing: false,
      necessary: true  // Always true for essential cookies
    };
  }

  storeConsent(userId, consents) {
    this.consents.set(userId, {
      ...consents,
      timestamp: new Date(),
      ipAddress: getClientIP()
    });
  }

  canProcess(userId, purpose) {
    const consent = this.consents.get(userId);
    return consent && consent[purpose] === true;
  }

  withdrawConsent(userId, purpose) {
    const consent = this.consents.get(userId);
    if (consent) {
      consent[purpose] = false;
      consent.lastModified = new Date();
    }
  }
}

Privacy by Design

Data Minimization

javascript
// Bad: Collect everything
const user = {
  name: 'John',
  email: 'john@example.com',
  phone: '555-1234',
  address: '123 Main St',
  ssn: 'XXX-XX-XXXX',  // Don't collect SSN!
  motherMaidenName: 'Smith'  // Don't collect!
};

// Good: Only what's needed
const user = {
  name: 'John',
  email: 'john@example.com'
};

Pseudonymization

javascript
// Replace identifiable data with artificial identifiers
function pseudonymize(data) {
  return {
    id: hash(data.email),
    region: data.postalCode.slice(0, 3),  // Partial
    age: calculateAge(data.birthDate),    // Category, not exact
    createdAt: data.createdAt
  };
}

Encryption

javascript
// Encrypt sensitive data at rest
import crypto from 'crypto';

const algorithm = 'aes-256-gcm';
const key = Buffer.from(process.env.ENCRYPTION_KEY, 'hex');

function encrypt(text) {
  const iv = crypto.randomBytes(16);
  const cipher = crypto.createCipheriv(algorithm, key, iv);

  let encrypted = cipher.update(text, 'utf8', 'hex');
  encrypted += cipher.final('hex');

  const authTag = cipher.getAuthTag();

  return {
    encrypted,
    iv: iv.toString('hex'),
    authTag: authTag.toString('hex')
  };
}
html
<!-- Cookie consent banner -->
<div id="cookie-banner">
  <p>We use cookies to improve your experience.</p>
  <button onclick="acceptEssential()">Essential Only</button>
  <button onclick="acceptAll()">Accept All</button>
  <button onclick="managePreferences()">Manage Preferences</button>
</div>

<script>
// Store consent
document.cookie = "cookie_consent=essential; path=/; max-age=31536000; SameSite=Lax";

// Respect consent
if (getConsent('analytics')) {
  loadAnalytics();
}
</script>

Data Breach Response

javascript
// Breach detection and notification
async function handleBreach(breach) {
  // 1. Contain breach
  await containBreach(breach.id);

  // 2. Assess impact
  const affectedUsers = await findAffectedUsers(breach);

  // 3. Notify authorities (within 72 hours)
  await notifyDPA(breach, affectedUsers.length);

  // 4. Notify affected individuals
  await notifyUsers(affectedUsers, breach);

  // 5. Document everything
  await documentBreach(breach, response);
}

DPO (Data Protection Officer)

Required if you:

  • Process large-scale data
  • Process special category data
  • Monitor individuals regularly

Fines

  • Lower level: Up to €10 million or 2% of global revenue
  • Higher level: Up to €20 million or 4% of global revenue

Privacy is a fundamental right!

Related essays

Next essay
LeetCode Problem Solving: Strategies, Patterns, and Practice Guide