waitlists - Resend - security - Python
4 min lectura

Double opt-in for a waitlist: count only confirmed email subscribers

Build a waitlist confirmation flow with a locally tested one-time SQLite token, safe GET/POST separation and Resend contact sync.

Double opt-in for a waitlist: count only confirmed email subscribers

You open a waitlist for a new product. Someone enters another person's address, and your system immediately adds it to launch emails. Double opt-in changes the sequence: send a confirmation message first and activate the contact only after the mailbox owner confirms. This solves a different problem from duplicate email retries. It establishes that the address owner chose to join this list.

Resend has an official Next.js example that creates an unsubscribed: true contact, sends a link and subscribes it on confirmation. The example explains the basic flow, but its confirmation route changes state on GET and does not consume the token once. Those are worth hardening before using the sample for a real launch. HTTP semantics defines GET as safe and read-only. Microsoft documents email security tools inspecting links and producing apparent clicks without a human action.

A safer waitlist sequence

  1. Collect the email address and the purpose of the list. Store a pending signup; keep it out of launch campaigns.
  2. Generate 32 random bytes. Put the raw token only in the confirmation link, and store its SHA-256 digest, email and expiry in your database.
  3. GET /confirm?token=... renders a page with a confirmation button but makes no subscription change. The button submits a POST carrying the token.
  4. The server atomically consumes a token that exists, is unexpired and has not been used. Then it syncs the confirmed contact to Resend. Keep a separate sync state so a provider failure can be retried.

Sending the confirmation message requires a Resend API key and verified domain. The current contact API can create the contact after confirmation. If an existing contact previously unsubscribed, do not silently reactivate it without checking its state and obtaining a fresh explicit choice. Make unsubscribing from later campaigns clear as well.

Locally tested core: expiry and one-time use

This example uses only Python's standard library and SQLite. Create the table once. Call confirm from your POST handler, never from the GET page that displays the button. The RETURNING statement requires SQLite 3.35 or later; I ran the local check with SQLite 3.43.2.

CREATE TABLE pending_signups (
  token_hash TEXT PRIMARY KEY,
  email TEXT NOT NULL,
  expires_at INTEGER NOT NULL,
  confirmed_at INTEGER
);
import hashlib, secrets, time

def issue(db, email):
    token = secrets.token_urlsafe(32)
    digest = hashlib.sha256(token.encode()).hexdigest()
    db.execute(
        "INSERT INTO pending_signups (token_hash,email,expires_at) VALUES (?,?,?)",
        (digest, email, int(time.time()) + 86400),
    )
    db.commit()
    return token  # send in the link; never write it to logs

def confirm(db, token):
    now = int(time.time())
    digest = hashlib.sha256(token.encode()).hexdigest()
    row = db.execute(
        """UPDATE pending_signups SET confirmed_at=?
           WHERE token_hash=? AND expires_at>? AND confirmed_at IS NULL
           RETURNING email""",
        (now, digest, now),
    ).fetchone()
    db.commit()
    return row[0] if row else None

issue returns an opaque token. confirm returns the email only on its first valid use. If it returns None, show a neutral error and let the visitor request a new link. In my local check, a valid token confirmed once; replay, an expired token and an invented token were rejected. OWASP recommends random, single-use, time-limited tokens for email verification.

Finish the production workflow

This core is not a complete production app. Add request limits per address and IP, avoid revealing whether an account already exists, and invalidate earlier links when resending. Persist separate confirmed, provider_pending and synced states. If Resend fails after the token is consumed, a worker should retry the contact operation without demanding another click. A token in a URL can appear in logs and referrers: use HTTPS, suppress token logging and set Referrer-Policy: no-referrer on the confirmation page.

Record the exact signup wording the person saw, the time they submitted the form and the time they confirmed. That evidence helps you distinguish a deliberate subscription from an accidental or repeated signup while debugging. Do not keep the raw token in that record; an event identifier and timestamps are enough. Make the success page say plainly which list the person joined and where to change that choice later.

With your own account, test that a pending address is absent from campaigns, visiting the GET page changes nothing, the POST activates it once, replay and expiry fail, and a simulated provider failure eventually syncs. No live Resend email or contact activation was performed while preparing this guide. For a founder, the useful metric is confirmed interest, not the raw count of addresses entered into a form.

Comentarios

Aún no hay comentarios. Sé el primero.

Deja un comentario