A magic link sign-in has two sides. The device that asked for the link gets a status ID. The link itself opens a page with sct_magic in the URL. Either side can finish the sign-in.
Send a link#
signInOrUp, signIn and signUp send a link when the app's email_auth_type is "magic" and no passkey applies. To send one directly:
const { data, error } = await scute.sendLoginMagicLink("ada@example.com");// data.magic_link.id: the status ID for this devicemagic_pending fires. Links expire after the app's magic_link_expiration.
Finish where the link opens#
The link points to the app's callback URL with ?sct_magic=<token>. On that page:
if (scute.getMagicLinkToken()) { const { data, error } = await scute.signInWithMagicLink(); if (error) showError(error.message); else if (data) showNextStep(data); // MFA, a contact to confirm, or a code}signInWithMagicLink(url?) reads the token from url (default window.location.href), verifies it and signs in. signInWithMagicLinkToken(token, url?) does the same with a token you already have. Both return:
| Result | Meaning |
|---|---|
| { error: null } | Signed in. signed_in fires. |
| { data: { mfaRequired: true, ... } } | MFA is needed. See MFA. |
| { data: { contactVerificationRequired: true, ... } } | A contact to confirm. See Sign in. |
| { data: { verificationRequired: true, verification } } | Attack protection wants a code first. See below. |
| { data: { challengeCompleted: true, signedIn: false, ... } } | The link belonged to a step-up or custom challenge that does not sign in. The challenge is passed. |
| { error: InvalidMagicLinkError } | No token in the URL, or the token does not decode. |
Remove the token from the address bar after reading it. scrubAuthTokensFromUrl(href) returns the URL without sct_magic, sct_oauth, sct_imp and sct_sk:
import { scrubAuthTokensFromUrl } from "@scute/js-core";window.history.replaceState({}, "", scrubAuthTokensFromUrl(window.location.href));Two steps: verify, then sign in#
verifyMagicLinkToken(token, url?) verifies without signing in, so you can decide what to do with the session. On success it returns { authPayload, magicPayload } and fires magic_verified; pass the payload to signInWithTokenPayload, or to signInWithRegisterDevice to register a passkey first.
const token = scute.getMagicLinkToken();const { data, error } = await scute.verifyMagicLinkToken(token);if (data && "authPayload" in data && !("signedIn" in data)) { await scute.signInWithRegisterDevice(data.authPayload); // passkey prompt, then sign in}When the URL has sct_sk=true or sct_oauth, verification signs in at once and the result carries signedIn: true.
Finish on the device that asked (cross-device)#
The device that requested the link can wait while the link is opened somewhere else, for example on a phone. Poll the status with the ID from data.magic_link.id:
async function waitForLink(id: string) { for (;;) { const { data, error } = await scute.getMagicLinkStatus(id); if (data) return scute.signInWithMagicLinkStatus(data, id); const code = error?.json?.error_code; if (code === "mfa_required") throw new Error("Finish signing in on the device that opened the link."); await new Promise((r) => setTimeout(r, 2000)); }}Until the link is opened, getMagicLinkStatus returns an error. While the other device finishes its own steps, error.json.error_code is sign_in_pending. signInWithMagicLinkId(id) asks for the status once and finishes in one call.
signInWithMagicLinkStatus(payload, magicLinkId?) returns:
| Result | Meaning |
|---|---|
| { data: null, error: null } | Signed in on this device. |
| { data: { mfaRequired: true, ... } } or { data: { contactVerificationRequired: true, ... } } | This device has its own step to do. |
| { data: { verificationRequired: true, ... } } | Attack protection wants a code on this device. |
| { error: MagicLinkWindowClosedError } | The link was opened too long ago for this device to pick up the sign-in. Start again. |
The ScuteSignIn component polls every 2 seconds and gives up after 10 minutes.
Attack protection#
For an unusual sign-in, the API can answer a link with a code challenge instead of a session (HTTP 202). Nothing is signed in yet:
{ verificationRequired: true, verification: { token, method, expires_at } | null, reason?, threats: string[] }verification_required fires and scute.pendingSignInVerification holds the challenge. Ask the user for the code that was sent, then:
const { data, error } = await scute.verifySignInVerification(code);This verifies the code with the challenge token and checks the same link again, which now signs in (or returns the next step). On the device that polls, it asks the status again. Pass { challengeToken, magicToken } or { challengeToken, magicLinkId } as the second argument to use other values than the pending ones.
Invites#
A user invited with admin.inviteUser is pending until they accept. confirmInvite(token, userMeta) sends the invite link's token with the profile fields and returns a token payload:
const { data, error } = await scute.confirmInvite(token, { first_name: "Ada" });if (data) await scute.signInWithTokenPayload(data);