Supabaseintermediate

onAuthStateChange

A Supabase method that listens for authentication events like sign-in, sign-out, and token refresh.

Detailed Explanation

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 in
  • SIGNED_OUT — user just logged out
  • TOKEN_REFRESHEDAccess Token was automatically refreshed
  • USER_UPDATED — user profile changed
  • INITIAL_SESSION — initial auth check on page load

This 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.

Free forever

Start building software you actually own.

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.