A doctor’s website used to be a brochure. Today it is where patients decide whether to trust you, often before they ever call. For a gastrointestinal surgeon in Algeria we built what we call a digital clinic: a premium website, patient education, an AI assistant, online booking and a private dashboard, in one product.

The interesting engineering isn’t the pages. It’s that this product holds medical data, so every design decision started from one question: who is allowed to read this?

Stack

  • Next.js 14 (App Router) with TypeScript and Tailwind CSS
  • Framer Motion for a calm, premium feel
  • Supabase: PostgreSQL, authentication and row-level security
  • Google Gemini behind the patient assistant
  • Installable as a PWA on Android and iPhone

Layer 1: the database says no by default

Every table has row-level security switched on. The rules read like the clinic’s own policy:

  • Patients can read their own appointments and profile.
  • The doctor can read all of them.
  • Anyone can read published videos and categories.
  • Only the doctor can manage content.

“The doctor” is a role on the profile, checked by a small SQL function that the policies reuse:

create or replace function public.is_doctor() returns boolean as $$ select exists( select 1 from public.profiles where id = auth.uid() and role = 'doctor' ) $$ language sql security definer; create policy "Doctors read all appointments" on public.appointments for select using (is_doctor()); 

Because the rule lives in Postgres, a bug in the interface can hide a button but can’t leak a row.

Layer 2: encrypt the fields that identify people

RLS decides who can query a row. We also wanted the most sensitive columns to be unreadable to anyone who gets the raw table, whether from a backup, a misconfigured export or a leaked service key.

So the appointment row doesn’t store a name, a phone number or a reason for the visit. It stores ciphertext:

full_name_encrypted text not null, phone_encrypted text not null, reason_encrypted text not null, 

Encryption happens on the server before the insert, with AES-256-GCM and a fresh 96-bit IV per value. Each field is stored as iv:authTag:ciphertext:

export function encrypt(plaintext: string): string { const iv = randomBytes(12) const cipher = createCipheriv('aes-256-gcm', KEY, iv) const data = Buffer.concat([cipher.update(plaintext, 'utf8'), cipher.final()]) return `${iv.toString('hex')}:${cipher.getAuthTag().toString('hex')}:${data.toString('hex')}` } 

GCM’s authentication tag means a tampered value fails to decrypt instead of decrypting to garbage. The API decrypts only when an authorised user asks, and returns a neutral placeholder if a value can’t be decrypted, so one bad row never breaks the doctor’s dashboard.

The key lives in an environment variable on the server and never reaches the browser.

Layer 3: treat every public form as hostile

The contact form and the booking form are the two doors anyone can knock on, so both get:

  • Input sanitisation before anything is stored, so a message can’t carry a script into the doctor’s inbox.
  • Rate limiting per client, so nobody can flood the clinic with fake appointment requests.
  • Server-side slot checks: a database function confirms a time slot is free before an appointment is created, so two patients can’t book the same slot.

The AI assistant: helpful, and humble about it

The assistant answers general questions about procedures, preparation and recovery. Its most important job is knowing when not to answer: if a message sounds urgent, it tells the patient to seek emergency care rather than book a consultation.

Two principles guided how we framed it:

  1. It informs; it doesn’t diagnose. The doctor diagnoses. The assistant explains and routes.
  2. Urgency beats convenience. When in doubt, it points to emergency care, even if that costs a booking.

What the doctor gets

A private dashboard to confirm or cancel appointments and export them to Excel, read patient messages, approve reviews, and manage the videos and before/after cases shown on the site. Patients get answers at any hour and book without a phone call.

Takeaways

  • Put access rules in the database. RLS turned “who can see this?” from a code review question into a schema property.
  • Encrypt identifying fields, not just the disk. Disk encryption protects a stolen drive; field encryption protects a leaked table.
  • Design the AI’s refusals first. In healthcare, the answers it won’t give matter more than the ones it will.

Want one for your practice?

This platform was built by THODZ SOLUTIONS. If you’re a doctor or run a clinic, reach us at thodz.com or support@thodz.com.