Guides/Production patterns
Sign up with an emailed code first
Accounts start by proving their address with a one-time code; a password is something a person adds later.
The problem
With password sign-up open, an account can exist under an address its owner never confirmed. An application that wants every account to start from a proven address, and still lets people use a password later, combines the Email OTP plugin with password sign-in whose sign-up is closed.
When to use it
- Sign-up should be passwordless, and passwords optional.
- Accounts carried over from another provider without a password should be able to sign in and set one.
- Sign-up is by invitation only, combined with the Waitlist plugin.
Setup
Configure the server
import { entroAuth } from "@entro314labs/entro-auth";
import { emailOTP } from "@entro314labs/entro-auth/plugins";
export const auth = entroAuth({
emailAndPassword: {
enabled: true,
disableSignUp: true,
},
plugins: [
emailOTP({
async sendVerificationOTP({ email, otp, type }) {
// send the code with your mail provider
},
}),
],
});/sign-up/email is refused. An account is made by signing in with a code.
Sign in, and sign up, with a code
await authClient.emailOtp.sendVerificationOtp({ email, type: "sign-in" });
// the person types the code
await authClient.signIn.emailOtp({ email, otp });The first successful sign-in for an address creates the account.
Set a password later
Setting a first password and resetting a forgotten one are the same two calls, so one screen serves both:
await authClient.emailOtp.requestPasswordReset({ email });
// the person types the code and a password
await authClient.emailOtp.resetPassword({ email, otp, password });For an account that never had a password, this creates its password credential. From then on the person signs in with either.
Invitation only
With the Waitlist plugin, only invited addresses can create an account, by code or otherwise. A sign-in code is still sent to an address without an invitation; the refusal comes when the code is used. To send none, set emailOTP({ disableSignUp: true }) and let invited people create their account another way, such as /sign-up/email with password sign-up left open. That variant is no longer code-first: the account starts with a password, and the invitation is what ties it to the address. See What Is Refused.
Protect the code endpoints
The Captcha plugin protects password sign-up, password sign-in and password reset by default, and none of the code endpoints. Name them in endpoints, which replaces the defaults:
import { captcha } from "@entro314labs/entro-auth/plugins";
captcha({
provider: "cloudflare-turnstile",
secretKey: process.env.TURNSTILE_SECRET_KEY!,
endpoints: [
"/email-otp/send-verification-otp",
"/email-otp/request-password-reset",
"/sign-in/email", // keep it if people sign in with a password
],
});An endpoint that still answers under an older name is covered under that name too: /email-otp/request-password-reset also covers /forget-password/email-otp.
Pitfalls
- Decide who may sign up before choosing this flow. A code sign-in creates an account for any address unless the waitlist holds it or
disableSignUpis set on the plugin. If the previous provider restricted sign-up, carry that restriction over. - A password set before the address was proven does not survive. If password sign-up stays open, an account can be created under someone else's address. When the address's owner signs in with a code, the password and the sessions that predate the proof are removed. Closing password sign-up avoids the case.
- A real captcha cannot be solved by an automated browser. A production check of the sign-in form with a captcha is a person's to make.
- The i18n plugin translates the library's own error codes only. A code your application throws from a hook, such as an account it has closed, reaches the client in the words it was thrown with. Translate it in your forms, by its code.