Skip to Content

Additional Emails

More verified addresses on an account than the one it signs in with

An account has one email, and it is the address the person signs in with. Some applications decide things by any address a person can prove: a claim on a company's page for whoever owns an address at its domain, a discount for an academic address. The additional emails plugin lets an account hold more addresses, each proven by a code sent to it.

They are for your application to read. They are never a way into the account: sign-in, password reset and provider linking look at the user's own email and nowhere else.

Installation

Add the plugin to your auth config

auth.ts
import { entroAuth } from "@entro314labs/entro-auth";
import { additionalEmails } from "@entro314labs/entro-auth/plugins"; 

export const auth = entroAuth({
    // ... other auth config
    plugins: [
        additionalEmails({ 
            async sendVerificationCode({ email, code, user }) { 
                await sendEmail({ 
                    to: email, 
                    subject: "Confirm this address", 
                    text: `Your code is ${code}`, 
                }); 
            }, 
        }), 
    ],
});

Migrate the database

Run the migration or generate the schema to add the userEmail table.

npx @entro314labs/entro-auth-cli@latest migrate

See the Schema section to add the table by hand.

Add the client plugin

auth-client.ts
import { createAuthClient } from "@entro314labs/entro-auth/client";
import { additionalEmailsClient } from "@entro314labs/entro-auth/client/plugins"; 

export const authClient = createAuthClient({
    plugins: [
        additionalEmailsClient(), 
    ],
});

Usage

Adding an Address

Ask for a code, then hand it back:

await authClient.additionalEmail.request({ email: "ada@work.example" });

const { error } = await authClient.additionalEmail.verify({
    email: "ada@work.example",
    code: "123456",
});

Both calls need a fresh session, since what your application grants by these addresses is granted to whoever adds one.

request always answers { status: true }. It sends nothing when the address is not free: when it is somebody's verified sign-in address, somebody's additional address, or the person's own. The answer being the same, it does not tell who holds an address.

verify stores the address. A wrong code answers INVALID_OTP; after three wrong ones the code is spent and the answer is TOO_MANY_ATTEMPTS. A code is good for ten minutes. Both calls are rate limited.

Listing and Removing

const { data } = await authClient.additionalEmail.list();
// [{ email: "ada@work.example", createdAt: ... }]

await authClient.additionalEmail.remove({ email: "ada@work.example" });

Reading Them on the Server

listVerifiedEmails returns every address an account has proven: its sign-in address when that is verified, then its additional ones. It has no HTTP route.

server.ts
const emails = await auth.api.listVerifiedEmails({ body: { userId } });
const mayClaim = emails.some((email) => email.endsWith("@acme.example"));

One Account per Address

An address is one account's at most, as a sign-in address or as an additional one.

Nothing stops a sign-up, or a change of sign-in address, to an address that somebody else holds as an additional one. The moment that account's address is verified, it is taken off the other account. The newest proof of a mailbox is the one that counts, and whoever controls it now is the one your application should believe.

An unverified sign-in address proves nothing, so it does not stop somebody adding the address to their own account.

Options

sendVerificationCode: (data: { email, code, user }, request?) => Promise<void> - Mails the code to the address being added. Required.

expiresIn: number - How long a code is good for, in seconds. Default: 600.

allowedAttempts: number - How many wrong codes are taken before the code is spent. Default: 3.

Schema

Table name: userEmail

Table
Field
Type
Attributes
Description
id
string
PK
Unique identifier for each row
userId
string
FK
The account the address belongs to
email
string
UQ
The address, in lower case; unique
createdAt
Date
-
When the address was proven

On this page