Skip to content

Custom email links

An email link replaces the code: the user clicks a link in the email and lands on a page of yours, which finishes the sign-in or the sign-up’s email verification. It works for zautha.signIn and zautha.signUp (and useSignIn, useSignUp) alike.

import { useSignIn, useSignUp } from "@zautha/react";
export function SendLinks() {
const { signIn } = useSignIn();
const { signUp } = useSignUp();
const redirectUrl = `${window.location.origin}/verify-link`;
return (
<>
{/* Sign-in: starts the attempt for the identifier if needed, then emails the link. */}
<button onClick={() => signIn?.emailLink.sendLink({ identifier: "ada@example.com", redirectUrl })}>
Email me a sign-in link
</button>
{/* Sign-up: after signUp.create(), the address to verify is the attempt's. */}
<button onClick={() => signUp?.emailLink.sendLink({ redirectUrl })}>Email me a verification link</button>
</>
);
}

Without React: zautha.signIn.emailLink.sendLink({ identifier, redirectUrl }) and zautha.signUp.emailLink.sendLink({ redirectUrl }). The sign-in one takes the identifier because it starts the attempt when none waits; the sign-up one needs the attempt signUp.create() made.

redirectUrl is where the link leads. It must be an absolute URL on one of your instance’s allowed origins (localhost on a development instance; on a production instance every https origin on your domain and its subdomains, and the origins you add under Auth settings; see Instances). Any other URL is refused with redirect_url_invalid, and so is one with a query or fragment parameter that starts with __zautha_.

The link opens redirectUrl with the attempt’s id and a single-use token in the URL fragment (#__zautha_link_token=…&__zautha_link_attempt=…): a fragment never reaches your server or its logs. You do not read it. zautha.load() (the React provider calls it) takes the parameters out of the address and completes the attempt with completeEmailLink(attemptId, token) on signIn or signUp, then finalizes it when the attempt is complete. The page only has to render from the resource’s state:

import { useSignIn } from "@zautha/react";
export function VerifyLink() {
const { isLoaded, signIn, errors } = useSignIn();
if (!isLoaded || signIn === null) return <p>Checking the link…</p>;
if (signIn.status === "link_opened_elsewhere") {
return <p>Open this link in the browser where you started, or type the code from the same email there.</p>;
}
if (signIn.status === "needs_second_factor") return <p>Enter your second factor.</p>;
if (errors.global.length > 0) return <p>{errors.global[0]?.message}</p>;
return <p>Signed in.</p>;
}

A sign-up link works the same way on useSignUp: signUp.status is complete (then finalized) or missing_requirements while fields are left.

A link opened in a browser that did not start the attempt (the user began on a computer and clicked the link on their phone) cannot sign that browser in: the attempt belongs to the first one. The resource’s status becomes link_opened_elsewhere, an SDK-only value. Tell the user to go back to the browser where they began and enter the code from the same email there (emailCode.verifyCode).

To complete a link yourself, for example from a framework that reads the fragment first, call zautha.signIn.completeEmailLink(attemptId, token) or zautha.signUp.completeEmailLink(attemptId, token); each returns { error } like every other method.