See Ash Code logo See Ash Code

The user was signed in. The email field was missing.

Ashlynn Antrobus
• • 3 min read

The user was signed in, but my backend couldn’t find the email field it expected in the token payload.

My middleware used payload["email"] after verifying the token. A valid identity didn’t guarantee that the decoded claims contained that application field.

An open address book has blank contact fields, including email lines.

Address book 1 by Mx. Granger, CC0 1.0; unmodified.

The June 27 change adds a fallback lookup using the verified subject:

email = payload.get("email") or clerk_adapter.fetch_user_email(
    clerk_user_id
)

It is another failure after the hostname decoding bug, but at a different point. This time the application needed profile data after token verification.

Two claim sets to check

These are fabricated decoded-claim examples, not signed tokens:

{"sub": "user_demo", "email": "[email protected]"}
{"sub": "user_demo"}

The first can supply the email directly. The second sends the verified user ID to the profile lookup. An empty email value also takes the fallback because the expression uses or.

That lets me test the field-handling rule independently from cryptographic verification. It doesn’t mean an arbitrary JSON object containing sub should be accepted as authentication.

Follow the profile response

The adapter requests the user’s details from the configured Clerk Backend API with the backend credential. It looks for the email entry whose ID matches primary_email_address_id.

For a fake response containing two email entries, a useful check would make the primary ID point to the second one. That catches an implementation that simply takes the first address and happens to pass a one-address example.

If the primary entry isn’t found, this implementation uses the first nonempty email address. If there isn’t one, it returns an empty string. Those are policies in this adapter, not facts established merely by a successful sign-in.

The fallback adds another dependency

The verified token can be valid while the Backend API request fails. The adapter translates request failures into an authentication error for the application’s caller.

That changes the request path. An email-free token now needs profile data as well as successful verification. I need tests for the failed lookup, not only the response that supplies an address.

The adapter caches email results by user ID in memory, including an empty result. That avoids repeating the same lookup for every request handled by that adapter instance.

There isn’t an expiry policy on this email cache in the patch. The nearby key cache has its own timing, but that doesn’t give the email cache the same expiration. If a profile changes, refreshing that data is another decision to make explicitly.

The application now checks the profile when the verified token has no email. When the token already carries one, there’s no need to make that extra request.

AA

Written by Ashlynn Antrobus

Software developer, author, and builder. Sharing technical lessons learned from building tools like Ebook2Text, IndexedDict, and rag-mcp, and navigating life as a programmer with dyslexia.