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.
Learn the concepts, fix things with confidence, and ship real products with AI beside you. No coding background required, and we're with you from the first idea to launch.
Free to start. No card. Leave whenever you want.