Firebase Auth has no email OTP. Here is how to add one
Firebase Auth offers email links, not 6-digit email codes. How to build email OTP sign-in with a Cloud Function and a custom token.
Firebase Auth has phone sign-in by SMS and email link sign-in, but no email OTP: no 6-digit code the user types back. Links break on phones because the email app often opens them in a different browser from the one where sign-in started. Here is how to add a real code, keeping Firebase as your login system.
The idea
- Your server makes a 6-digit code, emails it, and keeps a hash of it with an expiry.
- The user types the code into your app.
- Your server checks it, then creates a Firebase custom token for that user with the Admin SDK.
- The app calls
signInWithCustomToken, and the user is signed in to Firebase like any other.
Server: send and check
import { getAuth } from "firebase-admin/auth";
import crypto from "node:crypto";
// Send
export async function sendCode(email) {
const code = crypto.randomInt(0, 1_000_000).toString().padStart(6, "0");
const hash = crypto.createHmac("sha256", process.env.OTP_SECRET).update(email + code).digest("hex");
await db.codes.put(email, { hash, expires: Date.now() + 10 * 60_000, tries: 0 });
await sendEmail(email, `Your code is ${code}`);
}
// Check, then hand back a Firebase custom token
export async function verifyCode(email, code) {
const row = await db.codes.get(email);
if (!row || row.expires < Date.now() || row.tries >= 5) throw new Error("expired");
const hash = crypto.createHmac("sha256", process.env.OTP_SECRET).update(email + code).digest("hex");
if (!crypto.timingSafeEqual(Buffer.from(hash), Buffer.from(row.hash))) {
await db.codes.update(email, { tries: row.tries + 1 });
throw new Error("wrong code");
}
await db.codes.delete(email);
return getAuth().createCustomToken((await userFor(email)).uid);
}
// Find the Firebase user for this address, or make one. Only "not found" makes a new user:
// any other error (a network fault, a bad service account) must not.
async function userFor(email) {
try {
return await getAuth().getUserByEmail(email);
} catch (e) {
if (e.code !== "auth/user-not-found") throw e;
return getAuth().createUser({ email, emailVerified: true });
}
}
App: sign in with the token
import { getAuth, signInWithCustomToken } from "firebase/auth";
const { token } = await fetch("/api/verify-code", { method: "POST", body: JSON.stringify({ email, code }) }).then((r) => r.json());
await signInWithCustomToken(getAuth(), token);
What you still have to get right
- Store only a hash of the code, with an expiry and a try count, as above.
- Rate limit sends per email and per IP, and add a bot check on the form.
- Email delivery: a verified domain and a transactional provider, or codes land in spam.
- Cleanup: delete codes once used or expired.
- Create users only on “not found”. A catch-all around
getUserByEmailturns a network fault into a second account attempt.
The same with SignOps
SignOps Email OTP does the first two steps (making, storing, sending and checking the code), so your server only creates the custom token:
import { SignOps } from "@signops/node";
const signops = new SignOps(process.env.SIGNOPS_KEY);
// Send: keep the otp_id for the next request (your session, or hand it to the app)
const { otp_id } = await signops.otp.send({ email, app_name: "Your App" });
// Check, then hand back a Firebase custom token for the address the code went to
const r = await signops.otp.verify({ otp_id, code });
if (r.valid) return getAuth().createCustomToken((await userFor(r.email)).uid);
What we saw: the whole flow, in the Firebase emulator
On 24 November 2026 we ran the SignOps version end to end against the Firebase Auth emulator (firebase-tools 15.37.0, firebase-admin 14.5.1, the firebase web SDK 13.4.0, Node 22), with a real code from our test app sent to our own inbox. The emulator needs no Google account: a project id that starts with demo- runs offline.
echo '{"emulators":{"auth":{"port":9099}}}' > firebase.json
npx firebase-tools emulators:start --only auth --project demo-signops
# in the server's shell, so firebase-admin talks to the emulator, not to Google
export FIREBASE_AUTH_EMULATOR_HOST=127.0.0.1:9099
| Step | What happened |
|---|---|
signops.otp.send |
3.7 seconds; the code was in Gmail. A second send to the same address within 30 seconds threw 429 rate_limited, “Wait 17 seconds before sending another.” |
signops.otp.verify with the code |
valid: true in 0.68 seconds, with the address the code went to |
getUserByEmail for a new address |
Threw auth/user-not-found, which is why userFor catches exactly that code |
createUser and createCustomToken |
0.32 seconds; a 379-character token. In the emulator its header says alg: none, unsigned; against real Firebase the Admin SDK signs it with your service account |
signInWithCustomToken in the app |
0.13 seconds. The signed-in user had the same uid, the email set, emailVerified: true (because the server created it that way), and signInProvider: "custom" in its ID token |
| The same address the next time | getUserByEmail found the same uid, so a user signs in to one Firebase account however many codes they use |
We nearly started the server without FIREBASE_AUTH_EMULATOR_HOST. Without it, firebase-admin talks to the real Firebase, not the emulator, so set it in the server’s environment before initializeApp runs.
1,000 codes a month are free, and Starter includes 2,000 a month. The same two calls from a plain backend: Next.js, Laravel.
Sources: Firebase: connect to the Auth emulator, Firebase: create custom tokens, Firebase: email link authentication.