Email — Verification
Email verification extension for Caffeine AI.
Overview
This skill adds email address verification via a click-to-verify link that lands on the app's own frontend. The backend mints and stores the link token (verificationTokens) and confirms it through MixinEmailVerification; verifiedEmails tracks verified addresses. The frontend renders the /verify-email page, where the recipient clicks a button to confirm.
Required Setup Checklist
All five steps are mandatory.
- mops dependencies —
mops add caffeineai-email-verificationandmops add caffeineai-email. - Two stable state fields —
verifiedEmails : VerifiedEmails.StateandverificationTokens : VerificationTokens.State, initialised in the migration chain head withVerifiedEmails.new()andVerificationTokens.new(). - Mixin invocation —
include MixinEmailVerification(verifiedEmails, verificationTokens)inmain.mo. - Sending —
EmailVerification.sendVerificationEmail(verificationTokens, fromUsername, recipients, subject, htmlBody); the body MUST contain{{VERIFICATION_URL}}. - Frontend npm package and route —
@caffeineai/email-verificationinstalled, and a public route/verify-email(reachable without signing in) that renders a button callingconfirm().
CRITICAL: the link in the email is https://<app domain>/verify-email?token=…. Without the frontend route the recipient lands on a missing page and the address is never verified.
Backend
This component is for sending an email to users with a verification link which the user can click to prove they own the email address.
To check if an email address has been verified
Use the prefabricated module mo:caffeineai-email-verification/verifiedEmails.mo which cannot be modified.
To check whether an email is verified use the contains function. Do NOT try to track the email verification status independently by storing it against the user profile.
Pending verification links
Use the prefabricated module mo:caffeineai-email-verification/verificationTokens.mo which cannot be modified. It holds the tokens of links sent but not yet clicked; a token is single use and expires after 24 hours. The app only declares the state and passes it around.
To handle the verification link
Use the prefabricated module mo:caffeineai-email-verification/verificationMixin.mo which cannot be modified.
MixinEmailVerification takes both states. It adds _caffeineEmailConfirmVerification(token), which the app's frontend calls from the verification page: it marks the address the token was sent to as verified and returns it. Anyone holding a valid token can call it, the anonymous principal included, so the recipient needs no account. It also keeps the legacy _caffeineEmailVerify callback so links in mail sent before this version keep working.
For sending users a verification email
- Use the
sendVerificationEmailfunction of the prefabricated modulemo:caffeineai-email-verification/verification.mowhich cannot be modified. Do NOT callEmailClient.broadcastViaGatewayfrom app code: this module prepares the tokens and the link template it sends. - It returns a SendResult which is #ok if the email is sent successfully otherwise #err(error) with the error text.
- Each recipient receives an individual email with a specific verification link for them, pointing at the app's
/verify-emailpage. - The htmlBody MUST contain the placeholder text {{VERIFICATION_URL}}; the function returns #err when it is missing.
- The link lands on the app's
/verify-emailpage when the canister has the mail gateway key (INTEGRATIONS_GATEWAY_URL/INTEGRATIONS_GATEWAY_API_KEY, injected by the platform where the gateway is enabled). Without the key the mail goes through the transport canister as before: the transport fills{{VERIFICATION_URL}}with its own link and the click is confirmed through_caffeineEmailVerify. The app code is the same either way.
Example usage with endpoints for registering a user and for checking whether a user is verified.
The migration chain head:
Upgrading from the previous version
An app built with caffeineai-email-verification 0.1.x and caffeineai-email 0.2.x needs all of these:
- Re-pin:
mops add [email protected]andmops add [email protected]. - Declare the new stable field
verificationTokens : VerificationTokens.Stateand add it to the migration chain (a new migration file initialising it withVerificationTokens.new();verifiedEmailskeeps its shape and its data). - Change
include MixinEmailVerification(verifiedEmails)toinclude MixinEmailVerification(verifiedEmails, verificationTokens). - Replace
EmailClient.sendVerificationEmail(fromUsername, recipients, subject, htmlBody)withEmailVerification.sendVerificationEmail(verificationTokens, fromUsername, recipients, subject, htmlBody)(importmo:caffeineai-email-verification/verification). - Add the frontend package and the
/verify-emailroute described below.
Frontend
Install @caffeineai/email-verification. It exports the prefabricated hook useEmailVerification, which cannot be modified:
Rules for the verification page:
- Add a route at
/verify-emailthat is reachable WITHOUT signing in: recipients open it from their mail client and have no session. The backend method takes the token as its only credential, so the anonymous actor fromuseActoris enough. - Read the token with the hook (it takes
?token=from the URL itself) and render a button that callsconfirm(). NEVER callconfirm()on page load or in an effect: mail clients, link previewers and scanners fetch the URL without a person behind it, and a fetch must verify nothing. - Show the outcome from
status:donewith the verifiedemail,invalidfor a missing, used or expired link (offer to request a new email),errorwith the message.
Example page:
Register it with the app's TanStack Router alongside the other routes, as a child of the root route with path: '/verify-email', outside any authenticated layout.
If there is a UI for the admin to enter the content of a verification email then indicate that the placeholder text {{VERIFICATION_URL}} must be present in the email body.

