Custom sign-up flow
The prebuilt <SignUp /> runs this flow for you. To build your own form, drive the sign-up attempt
with the useSignUp hook from @zautha/react or with zautha.signUp from
@zautha/js. Like the sign-in resource, every method returns { error } and never throws.
The shape of a flow
Section titled “The shape of a flow”createstarts the attempt with the fields you have. On a production instance it first waits for the bot check (see Bot protection).- The attempt’s
snapshotlists what is left:requiredFields(what the instance asks for),missingFields(not given yet) andunverifiedFields(given but waiting for a code, e.g.email_address).updateadds fields. - Send and check the email code with
emailCode.sendCode()andemailCode.verifyCode({ code }). - When
statusiscomplete, callfinalize()to make the new session the active one.
status |
The attempt waits for |
|---|---|
missing_requirements |
the fields in missingFields or unverifiedFields |
complete |
finalize() |
abandoned |
nothing: it expired, start a new one |
Create, verify, finalize
Section titled “Create, verify, finalize”import { useSignUp } from "@zautha/react";import { useState, type FormEvent } from "react";
export function SignUpForm() { const { isLoaded, signUp, errors, fetchStatus } = useSignUp(); const [email, setEmail] = useState(""); const [password, setPassword] = useState(""); const [code, setCode] = useState("");
if (!isLoaded || signUp === null) return null;
async function start(event: FormEvent) { event.preventDefault(); const created = await signUp!.create({ emailAddress: email, password }); if (created.error !== null) return; await signUp!.emailCode.sendCode(); }
async function verify(event: FormEvent) { event.preventDefault(); const { error } = await signUp!.emailCode.verifyCode({ code }); if (error === null && signUp!.status === "complete") await signUp!.finalize(); }
if (signUp.snapshot.unverifiedFields.includes("email_address")) { return ( <form onSubmit={verify}> <input value={code} onChange={(e) => setCode(e.target.value)} autoComplete="one-time-code" /> {errors.fields.code && <p>{errors.fields.code.message}</p>} <button disabled={fetchStatus === "fetching"}>Verify</button> </form> ); }
return ( <form onSubmit={start}> <input value={email} onChange={(e) => setEmail(e.target.value)} autoComplete="email" /> {errors.fields.emailAddress && <p>{errors.fields.emailAddress.message}</p>} <input type="password" value={password} onChange={(e) => setPassword(e.target.value)} autoComplete="new-password" /> {errors.fields.password && <p>{errors.fields.password.message}</p>} <button disabled={fetchStatus === "fetching"}>Sign up</button> </form> );}The same with zautha.signUp:
import { Zautha } from "@zautha/js";
const zautha = new Zautha("pk_test_…");await zautha.load();const { signUp } = zautha;
await signUp.create({ emailAddress: "ada@example.com", password: "a long passphrase" });await signUp.emailCode.sendCode();await signUp.emailCode.verifyCode({ code: "123456" });
if (signUp.status === "complete") { await signUp.finalize();} else { console.log(signUp.snapshot.missingFields); // e.g. ["username"]: collect it, then signUp.update({ username })}When the instance asks for more than an address and a password (a username, first and last name), create and
update take them: { username, firstName, lastName }. unsafeMetadata carries data of your own onto the user.
Field errors
Section titled “Field errors”As in the sign-in flow, a failed call fills errors: errors.fields keyed by the camelCase parameter name and
errors.global for the rest. The ones a sign-up form meets:
| Field | Code | Meaning |
|---|---|---|
emailAddress |
form_identifier_exists |
an account has this address already |
emailAddress |
identifier_not_allowed |
the instance’s sign-up restrictions refuse the address |
emailAddress |
form_email_invalid |
not an email address |
password |
form_password_length, form_password_pwned |
too short, or found in a data breach |
username |
form_username_exists, form_username_invalid |
taken, or not allowed |
code |
form_code_incorrect |
the wrong email code |
import { Zautha, isZauthaApiError } from "@zautha/js";
const { signUp } = new Zautha("pk_test_…");
const { error } = await signUp.create({ emailAddress: "ada@blocked.example" });if (error !== null) { const field = signUp.errors.fields.emailAddress; if (field?.code === "identifier_not_allowed") { // Show a message of your own: the answer does not say which list refused it, // and does not depend on whether the address already has an account. } else if (isZauthaApiError(error, "form_identifier_exists")) { // Offer sign-in instead. }}For an address typed into a form, identifier_not_allowed is an answer about the address only, not about whether it
has an account, so a form cannot be used to find out who is a user. With an invitation’s ticket the field is ticket; with a social provider that
gave no email address, the address the user types later is checked then. Handle it on whichever field it arrives:
loop over Object.values(signUp.errors.fields) if you show one message for all.
With an organization invitation’s ticket
Section titled “With an organization invitation’s ticket”An invitation email links to the hosted login’s /invitation page, which signs the invitee up (or in) with the link’s
ticket. If you carry the ticket to a page of your own, pass it to create. The address is the invitation’s and is
verified already, so no code is sent:
import { Zautha } from "@zautha/js";
const { signUp } = new Zautha("pk_test_…");
declare const ticket: string; // the link's __zautha_ticket parameter
await signUp.create({ strategy: "ticket", ticket, password: "a long passphrase" });if (signUp.status === "complete") await signUp.finalize();The invitation is accepted when the user is created. A used or revoked ticket is form_ticket_invalid on ticket.
Social providers
Section titled “Social providers”signUp.authenticateWithRedirect({ strategy, redirectUrl, redirectUrlComplete }) leaves for the provider and takes no
password. See Social sign-in.