The primary composable for accessing authentication state. Returns reactive user, session, and auth client.
const { loggedIn, user, session, client, signIn, signOut } = useUserSession()
true if the user is currently authenticated.true when the initial session fetch is complete.signInProxies Better Auth signIn.
await signIn.email({
email: 'user@example.com',
password: 'password'
})
Methods like signIn return a promise that resolves when the server responds, not when local state updates.
// This awaits the server response
await client.signIn.email({ email, password })
// Local state updates asynchronously after
// Use onSuccess callback for actions that depend on updated state
await client.signIn.email(
{ email, password },
{ onSuccess: () => navigateTo('/dashboard') }
)
signUpProxies Better Auth signUp with the same onSuccess behavior as signIn.
await signUp.email({
email: 'user@example.com',
password: 'password'
})
signOutSigns the user out and clears the local session state.
await signOut()
Options
await signOut({
onSuccess: () => navigateTo('/'),
})
waitForSession()Waits for session state to be ready. Resolves when user is logged in or after 5 second timeout.
await waitForSession()
// Session is now ready (or timed out)
fetchSessionManually triggers a session refresh. Useful if you've updated user data on the server via a side channel.
await fetchSession()
Options
await fetchSession({
headers, // optional HeadersInit
force: true, // disables Better Auth cookie cache for this fetch
})
updateUserOptimistically updates the local user object.
updateUser({ name: 'New Name' })
user and session are global states using useState. Changes in one component are instantly reflected everywhere.