Receive a notification every time an auth event happens.
async
function and it runs synchronously during the processing of the changes causing the event. You can easily create a dead-lock by using await
on a call to another method of the Supabase library.
async
functions as callbacks.await
calls in async
callbacks.supabase.auth.onAuthStateChange((event, session) => \{
setTimeout(async () => \{
// await on other Supabase function here
// this runs right after the callback has finished
\}, 0)
\})
INITIAL_SESSION
SIGNED_IN
SIGNED_OUT
supabase.auth.signOut()
.TOKEN_REFRESHED
supabase.auth.getSession()
for the same purpose.USER_UPDATED
supabase.auth.updateUser()
method finishes successfully. Listen to it to update your application's UI based on new profile information.PASSWORD_RECOVERY
SIGNED_IN
event when the user lands on a page that includes a password recovery link in the URL.A callback function to be invoked when an auth event happens.
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()
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)
\})
\})
\}
\})
// 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')
\}
\})
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>
)
\}
supabase.auth.onAuthStateChange((event, session) => \{
if (event === 'PASSWORD_RECOVERY') \{
console.log('PASSWORD_RECOVERY', session)
// show screen to update user's password
showPasswordResetScreen(true)
\}
\})
supabase.auth.onAuthStateChange((event, session) => \{
if (event === 'SIGNED_IN') console.log('SIGNED_IN', session)
\})
supabase.auth.onAuthStateChange((event, session) => \{
if (event === 'TOKEN_REFRESHED') console.log('TOKEN_REFRESHED', session)
\})
supabase.auth.onAuthStateChange((event, session) => \{
if (event === 'USER_UPDATED') console.log('USER_UPDATED', session)
\})