Capxul Docs
How-to guides

Authenticate with email OTP

Request, verify, inspect, and clear an SDK session without React.

Create a client, then request a code for the email address:

const canSend = await client.auth.canSendOtp({ email });
if (!canSend.ok) throw canSend.error;

const requested = await client.auth.signIn({ email });
if (!requested.ok) throw requested.error;

Pass the received code back with the same email:

const verified = await client.auth.verifyOtp({ email, code });
if (!verified.ok) {
  switch (verified.error.code) {
    case "OTP_EXPIRED":
    case "INVALID_INPUT":
      // Ask the user to request or enter a new code.
      break;
    default:
      console.error(verified.error);
  }
  return;
}

console.log(verified.value.email);

Inspect the cached session later:

const session = await client.auth.getSession();
if (!session.ok) throw session.error;

if (session.value === null) {
  console.log("Signed out");
}

Clear both the remote and cached session with signOut:

const signedOut = await client.auth.signOut();
if (!signedOut.ok) throw signedOut.error;

All auth methods accept an optional { signal } as the last argument. A pre-aborted signal resolves a CANCELLED result.