Skip to main content

useMagicLink, useEmailFirst

Hooks for a sign-in card of your own. Ask which methods the organization offers, and send a sign-in link by email.

ComponentReadyView source
Works with
ReactNext.js

Type any address. The preview answers as an organization that offers magic links, passkeys and passwords.

Installation​

npm install @lumoauth/react

Usage​

The card has three steps, and each hook covers one of them.

  1. Ask for the address. useEmailFirst().checkEmail(email) asks which sign-in methods the organization offers.
  2. Offer the methods. Show a button for each flag of the result that is true.
  3. Send the link. useMagicLink().sendMagicLink(email) asks LumoAuth to send it. isSent becomes true when the server accepted the request.
import { useEmailFirst, useMagicLink, useAuth } from '@lumoauth/react';

const { checkEmail, result } = useEmailFirst();
const { sendMagicLink, isSent, error, errorCode } = useMagicLink();
const { signInWithRedirect } = useAuth();

await checkEmail(email);

if (result?.magicLinkEnabled) await sendMagicLink(email);
else signInWithRedirect(); // the hosted page offers every other method

A passkey and a password are parts of the hosted sign-in page. For those, send the person there with signInWithRedirect().

The demo above reuses the class names of the SDK cards (la-card, la-input, la-btn), so that it matches SignIn. Your own card can use any markup.

Before it works​

  • Turn on magic links in your organization.
  • Add the origin of your app to the allowed origins of the application. A request from another origin is refused.

The link signs the person in at LumoAuth and then returns them to your app.

You callThe person returns toWhat finishes the sign-in
sendMagicLink(email)The page that asked for the link.The provider, by itself.
sendMagicLink(email, returnTo)returnTo, a page of your app. Its origin must be an allowed origin or a redirect URI origin of the application.Your page: call signIn() on it.

A link is valid for 15 minutes.

Refusals​

sendMagicLink from this hook does not throw when the server refuses. It sets error and errorCode.

errorCodeMeaningWhat to do
magic_link_disabledThe organization does not offer magic links.Offer another method. checkEmail tells you this in advance.
origin_not_allowedThe request came from an origin that the application does not list.Add the origin to the application.
captcha_requiredThe organization asks for a CAPTCHA on this request.Show the CAPTCHA, then pass its answer: sendMagicLink(email, undefined, { captchaToken }).
rate_limitedToo many requests.Ask the person to wait.

What the hooks do not reveal​

The server gives the same answer for every address. isSent does not mean that the address has an account, and the result of checkEmail describes the organization, not the person. Word your messages in the same way: "If this address can sign in here, a link is on its way."

Returns​

ReturnsTypeDescription
sendMagicLink(email, returnTo?, { captchaToken? }?) => Promise<void>Ask for a sign-in link. A refusal does not throw: it sets error and errorCode.
isLoadingbooleanTrue while the request runs.
isSentbooleanTrue after the server accepted the request. It does not mean that the address has an account.
errorstring | nullThe message when the request failed.
errorCodestring | nullThe reason of the server when it refused the request.
reset() => voidGo back to the idle state.

useEmailFirst​

ReturnsTypeDescription
checkEmail(email) => Promise<EmailCheckResult>Ask which sign-in methods the organization offers.
isLoadingbooleanTrue while the check runs.
resultEmailCheckResult | nullThe result of the last check.
existsboolean | nullShorthand for result.exists.
reset() => voidGo back to the idle state.

EmailCheckResult​

ReturnsTypeDescription
existsbooleanTrue when the server answered the check. False when the check was not available: a network failure, a rate limit or a refused request. Every other flag is then false.
magicLinkEnabledbooleanThe organization offers magic links.
passkeyEnabledbooleanThe organization offers passkeys.
passwordEnabledbooleanThe organization offers passwords.

The result also has hasPasskey and hasPushDevice. Both are deprecated and always false: the server does not say which methods one address has.