Email-first sign-in with a magic link
Ask for the email address first, then offer the next step: a link by email, or the hosted page for a password or a passkey.
What it shows
- The card asks for the email address and calls the email check,
useEmailFirst().checkEmail(). - The card shows a button for each method that the organization offers.
- For a magic link,
useMagicLink().sendMagicLink(email)asks LumoAuth to send one. - The person opens the link and confirms on the page Continue signing in of LumoAuth.
- LumoAuth sends the browser back to the page that asked for the link, and the provider finishes the sign-in there.
Before you start
- A public application with a redirect URI, and the origin of your app in its Allowed origins. The Starter App of a new organization accepts
http://localhost:5173. Sign in through the hosted page has the steps for an application of your own. - Magic links turned on. Open Sign-in settings in the portal, choose Login flow, turn on Magic link login and click Save changes.
- A user whose mailbox you can read.
| Variable | Value |
|---|---|
LUMOAUTH_URL | The address of LumoAuth: https://app.lumoauth.dev, or https://eu.app.lumoauth.dev for the EU region. |
LUMOAUTH_ORG_ID | Your organization, for example acme-corp. |
LUMOAUTH_CLIENT_ID | The client ID of the application. A browser app has no client secret. |
Code
Run each command from the recipes folder of the examples repository. Set the folder up once for your language. Every version below was run against a LumoAuth organization. The recipe is a small project with its own package.json. A real browser asked for the link, opened it from the message and came back signed in.
- React
sign-in/email-first-and-magic-link/js/src/App.jsx
// The card: ask for the address, show the methods, send the link.
import { useState } from 'react';
import { SignedIn, SignedOut, SignOutButton, useAuth, useEmailFirst, useMagicLink, useUser } from '@lumoauth/react';
export default function App() {
const user = useUser();
return (
<main>
<h1>Email-first sign-in</h1>
<SignedOut>
<SignInCard />
</SignedOut>
<SignedIn>
<p>{user ? `Signed in as ${user.email}` : 'Loading the profile…'}</p>
<SignOutButton />
</SignedIn>
</main>
);
}
function SignInCard() {
const [email, setEmail] = useState('');
const { checkEmail, result, isLoading, reset } = useEmailFirst();
const { sendMagicLink, isSent, error, errorCode } = useMagicLink();
const { signInWithRedirect } = useAuth();
// Step 1: ask which methods the organization offers. The answer is the
// same for every address.
async function submitEmail(event) {
event.preventDefault();
const methods = await checkEmail(email);
// exists is false only when the check was not available. Use the hosted page then.
if (!methods.exists) signInWithRedirect();
}
if (!result?.exists) {
return (
<form onSubmit={submitEmail}>
<label>
Email address <input type="email" value={email} onChange={(event) => setEmail(event.target.value)} required />
</label>
<button type="submit" disabled={isLoading}>Continue</button>
</form>
);
}
// Step 3: the server accepted the request. It does not say whether the
// address has an account, so the message does not say it either.
if (isSent) return <p>If this address can sign in here, a link is on its way.</p>;
// Step 2: one button for each method.
const yesNo = (flag) => (flag ? 'yes' : 'no');
return (
<>
<p>
Methods of the organization: magic link {yesNo(result.magicLinkEnabled)}, passkey {yesNo(result.passkeyEnabled)},
password {yesNo(result.passwordEnabled)}
</p>
{result.magicLinkEnabled && (
<button type="button" onClick={() => sendMagicLink(email)}>Email me a sign-in link</button>
)}
{/* A password and a passkey are parts of the hosted page. */}
{(result.passwordEnabled || result.passkeyEnabled) && (
<button type="button" onClick={() => signInWithRedirect()}>Use a password or a passkey</button>
)}
{error && <p role="alert">The link was not sent: {errorCode ?? error}</p>}
<button type="button" onClick={reset}>Use another address</button>
</>
);
}
sign-in/email-first-and-magic-link/js/src/main.jsx
// A sign-in card of your own: the email address first, then a link by email.
//
// cd sign-in/email-first-and-magic-link/js && npm install && npm run dev
//
// Then open http://localhost:5173.
import { createRoot } from 'react-dom/client';
import { AuthCallback, LumoAuthProvider } from '@lumoauth/react';
import App from './App.jsx';
const callbackPath = '/auth/callback';
// The provider also finishes the sign-in when the person comes back from the
// link in the email: it starts the redirect to LumoAuth, which shows no form.
createRoot(document.getElementById('root')).render(
<LumoAuthProvider
domain={import.meta.env.LUMOAUTH_URL}
orgId={import.meta.env.LUMOAUTH_ORG_ID}
clientId={import.meta.env.LUMOAUTH_CLIENT_ID}
redirectUri={window.location.origin + callbackPath}
>
{window.location.pathname === callbackPath ? <AuthCallback afterSignInUrl="/" /> : <App />}
</LumoAuthProvider>,
);
sign-in/email-first-and-magic-link/js/vite.config.js
import { defineConfig } from 'vite';
import react from '@vitejs/plugin-react';
// The browser gets these three values and nothing else from the environment.
// None of them is a secret: a browser app has no client secret and no API key.
export default defineConfig({
plugins: [react()],
define: {
'import.meta.env.LUMOAUTH_URL': JSON.stringify(process.env.LUMOAUTH_URL ?? 'https://app.lumoauth.dev'),
'import.meta.env.LUMOAUTH_ORG_ID': JSON.stringify(process.env.LUMOAUTH_ORG_ID ?? ''),
'import.meta.env.LUMOAUTH_CLIENT_ID': JSON.stringify(process.env.LUMOAUTH_CLIENT_ID ?? ''),
},
});
cd sign-in/email-first-and-magic-link/js && npm install && npm run dev
Open http://localhost:5173.
Check the result
Type the address of the user and click Continue, then Email me a sign-in link. The page shows the first two lines. Open the link in the message, in the same browser, and click Sign in. The app then shows the third line.
Output
Methods of the organization: magic link yes, passkey no, password yes
If this address can sign in here, a link is on its way.
Signed in as maya.chen@example.com
- Type an address that has no account. The page shows the same two lines, and no message is sent.
- The first sign-in of a person to an application shows the consent page before the browser returns to your app.
- Turn Magic link login off and load the page again. The first line says "magic link no", and the card has no button for the link.
Limits
- A link is valid for 15 minutes, unless you change Link expiry in the same section of the portal. It works once.
- The email check describes the organization, not the person. The card cannot tell a known address from a new one, so it cannot send a new person to sign-up.
- A request from an origin that the application does not list is refused with
origin_not_allowed. The email check then returnsexists: false, and the card falls back to the hosted page. - Use a password or a passkey opens the hosted page, which asks for the address again.
- The link signs the person in at LumoAuth, in the browser that opens it. The app gets its tokens in that browser, not in the one that asked for the link.