TanStack Start integration for Authverse projects
Authverse can be easily integrated with TanStack Start to deliver a secure, modern, and server-first authentication experience.
Before getting started, make sure you already have an Authverse instance configured.
If you haven’t done this yet, please check the Installation guide.
TanStack Start works seamlessly with Authverse because it provides:
This makes it an excellent choice for authentication-heavy applications such as dashboards, admin panels, and SaaS platforms.
In many cases, you may want to restrict access to certain pages (such as a dashboard) so that only authenticated users can access them.
Authverse, together with Better Auth, allows you to protect pages using an authentication middleware that runs on the server.
This ensures that unauthenticated users are blocked before the page is rendered.
/auth/loginOnce the middleware is set up, you can attach it to any route that requires authentication.
Always import the middleware from its exact path:
import { authMiddleware } from "@/lib/middleware/auth";/dashboard Pageimport { createFileRoute } from "@tanstack/react-router";
import { authMiddleware } from "@/lib/middleware/auth";
export const Route = createFileRoute("/dashboard")({
component: RouteComponent,
server: {
middleware: [authMiddleware],
},
});
function RouteComponent() {
return <div>Dashboard</div>;
}You can reuse the same middleware to protect other routes such as:
/profile/settings/billing/adminSimply add authMiddleware to the route’s server.middleware array.
Always protect sensitive pages on the server
Do not rely on client-side checks for authentication
Keep middleware small, reusable, and focused
Extend the middleware later for: