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.
Type any address. The preview answers as an organization that offers magic links, passkeys and passwords.
Installation
- npm
- pnpm
- Yarn
- Bun
npm install @lumoauth/react
pnpm add @lumoauth/react
yarn add @lumoauth/react
bun add @lumoauth/react
Usage
The card has three steps, and each hook covers one of them.
- Ask for the address.
useEmailFirst().checkEmail(email)asks which sign-in methods the organization offers. - Offer the methods. Show a button for each flag of the result that is true.
- Send the link.
useMagicLink().sendMagicLink(email)asks LumoAuth to send it.isSentbecomes 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.
Where the link returns
The link signs the person in at LumoAuth and then returns them to your app.
| You call | The person returns to | What 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.
errorCode | Meaning | What to do |
|---|---|---|
magic_link_disabled | The organization does not offer magic links. | Offer another method. checkEmail tells you this in advance. |
origin_not_allowed | The request came from an origin that the application does not list. | Add the origin to the application. |
captcha_required | The organization asks for a CAPTCHA on this request. | Show the CAPTCHA, then pass its answer: sendMagicLink(email, undefined, { captchaToken }). |
rate_limited | Too 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
useMagicLink
| Returns | Type | Description |
|---|---|---|
sendMagicLink | (email, returnTo?, { captchaToken? }?) => Promise<void> | Ask for a sign-in link. A refusal does not throw: it sets error and errorCode. |
isLoading | boolean | True while the request runs. |
isSent | boolean | True after the server accepted the request. It does not mean that the address has an account. |
error | string | null | The message when the request failed. |
errorCode | string | null | The reason of the server when it refused the request. |
reset | () => void | Go back to the idle state. |
useEmailFirst
| Returns | Type | Description |
|---|---|---|
checkEmail | (email) => Promise<EmailCheckResult> | Ask which sign-in methods the organization offers. |
isLoading | boolean | True while the check runs. |
result | EmailCheckResult | null | The result of the last check. |
exists | boolean | null | Shorthand for result.exists. |
reset | () => void | Go back to the idle state. |
EmailCheckResult
| Returns | Type | Description |
|---|---|---|
exists | boolean | True 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. |
magicLinkEnabled | boolean | The organization offers magic links. |
passkeyEnabled | boolean | The organization offers passkeys. |
passwordEnabled | boolean | The 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.