JavaScript: Listen to auth events

Receive a notification every time an auth event happens.

Parameters

Examples

Listen to auth changes

const \{ data \} = supabase.auth.onAuthStateChange((event, session) => \{
  console.log(event, session)

  if (event === 'INITIAL_SESSION') \{
    // handle initial session
  \} else if (event === 'SIGNED_IN') \{
    // handle sign in event
  \} else if (event === 'SIGNED_OUT') \{
    // handle sign out event
  \} else if (event === 'PASSWORD_RECOVERY') \{
    // handle password recovery event
  \} else if (event === 'TOKEN_REFRESHED') \{
    // handle token refreshed event
  \} else if (event === 'USER_UPDATED') \{
    // handle user updated event
  \}
\})

// call unsubscribe to remove the callback
data.subscription.unsubscribe()

Listen to sign out

supabase.auth.onAuthStateChange((event, session) => \{
  if (event === 'SIGNED_OUT') \{
    console.log('SIGNED_OUT', session)

    // clear local and session storage
    [
      window.localStorage,
      window.sessionStorage,
    ].forEach((storage) => \{
      Object.entries(storage)
        .forEach(([key]) => \{
          storage.removeItem(key)
        \})
    \})
  \}
\})

Store OAuth provider tokens on sign in

// Register this immediately after calling createClient!
// Because signInWithOAuth causes a redirect, you need to fetch the
// provider tokens from the callback.
supabase.auth.onAuthStateChange((event, session) => \{
  if (session && session.provider_token) \{
    window.localStorage.setItem('oauth_provider_token', session.provider_token)
  \}

  if (session && session.provider_refresh_token) \{
    window.localStorage.setItem('oauth_provider_refresh_token', session.provider_refresh_token)
  \}

  if (event === 'SIGNED_OUT') \{
    window.localStorage.removeItem('oauth_provider_token')
    window.localStorage.removeItem('oauth_provider_refresh_token')
  \}
\})

Use React Context for the User's session

const SessionContext = React.createContext(null)

function main() \{
  const [session, setSession] = React.useState(null)

  React.useEffect(() => \{
    const \{data: \{ subscription \}\} = supabase.auth.onAuthStateChange(
      (event, session) => \{
        if (event === 'SIGNED_OUT') \{
          setSession(null)
        \} else if (session) \{
          setSession(session)
        \}
      \})

    return () => \{
      subscription.unsubscribe()
    \}
  \}, [])

  return (
    <SessionContext.Provider value=\{session\}>
      <App />
    </SessionContext.Provider>
  )
\}

Listen to password recovery events

supabase.auth.onAuthStateChange((event, session) => \{
  if (event === 'PASSWORD_RECOVERY') \{
    console.log('PASSWORD_RECOVERY', session)
    // show screen to update user's password
    showPasswordResetScreen(true)
  \}
\})

Listen to sign in

supabase.auth.onAuthStateChange((event, session) => \{
  if (event === 'SIGNED_IN') console.log('SIGNED_IN', session)
\})

Listen to token refresh

supabase.auth.onAuthStateChange((event, session) => \{
  if (event === 'TOKEN_REFRESHED') console.log('TOKEN_REFRESHED', session)
\})

Listen to user updates

supabase.auth.onAuthStateChange((event, session) => \{
  if (event === 'USER_UPDATED') console.log('USER_UPDATED', session)
\})