onAuthStateChange()
Receive a notification every time an auth event happens.
1supabase.auth.onAuthStateChange((event, session) => {
2 console.log(event, session)
3})
Parameters#
callbackrequired
object
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#
1supabase.auth.onAuthStateChange((event, session) => {
2 console.log(event, session)
3})
Listen to sign in#
1supabase.auth.onAuthStateChange((event, session) => {
2 if (event == 'SIGNED_IN') console.log('SIGNED_IN', session)
3})
Listen to sign out#
1supabase.auth.onAuthStateChange((event, session) => {
2 if (event == 'SIGNED_OUT') console.log('SIGNED_OUT', session)
3})
Listen to token refresh#
1supabase.auth.onAuthStateChange((event, session) => {
2 if (event == 'TOKEN_REFRESHED') console.log('TOKEN_REFRESHED', session)
3})
Listen to user updates#
1supabase.auth.onAuthStateChange((event, session) => {
2 if (event == 'USER_UPDATED') console.log('USER_UPDATED', session)
3})
Listen to user deleted#
1supabase.auth.onAuthStateChange((event, session) => {
2 if (event == 'USER_DELETED') console.log('USER_DELETED', session)
3})
Listen to password recovery events#
1supabase.auth.onAuthStateChange((event, session) => {
2 if (event == 'PASSWORD_RECOVERY') console.log('PASSWORD_RECOVERY', session)
3})