A Supabase method that listens for authentication events like sign-in, sign-out, and token refresh.
onAuthStateChange is a Supabase method that lets your app react to Authentication events in real time. When a user signs in, signs out, or their Session changes, this listener fires a Callback with the event type and session data.
Common events include:
SIGNED_IN — user just logged inSIGNED_OUT — user just logged outTOKEN_REFRESHED — Access Token was automatically refreshedUSER_UPDATED — user profile changedINITIAL_SESSION — initial auth check on page loadThis method is typically used inside an Auth Provider to keep your app's UI in sync with the user's auth state. For example, when a user logs out in one tab, onAuthStateChange can update the navbar in another tab.
It is part of the Supabase JavaScript library and returns an unsubscribe function so you can clean up the listener in a useEffect cleanup.
We give you the skills to build, deploy, and own a full product. Professional stack, AI co-pilot, no coding background required.