All posts
authbackend

WhatsApp OTP Login, and What I'd Add the Second Time

In Indonesia, people check WhatsApp before email. Here's how an OTP login works, and the guardrails that are easy to skip on a first build.

Muhamad Choirul Adam 3 min readPhoto: Unsplash

The reseller system I built as an intern logs people in with a code sent to WhatsApp. No password, no email. For our users that was the obvious choice: in Indonesia, almost everyone has WhatsApp open all day, and plenty of resellers never check email at all.

The happy path is simple. The parts that make it safe are where the work is.

The flow

Rendering diagram…

Five steps. Here's what I'd make sure of from day one if I built it again.

1. Normalize the phone number first

Indonesian users type their number every way possible: 0812..., 812..., +62 812..., 62-812.... If you store them as typed, the same person gets two accounts.

function normalizePhone(raw: string) {
  const digits = raw.replace(/\D/g, "");
  if (digits.startsWith("62")) return `+${digits}`;
  if (digits.startsWith("0")) return `+62${digits.slice(1)}`;
  return `+62${digits}`;
}

Store everything in E.164 format (+62812...) and normalize at the edge, before anything touches the database.

2. Store a hash, not the code

An OTP is a short-lived password. Treat it like one: store a hash and compare in constant time. If your database leaks, active codes shouldn't leak with it.

const code = crypto.randomInt(100000, 1000000).toString();
await db.otp.upsert({
  phone,
  codeHash: sha256(code),
  expiresAt: addMinutes(new Date(), 5),
  attempts: 0,
});

Use a cryptographic random source. Math.random() is not one.

3. Limit everything

Six digits is a million combinations. Without limits, that's not much.

  • Expiry: five minutes is plenty.
  • Attempts: after five wrong codes, the code is dead. Ask for a new one.
  • Resend cooldown: sixty seconds between sends to the same number.
  • Rate limit by IP too: one attacker can try many phone numbers.
  • One use: delete the code as soon as it's accepted.

Sending a WhatsApp message also costs money per message, so rate limits protect your bill as well as your users.

4. Don't reveal who has an account

"This number isn't registered" tells an attacker which numbers are. Respond the same way either way: "If this number can sign in, we've sent a code." Then decide quietly whether to create the account on verification.

5. Plan for WhatsApp being down

Message delivery depends on a provider you don't control. Show a clear "didn't get it?" path after the cooldown, and have a fallback ready, like SMS or email, even if you rarely need it.

The part users notice

None of the above is visible when it works. What users see is how fast the code arrives, whether the input auto-fills, and whether a wrong digit gives a clear message instead of a generic error. Get the guardrails right, then spend your remaining time there.