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.
| Event | When |
|---|---|
| initial_session | Right after you subscribe. |
| signed_in | A sign-in finished. |
| signed_out | signOut() ran, or another tab signed out. |
| session_expired | A refresh was refused, or the token expired with auto refresh off. |
| session_refetch | getSession() or refetchSession() read the user again. |
| token_refreshed | The access token was refreshed. |
| webauthn_register_start | A passkey prompt for registration opened. |
| webauthn_register_success | A 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.
| Event | When |
|---|---|
| magic_pending | A sign-in link was sent. |
| magic_new_device_pending | A link was sent because this device has no passkey for the user. |
| magic_verified | A link was verified (two-step flow). |
| magic_verified_oauth | Defined, not emitted by @scute/js-core in this version. |
| otp_pending | A code was sent. |
| otp_new_device_pending | A code was sent because this device has no passkey for the user. |
| webauthn_verify_start | A passkey prompt for sign-in opened. |
| webauthn_verify_success | The passkey assertion was accepted. |
| mfa_required | A second factor is needed. See pendingMfaChallenge. |
| mfa_enrollment_required | A factor has to be set up first. |
| mfa_enrollment_suggested | The grace period nudge, fired before signed_in. |
| mfa_verified | Defined, not emitted by @scute/js-core in this version. |
| verification_required | Attack protection wants a code before a link signs in. |
| contact_verification_required | The 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.