Skip to main content
Version: v2 RC

onAuthStateChange()

Receive a notification every time an auth event happens.

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

Parameters

  • callbackrequiredobject

    A callback function to be invoked when an auth event happens.

Notes

  • Types of auth events: SIGNED_IN, SIGNED_OUT, TOKEN_REFRESHED, USER_UPDATED, USER_DELETED, PASSWORD_RECOVERY

Examples

Listen to auth changes

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

Listen to sign in

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

Listen to sign out

supabase.auth.onAuthStateChange((event, session) => {
if (event == 'SIGNED_OUT') console.log('SIGNED_OUT', 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)
})

Listen to user deleted

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

Listen to password recovery events

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