useAuthenticate
Redirect unauthenticated Solid users to the configured sign-in route.
useAuthenticate calls useSession and, once the query settles, redirects unauthenticated users to the configured sign-in path. The current URL is preserved as a redirectTo query parameter so the user lands back where they started after signing in.
Use this as the primary guard inside protected route components.
Usage
import { useAuthenticate } from "@better-auth-ui/solid"
export function AccountPage() {
const session = useAuthenticate(authClient)
if (session.isPending) return <Spinner />
if (!session.data) return null // navigating to sign-in
return <Account user={session.data.user} />
}The redirect uses basePaths.auth + viewPaths.auth.signIn from AuthProvider: override those to target a custom sign-in view.
Accepts the same arguments as useSession: see its Params.
First-render caveat
The redirect runs inside a Solid createEffect, so it is browser-only. During the first render, session.data is undefined and navigation has not started.
The protected interface can appear briefly before the browser effect redirects to sign-in.
You have two ways to handle this:
Gate on isPending (simplest)
Render a skeleton or spinner while the session query is pending, and null once it resolves to unauthenticated (the redirect is already in flight). This is what the Usage example does and is enough for most apps:
const session = useAuthenticate(authClient)
if (session.isPending) return <Skeleton />
if (!session.data) return null // navigating to sign-in
return <Account user={session.data.user} />No flash of protected content, no server work required: just a brief loading state on first mount. Works identically for SSR, client-rendered, and statically prerendered routes.
Pair with a server-side guard (no loading state)
To render the protected interface immediately without a skeleton, prefetch the session on the server. Then hydrate it into the query cache:
- TanStack Start: check the session in a server route guard or loader, then hydrate the session query for the protected route.
- Solid server runtimes: use the server-side session helpers from
useSessionwith the request headers for the current request.
The server-side check protects the first render. useAuthenticate redirects after token expiration, remote sign-out, or server-side session revocation.
Last updated on