Read the current session
getSession reads the locally persisted session and refreshes an access token that is close to expiration.
const result = await client.auth.getSession() if (result.error) { throw result.error} if (result.data.session) { console.log(result.data.session.access_token) console.log(result.data.session.expires_at)}Refresh explicitly
refreshSession uses the refresh token from the stored session. Pass a refresh token explicitly when the session is managed outside the client.
const result = await client.auth.refreshSession() if (result.error) { throw result.error} console.log(result.data.session)Restore an existing token pair
setSession restores an access-token and refresh-token pair. It refreshes an expired access token or validates a current one, then stores the resulting session.
const result = await client.auth.setSession({ access_token, refresh_token}) if (result.error) { throw result.error} console.log(result.data.user)console.log(result.data.session)Choose a sign-out scope
The target determines which sessions are revoked. When target is omitted, global is used.
- local: Revoke only the current session.
- others: Revoke every session except the current session. It does not emit a sign-out event for the current session.
- global: Revoke every session for the user.
const target: 'local' | 'others' | 'global' = 'local' const result = await client.auth.signOut({ target }) if (result.error) { throw result.error}Lifecycle controls
startAutoRefresh begins automatic token refresh. stopAutoRefresh stops it. dispose also closes the cross-tab BroadcastChannel and removes all Auth state listeners.
await client.auth.startAutoRefresh()await client.auth.stopAutoRefresh() // Stop refresh, close the BroadcastChannel, and remove listeners.await client.auth.dispose()