For AI agents: the documentation index is at llms.txt. Guide pages have a markdown version at the same path with .md appended, before any query string or fragment.
Swift: Listen to auth events
Returns an AsyncStream<AuthChange> of important events occurring on the user's session. Each call returns a new stream.
The stream is registered when the call returns, so it misses no change. It finishes when your task is canceled or the client is released.
Each AuthChange has event, session, status (an AuthStatus), and signOutReason.
Events are delivered asynchronously. To read the state right after a call, use supabase.auth.status or supabase.auth.currentSession.
Emitted events:
initialSession
Emitted right after the client reads the initial session from storage.
The session can be expired. With autoRefresh: .automatic, the client tries one refresh of an expired session first, for at most initialRefreshTimeout (5 seconds by default). Check session.isExpired() in your handler.
signedIn
Emitted each time a user session is confirmed or re-established, including on user sign in.
Avoid making assumptions as to when this event is fired, this may occur even when the user is already signed in. Instead, check the user object attached to the event to see if a new user has signed in and update your application's UI.
signedOut
Emitted when the user signs out. This can be after:
A call to supabase.auth.signOut().
After the user's session has expired for any reason:
User has signed out on another device.
The session has reached its timebox limit or inactivity timeout.
User has signed in on another device with single session per user enabled.
signOutReason tells .userInitiated apart from .sessionExpired, .sessionRevoked, and .sessionMissing.
Use this to clean up any local storage your application has associated with the user.
tokenRefreshed
Emitted each time a new access and refresh token are fetched for the signed-in user.
With auto refresh, the client refreshes the session before the access token expires. The token in the event can still be expired when your handler runs, for example after the app resumes. When you need a session for a new request, call try await supabase.auth.session().
The frequency of this event is related to the JWT expiry limit configured on your project.
userUpdated
Emitted each time updateUser(_:redirectTo:) or linkIdentity finishes successfully, after unlinkIdentity(_:) when the client can fetch the updated user, and when the signed-in user verifies an email or phone change. Listen to it to update your application's UI based on new profile information.
passwordRecovery
Emitted instead of the signedIn event when the user opens a password recovery link, or verifies a code with verifyOTP and type .recovery.