JavaScript SDK

Auth events

Listen to sign-in, sign-out, refresh and sign-in step events with onAuthStateChange.

onAuthStateChange calls your function on every auth event and returns a function that unsubscribes.

const unsubscribe = scute.onAuthStateChange((event, session, user) => {  if (event === "signed_in") console.log("Signed in as", user?.email);  if (event === "signed_out") console.log("Signed out");});unsubscribe();

The callback runs once right away with initial_session. Event names are also exported as constants: AUTH_CHANGE_EVENTS.SIGNED_IN is "signed_in".

Session events#

For these events session and user are the real state: the client reads the session (and fetches the user when needed) before calling you. The list is exported as SESSION_EVENTS.

EventWhen
initial_sessionRight after you subscribe.
signed_inA sign-in finished.
signed_outsignOut() ran, or another tab signed out.
session_expiredA refresh was refused, or the token expired with auto refresh off.
session_refetchgetSession() or refetchSession() read the user again.
token_refreshedThe access token was refreshed.
webauthn_register_startA passkey prompt for registration opened.
webauthn_register_successA passkey was registered.

Step events#

Every other event comes with an unauthenticated placeholder session and user: null. Do not overwrite your session state with them.

EventWhen
magic_pendingA sign-in link was sent.
magic_new_device_pendingA link was sent because this device has no passkey for the user.
magic_verifiedA link was verified (two-step flow).
magic_verified_oauthDefined, not emitted by @scute/js-core in this version.
otp_pendingA code was sent.
otp_new_device_pendingA code was sent because this device has no passkey for the user.
webauthn_verify_startA passkey prompt for sign-in opened.
webauthn_verify_successThe passkey assertion was accepted.
mfa_requiredA second factor is needed. See pendingMfaChallenge.
mfa_enrollment_requiredA factor has to be set up first.
mfa_enrollment_suggestedThe grace period nudge, fired before signed_in.
mfa_verifiedDefined, not emitted by @scute/js-core in this version.
verification_requiredAttack protection wants a code before a link signs in.
contact_verification_requiredThe app wants an email or phone confirmed first.

Session and user in events#

The session passed to listeners and to other tabs never carries the refresh token.

For React, AuthContextProvider subscribes for you and exposes the state through useAuth. See React.