Authentication
Learn how to use the pre-configured authentication in your application.
Authentication is handled by Better Auth, configured in packages/auth/.
Session Data
The session is enriched with additional fields via a custom plugin:
| Field | Description |
|---|---|
user.id | The user's unique ID. |
user.email | The user's email address. |
user.name | The user's display name. |
user.role | The user's role (Role.USER or Role.ADMIN). |
user.plan | The user's current subscription plan name. |
user.credits | The user's running balance. |
Server-Side Usage
In server components and tRPC procedures, the session is available through the context:
// In tRPC procedures
export const userRouter = router({
getProfile: protectedProcedure.query(({ ctx }) => {
const userId = ctx.session.user.id;
return userService.getProfile(userId);
}),
});Client-Side Usage
Use the Better Auth client hook in client components:
"use client";
import { authClient } from "@package/auth/client";
export function UserProfile() {
const { data: session } = authClient.useSession();
if (!session) {
return null;
}
return <p>Welcome, {session.user.name}</p>;
}Route Protection
The dashboard application automatically redirects unauthenticated users to the sign-in page. All (main)/ routes require authentication.
For API protection, see the API section.