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:

FieldDescription
user.idThe user's unique ID.
user.emailThe user's email address.
user.nameThe user's display name.
user.roleThe user's role (Role.USER or Role.ADMIN).
user.planThe user's current subscription plan name.
user.creditsThe 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.