---
name: aiskillstore-authentication-logic
description: "Guide to using Better Auth for client and server-side authentication."
metadata:
  decimalai-skill-id: "d5c6bc34-1555-4ef0-8eb8-20146ed0e007"
  decimalai-version-id: "87ba0e8d-d5be-465d-a2ed-5b3290322372"
  decimalai-version: "1"
  decimalai-body-sha256: "516b77aafdf6f8dcc8b8ae45b3598e61c92ea051af347e807e875088bc59e662"
  decimalai-name: "aiskillstore/authentication-logic"
  decimalai-format: "1"
  decimalai-invocation: "model"
  decimalai-category: "coding"
  decimalai-source: "https://app.decimal.ai/s/aiskillstore-authentication-logic@1/SKILL.md"
---
# Authentication Logic

## Overview
We use **Better Auth** (`better-auth`) for identifying users.

## Config
- **Client**: `lib/auth-client.ts` exports `authClient`.
- **Server**: `lib/auth.ts` exports `auth`.

## Client-Side Usage
Use `authClient` for signing in, signing out, and checking session state in Client Components.

```tsx
import { authClient } from "@/lib/auth-client";

// Sign In
await authClient.signIn.email({
  email,
  password,
});

// Social Sign In
await authClient.signIn.social({
  provider: "google",
  callbackURL: "/onboarding", 
});

// Sign Out
await authClient.signOut();
```

## Server-Side Usage
Use `auth.api.getSession` for protecting API routes or Server Actions.

```ts
import { auth } from "@/lib/auth";
import { headers } from "next/headers";

const session = await auth.api.getSession({
  headers: await headers()
});

if (!session) {
  return new Response("Unauthorized", { status: 401 });
}
```

## AuthBar Component
- Located at `textbook/src/components/AuthBar/index.tsx`.
- Displays user avatar or login button.
- Fetches session from `/api/auth/session` (Next.js API route proxying Better Auth).
