mcpField
U
using-authentication

using-authentication

github.com/andrelandgrafSecurity2394 installsUpdated 20w ago
Repository

About using-authentication

A Fullstack Recipes skill showing developers how to use Better Auth for client- and server-side authentication. It covers sessions, route protection, sign-in and sign-out, and user-specific data.

Key features

  1. 01Client-side session hooks
  2. 02Server-side session access
  3. 03Protected route patterns
  4. 04Email sign-in and sign-up
  5. 05Fetching user-specific data

Use cases

  1. 01Display a signed-in user's session
  2. 02Require authentication for pages and API routes
  3. 03Build sign-in and registration flows

Install

Copy the command and run it in a terminal.

$npx skills add https://github.com/andrelandgraf/fullstackrecipes --skill using-authentication

After installing, type/using-authenticationin Claude Code to confirm it loaded.

Details

From the project README

Working with Authentication

Use Better Auth for client and server-side authentication. Covers session access, protected routes, sign in/out, and fetching user data.

Implement Working with Authentication

Use Better Auth for client and server-side authentication. Covers session access, protected routes, sign in/out, and fetching user data.

See:


Client-Side Authentication

Use the auth client hooks in React components:

"use client";

import { useSession, signOut } from "@/lib/auth/client";

export function UserMenu() {
  const { data: session, isPending } = useSession();

  if (isPending) return <div>Loading...</div>;
  if (!session) return <a href="/sign-in">Sign In</a>;

  return (
    <div>
      <span>{session.user.name}</span>
      <button onClick={() => signOut()}>Sign Out</button>
    </div>
  );
}
Server-Side Session Access

Get the session in Server Components and API routes:

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

// In a Server Component
export default async function Page() {
  const session = await auth.api.getSession({
    headers: await headers(),
  });

  if (!session) {
    return <div>Not signed in</div>;
  }

  return <div>Hello, {session.user.name}</div>;
}
// In an API route
export async function POST(request: Request) {
  const session = await auth.api.getSession({
    headers: await headers(),
  });

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

  // Use session.user.id for queries...
}
Protected Pages Pattern

Redirect unauthenticated users:

import { redirect } from "next/navigation";
import { headers } from "next/headers";
import { auth } from "@/lib/auth/server";

export default async function ProtectedPage() {
  const session = await auth.api.getSession({
    headers: await headers(),
  });

  if (!session) {
    redirect("/sign-in");
  }

  return <Dashboard user={session.user} />;
}
Auth Pages Pattern

Redirect authenticated users away from auth pages:

import { redirect } from "next/navigation";
import { headers } from "next/headers";
import { auth } from "@/lib/auth/server";

export default async function SignInPage() {
  const session = await auth.api.getSession({
    headers: await headers(),
  });

  if (session) {
    redirect("/chats"); // Already signed in
  }

  return <SignIn />;
}
Signing In
import { signIn } from "@/lib/auth/client";

// Email/password
await signIn.email({
  email: "[email protected]",
  password: "password",
  callbackURL: "/chats",
});

// Social provider
await signIn.social({
  provider: "google",
  callbackURL: "/chats",
});
Signing Up
import { signUp } from "@/lib/auth/client";

await signUp.email({
  email: "[email protected]",
  password: "password",
  name: "John Doe",
  callbackURL: "/verify-email",
});
Signing Out
import { signOut } from "@/lib/auth/client";

await signOut({
  fetchOptions: {
    onSuccess: () => {
      router.push("/");
    },
  },
});
Fetching User Data After Auth

In protected pages, fetch user-specific data after validating the session:

export default async function DashboardPage() {
  const session = await auth.api.getSession({
    headers: await headers(),
  });

  if (!session) {
    redirect("/sign-in");
  }

  const [chats, profile] = await Promise.all([
    getUserChats(session.user.id),
    getUserProfile(session.user.id),
  ]);

  return <Dashboard chats={chats} profile={profile} />;
}

References

Comments

Markdown supported