Skip to main content

methods/email-magic-link/react

For AI agents: see llms.txt for the complete documentation index. Markdown versions are available by adding .md to a page URL or requesting Accept: text/markdown.

React client for the emailMagicLink method, exported at @convex-dev/auth/methods/email-magic-link/react.

Type Aliases​

SendResult​

type SendResult =
| {
success: true;
}
| Extract<
SendResult,
{
success: false;
}
>
| {
success: false;
userError: OtherError;
};

The result of the sendMagicLink callback from useSendEmailMagicLink.


SignInState​

type SignInState =
| {
status: "loading";
}
| {
status: "complete";
}
| {
status: "error";
userError:
| Extract<
ClientView<SignInResult>,
{
status: "error";
}
>["userError"]
| MissingSecretError
| OtherError;
};

The state of useSignInWithEmailMagicLink.

Functions​

function useSendEmailMagicLink(api): object;

Client for the one form of the method: send a sign-in link to the address, and keep the returned secret for useSignInWithEmailMagicLink. The same call serves a person who has an account and a person who has none.

function SignIn() {
const { sendMagicLink, pending } = useSendEmailMagicLink(api.auth);
const [email, setEmail] = useState("");
const [sent, setSent] = useState(false);
const [error, setError] = useState<string | null>(null);
if (sent) {
return <p>We sent a sign-in link to {email}.</p>;
}
return (
<form
onSubmit={async (e) => {
e.preventDefault();
setError(null);
const result = await sendMagicLink({ email });
if (result.success) {
setSent(true);
} else {
setError(result.userError.error);
}
}}
>
<input
type="email"
value={email}
onChange={(e) => setEmail(e.target.value)}
/>
<button type="submit" disabled={pending}>
Continue with email
</button>
{error !== null && <p>Could not send the link: {error}</p>}
</form>
);
}

Parameters​

ParameterTypeDescription
api{ sendEmailMagicLink: SendMutation; }The functions of the method, usually api.auth.
api.sendEmailMagicLinkSendMutation-

Returns​

object

pending​
pending: boolean;
sendMagicLink: (args) => Promise<SendResult>;
Parameters​
ParameterType
args{ email: string; }
args.emailstring
Returns​

Promise<SendResult>


function useSignInWithEmailMagicLink(api, emailCode): SignInState;

Client for the landing page of the link: as soon as the page opens, present the code from the link with the secret stored by useSendEmailMagicLink, and adopt the minted session.

function SignInWithLink({ emailCode }: { emailCode: string }) {
const state = useSignInWithEmailMagicLink(api.auth, { emailCode });
switch (state.status) {
case "loading": // signing in…
case "complete": // signed in
case "error": // map state.userError to a message
}
}

Parameters​

ParameterTypeDescription
api{ signInWithEmailMagicLink: SignInMutation; }The functions of the method, usually api.auth.
api.signInWithEmailMagicLinkSignInMutation-
emailCode{ emailCode: string; }The code query parameter of the link.
emailCode.emailCodestring-

Returns​

SignInState