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.

Ready to Build Something Real?

We give you the skills to build, deploy, and own a full product. Professional stack, AI co-pilot, no coding background required.